Skip to content

Commit 73b163a

Browse files
committed
add XPath docs
1 parent ad8a42e commit 73b163a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

docs/src/manual.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,56 @@ shell> cat out.xml
296296
297297
```
298298

299+
XPath queries
300+
-------------
301+
302+
[XPath](https://en.wikipedia.org/wiki/XPath) is a query language for XML. The
303+
user can retrieve target elements using a short string query. For example,
304+
`"//genus/species"` selects all "species" elements just under a "genus" element.
305+
306+
The `find` (`findfirst` and `findlast`) function is overloaded for XPath query and returns a vector of
307+
selected nodes:
308+
```jlcon
309+
julia> primates = readxml("primates.xml")
310+
EzXML.Document(EzXML.Node(<DOCUMENT_NODE@0x00007fbeddc2a1d0>))
311+
312+
julia> find(primates, "/primates") # Find the "primates" element just under the document
313+
1-element Array{EzXML.Node,1}:
314+
EzXML.Node(<ELEMENT_NODE@0x00007fbeddc1e190>)
315+
316+
julia> find(primates, "//genus")
317+
2-element Array{EzXML.Node,1}:
318+
EzXML.Node(<ELEMENT_NODE@0x00007fbeddc12c50>)
319+
EzXML.Node(<ELEMENT_NODE@0x00007fbeddc16ea0>)
320+
321+
julia> println(findfirst(primates, "//genus"))
322+
<genus name="Homo">
323+
<species name="sapiens">Human</species>
324+
</genus>
325+
326+
```
327+
328+
If you would like to change the starting node of a query, you can pass the node
329+
as the first argument of `find`:
330+
```jlcon
331+
julia> genus = findfirst(primates, "//genus")
332+
EzXML.Node(<ELEMENT_NODE@0x00007fbeddc12c50>)
333+
334+
julia> println(genus)
335+
<genus name="Homo">
336+
<species name="sapiens">Human</species>
337+
</genus>
338+
339+
julia> println(findfirst(genus, "species"))
340+
<species name="sapiens">Human</species>
341+
342+
```
343+
344+
`find(<node>, <xpath>)` automatically registers namespaces applied to `<node>`,
345+
which means prefixes are available in the XPath query. This is especially useful
346+
when an XML document is composed of elements originated from different
347+
namespaces.
348+
299349
Streaming interfaces
300350
--------------------
301351

0 commit comments

Comments
 (0)