Introduction to XPath - Getting Elements
(Page 3 of 4 )
You can use indexing of elements to give you results similar to arrays. The /book expression selects both <book> elements. To get the first <book> element use /book[1]. Similarly, for the second <book> element use /book[2] or/book[last()]. The last() function identifies the last element.
/book[last()]
<bookstore>
<book>
<title id='1'>XPath Tutorial</title>
<author>Mamun Zaman</author>
<year>2007</year>
<price>00.99</price>
</book>
<book>
<title id='2'>AJAX Tutorial</title>
<author>Charles</author>
<year>2007</year>
<price>03.45</price>
</book>
</bookstore>
Attributes are specified by the @ prefix. The /book/title/@id expression selects only the attribute as mentioned below.
<bookstore>
<book>
<title id='1'>XPath Tutorial</title>
<author>Mamun Zaman</author>
<year>2007</year>
<price>00.99</price>
</book>
<book>
<title id='2'>AJAX Tutorial</title>
<author>Charles</author>
<year>2007</year>
<price>03.45</price>
</book>
</bookstore>
//B[@id] selects B elements which have the id attribute in the following XML.
<A>
<B id = "b1"/>
<B id = "b2"/>
<B name = "bname"/>
<B/>
</A>
//B[@id='b1'] selects B elements which have the id attribute with a value of b1.
<A>
<B id = "b1"/>
<B name = " xyz "/>
<B name = "bbb"/>
</A>
The name() function returns the name of the element. Some other functions for selections are starts-with() and contains(). The //*[starts-with(name(),'B')] expression selects all elements whose names start with 'B'. The//*[contains(name(),'C')] expression selects all elements whose names contain 'C'. The string-length() function returns the length of the input string. The //*[string-length(name())>5] expression selects all the elements whose names are five or more letters long.
Several paths can be combined with the pipe ( | ) separator. The //year | //price expression selects all year and price elements.
<bookstore>
<book>
<title id='1'>XPath Tutorial</title>
<author>Mamun Zaman</author>
<year>2007</year>
<price>00.99</price>
</book>
<book>
<title id='2'>AJAX Tutorial</title>
<author>Charles</author>
<year>2007</year>
<price>03.45</price>
</book>
</bookstore>
To find a node of type text use the text() function. To get the year value use //year/text().
<bookstore>
<book>
<title id='1'>XPath Tutorial</title>
<author>Mamun Zaman</author>
<year>2007</year>
<price>00.99</price>
</book>
<book>
<title id='2'>AJAX Tutorial</title>
<author>Charles</author>
<year>2007</year>
<price>03.45</price>
</book>
</bookstore>
XPath defines four data types: node-sets, strings, numbers and Booleans.
The available operators are:
- The "/", "//" and "[...]" operators, used in path expressions=.
- A union operator, "|", which forms the union of two node-sets.
- Boolean operators "and" and "or", and a function "not()"
- Arithmetic operators "+", "-", "*", "div" (divide), and "mod"
- Comparison operators "=", "!=", "<", ">", "<=", ">="
The function library includes:
- Functions to manipulate strings: concat(), substring(), contains(), substring-before(), substring-after(), translate(), normalize-space(), string-length()
- Functions to manipulate numbers: sum(), round(), floor(), ceiling()n>
- Functions to get properties of nodes: name(), local-name(), namespace-uri()
- Functions to get information about the processing context: position(), last()
- Type conversion functions: string(), number(), boolean()
Some of the more common and useful functions are detailed in the next section. For a complete description, see the W3C Recommendation document. W3Schools also has some nice XPath articles.
Next: Using XPath >>
More XML Articles
More By Mamun Zaman