Skip to content

Commit d459618

Browse files
committed
Merge branch 'maint'
2 parents 756ba35 + 9cd64e3 commit d459618

4 files changed

Lines changed: 145 additions & 6 deletions

File tree

lib/xmerl/src/xmerl.erl

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ A callback module can inherit definitions from other callback modules, through
154154
the required function `'#xml-interitance#'() -> [ModuleName::atom()]`.
155155

156156
_See also:_ `export/2`, `export_simple/3`.
157+
158+
## Examples
159+
160+
```erlang
161+
1> {Element, []} = xmerl_scan:string("<greeting>hello</greeting>").
162+
2> lists:flatten(xmerl:export([Element], xmerl_xml, [])).
163+
"<?xml version=\"1.0\"?><greeting>hello</greeting>"
164+
```
165+
166+
```erlang
167+
1> {Element, []} = xmerl_scan:string("<note><to>Tove</to><from>Jani</from></note>").
168+
2> Prolog = ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"].
169+
3> lists:flatten(xmerl:export([Element], xmerl_xml, [{prolog, Prolog}])).
170+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<note><to>Tove</to><from>Jani</from></note>"
171+
```
157172
""".
158173
-spec export(Content, Callback, RootAttributes) ->
159174
ExportedFormat :: term() when
@@ -214,6 +229,13 @@ attributes. The XML-data is always converted to normal form before being passed
214229
to the callback module.
215230

216231
_See also:_ `export/3`, `export_simple/2`.
232+
233+
## Examples
234+
235+
```erlang
236+
1> lists:flatten(xmerl:export_simple([{greeting, [], ["hello"]}], xmerl_xml, [])).
237+
"<?xml version=\"1.0\"?><greeting>hello</greeting>"
238+
```
217239
""".
218240
-spec export_simple(Content, Callback, RootAttributes) ->
219241
ExportedFormat :: term() when
@@ -242,7 +264,16 @@ export1(Content, Callbacks, RootAttrs) when is_list(Content) ->
242264
tagdef('#root#',1,[],Args,Callbacks).
243265

244266

245-
-doc "Exports simple XML content directly, without further context.".
267+
-doc """
268+
Exports simple XML content directly, without further context.
269+
270+
## Examples
271+
272+
```erlang
273+
1> lists:flatten(xmerl:export_simple_content([{greeting, [], ["hello"]}], xmerl_xml)).
274+
"<greeting>hello</greeting>"
275+
```
276+
""".
246277
-spec export_simple_content(Content, Callback) -> _ when
247278
Content :: [simple_element()],
248279
Callback :: callback().
@@ -255,6 +286,14 @@ export_simple_content(Content, Callbacks) when is_list(Callbacks) ->
255286

256287
-doc """
257288
Export normal XML content directly, without further context.
289+
290+
## Examples
291+
292+
```erlang
293+
1> {Element, []} = xmerl_scan:string("<greeting>hello</greeting>").
294+
2> lists:flatten(xmerl:export_content([Element], [xmerl_xml])).
295+
"<greeting>hello</greeting>"
296+
```
258297
""".
259298
-spec export_content(Content, Callbacks) -> _ when
260299
Content :: [element()],
@@ -274,7 +313,16 @@ export_content([E | Es], Callbacks) ->
274313
export_content([], _Callbacks) ->
275314
[].
276315

277-
-doc "Export a simple XML element directly, without further context.".
316+
-doc """
317+
Export a simple XML element directly, without further context.
318+
319+
## Examples
320+
321+
```erlang
322+
1> lists:flatten(xmerl:export_simple_element({greeting, [], ["hello"]}, xmerl_xml)).
323+
"<greeting>hello</greeting>"
324+
```
325+
""".
278326
-spec export_simple_element(Element, Callback) -> _ when
279327
Element :: simple_element(),
280328
Callback :: callback().
@@ -284,7 +332,17 @@ export_simple_element(Element, Callback) when is_atom(Callback) ->
284332
export_simple_element(Element, Callbacks) when is_list(Callbacks) ->
285333
export_element(xmerl_lib:expand_element(Element), Callbacks).
286334

287-
-doc "Exports a normal XML element directly, without further context.".
335+
-doc """
336+
Exports a normal XML element directly, without further context.
337+
338+
## Examples
339+
340+
```erlang
341+
1> {Element, []} = xmerl_scan:string("<greeting>hello</greeting>").
342+
2> lists:flatten(xmerl:export_element(Element, xmerl_xml)).
343+
"<greeting>hello</greeting>"
344+
```
345+
""".
288346
-spec export_element(Element, Callback) -> _ when
289347
Element :: element(),
290348
Callback :: callback().
@@ -312,6 +370,14 @@ export_element(#xmlDecl{}, _CBs) ->
312370

313371
-doc """
314372
For on-the-fly exporting during parsing (SAX style) of the XML document.
373+
374+
## Examples
375+
376+
```erlang
377+
1> {Element, []} = xmerl_scan:string("<empty/>").
378+
2> lists:flatten(xmerl:export_element(Element, xmerl_xml, state0)).
379+
"<empty/>"
380+
```
315381
""".
316382
-spec export_element(Element, Callback, CallbackState) ->
317383
ExportedFormat when
@@ -355,7 +421,16 @@ tagdef(Tag,Pos,Parents,Args,CBs) ->
355421
end.
356422

357423

358-
-doc "Find the list of inherited callback modules for a given module.".
424+
-doc """
425+
Find the list of inherited callback modules for a given module.
426+
427+
## Examples
428+
429+
```erlang
430+
1> xmerl:callbacks(xmerl_xml).
431+
[xmerl_xml]
432+
```
433+
""".
359434
-spec callbacks(Module :: module()) -> [module()].
360435
callbacks(Module) ->
361436
Result = check_inheritance(Module, []),

lib/xmerl/src/xmerl_xpath.erl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ Extracts the nodes from the parsed XML tree according the XPath `String`.
126126

127127
`Scalar` is an `#xmlObj{}` record record with the fields `type` and `value`,
128128
where `#xmlObj.type` is `boolean | number | string`.
129+
130+
## Examples
131+
132+
```erlang
133+
1> Xml = "<root><item id=\"a\">one</item><item id=\"b\">two</item></root>".
134+
"<root><item id=\"a\">one</item><item id=\"b\">two</item></root>"
135+
2> {Doc, []} = xmerl_scan:string(Xml).
136+
...
137+
3> xmerl_xs:value_of(xmerl_xpath:string("/root/item[2]", Doc)).
138+
["two"]
139+
```
129140
""".
130141
-spec string(String, Node, Parents, Doc, Options) ->
131142
[nodeEntity()] | Scalar when

lib/xmerl/src/xmerl_xs.erl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,24 @@ Example, original XSLT:
6969

7070
becomes in Erlang:
7171

72-
```text
72+
```erlang
7373
template(E = #xmlElement{ parents=[{'doc',_}|_], name='title'}) ->
7474
["<h1>",
7575
xslapply(fun template/1, E),
7676
"</h1>"];
7777

7878
```
79+
80+
## Examples
81+
82+
```erlang
83+
1> Xml = "<root><item>one</item><item>two</item></root>".
84+
"<root><item>one</item><item>two</item></root>"
85+
2> {Doc, []} = xmerl_scan:string(Xml).
86+
...
87+
3> xmerl_xs:xslapply(fun(E) -> xmerl_xs:value_of(E) end, xmerl_xs:select("/root/item", Doc)).
88+
[["one"],["two"]]
89+
```
7990
""".
8091
-spec xslapply(Fun, ElementList) -> io_lib:chars() when
8192
Fun :: fun ((xmerl:element()) -> io_lib:chars() ),
@@ -108,6 +119,17 @@ becomes:
108119
value_of(select(".", E)), "</h1></div>"]
109120
110121
```
122+
123+
## Examples
124+
125+
```erlang
126+
1> Xml = "<root><item>one</item><item>two</item></root>".
127+
"<root><item>one</item><item>two</item></root>"
128+
2> {Doc, []} = xmerl_scan:string(Xml).
129+
...
130+
3> xmerl_xs:value_of(xmerl_xs:select("/root/item[2]", Doc)).
131+
["two"]
132+
```
111133
""".
112134
-spec value_of(E) -> io_lib:chars() when
113135
E :: xmerl:element() | [xmerl:element()].
@@ -125,6 +147,17 @@ Extract the nodes from the xml tree according to XPath.
125147
Equivalent to [`xmerl_xpath:string(Str, E)`](`xmerl_xpath:string/2`).
126148

127149
_See also:_ `value_of/1`.
150+
151+
## Examples
152+
153+
```erlang
154+
1> Xml = "<root><item>one</item><item>two</item></root>".
155+
"<root><item>one</item><item>two</item></root>"
156+
2> {Doc, []} = xmerl_scan:string(Xml).
157+
...
158+
3> xmerl_xs:value_of(xmerl_xs:select("/root/item[2]", Doc)).
159+
["two"]
160+
```
128161
""".
129162
-spec select(String, E) -> Result when
130163
String :: term(),
@@ -146,6 +179,19 @@ The default fallback behaviour.
146179

147180
Template funs should end with:
148181
`template(E) -> built_in_rules(fun template/1, E)`.
182+
183+
## Examples
184+
185+
```erlang
186+
1> Xml = "<root><item>one</item></root>".
187+
"<root><item>one</item></root>"
188+
2> {Doc, []} = xmerl_scan:string(Xml).
189+
...
190+
3> [Text] = xmerl_xs:select("/root/item/text()", Doc).
191+
...
192+
4> xmerl_xs:built_in_rules(fun(_) -> [] end, Text).
193+
"one"
194+
```
149195
""".
150196
-spec built_in_rules(Fun, E :: xmerl:element()) -> io_lib:chars() when
151197
Fun :: fun ((xmerl:element()) -> io_lib:chars()).

lib/xmerl/test/xmerl_SUITE.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
%% Test groups
4444
%%----------------------------------------------------------------------
4545
all() ->
46-
[{group, cpd_tests}, xpath_text1, xpath_main,
46+
[doctests,
47+
{group, cpd_tests}, xpath_text1, xpath_main,
4748
xpath_abbreviated_syntax, xpath_functions, xpath_namespaces,
4849
{group, misc}, {group, eventp_tests},
4950
{group, ticket_tests}, {group, app_test},
@@ -116,6 +117,12 @@ end_per_testcase(_Func,_Config) ->
116117
%%----------------------------------------------------------------------
117118
%% Test cases
118119
%%----------------------------------------------------------------------
120+
doctests(_Config) ->
121+
Options = [{missing_tests, []}],
122+
ok = ct_doctest:module(xmerl, Options),
123+
ok = ct_doctest:module(xmerl_xpath, Options),
124+
ok = ct_doctest:module(xmerl_xs, Options).
125+
119126
cpd_invalid1(Config) ->
120127
file:set_cwd(datadir(Config)),
121128
case catch xmerl_scan:file(datadir_join(Config,[cpd,"cpd_test.xml"]),[]) of

0 commit comments

Comments
 (0)