XPath Syntax
XPath uses path expressions to select nodes or node sets in an XML document. Nodes are selected by following a path or steps.
XML Example Document
We will use this XML document in the following examples.
Example
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
Selecting Nodes
XPath uses path expressions to select nodes in an XML document. Nodes are selected by following a path or steps. Here are the most useful path expressions:
Expression | Description |
---|---|
nodename | Selects all child nodes of this node. |
/ | Selects from the root node (child node). |
// | Selects nodes in the document from the current node that match the selection, regardless of their position (descendant nodes). |
. | Selects the current node. |
.. | Selects the parent of the current node. |
@ | Selects attributes. |
In the table below, we have listed some path expressions and the results of those expressions:
Path Expression | Result |
---|---|
bookstore | Selects all child nodes of the bookstore element. |
/bookstore | Selects the root element bookstore. Note: If the path starts with a forward slash ( / ), this path always represents an absolute path to an element! |
bookstore/book | Selects all book elements that are children of bookstore. |
//book | Selects all book child elements, regardless of their position in the document. |
bookstore//book | Selects all book elements that are descendants of the bookstore element, regardless of their position under bookstore. |
//@lang | Selects all attributes named lang. |
Predicates
Predicates are used to find a specific node or a node that contains a specified value.
Predicates are embedded in square brackets.
In the table below, we list some path expressions with predicates, and the results of those expressions:
Path Expression | Result |
---|---|
/bookstore/book[1] | Selects the first book element that is a child of the bookstore element. |
/bookstore/book[last()] | Selects the last book element that is a child of the bookstore element. |
/bookstore/book[last()-1] | Selects the second-to-last book element that is a child of the bookstore element. |
/bookstore/book[position()<3] | Selects the first two book elements that are children of the bookstore element. |
//title[@lang] | Selects all title elements that have an attribute named lang. |
//title[@lang='eng'] | Selects all title elements that have a lang attribute with the value eng. |
/bookstore/book[price>35.00] | Selects all book elements of the bookstore element where the price element's value is greater than 35.00. |
/bookstore/book[price>35.00]//title | Selects all title elements of the book elements in the bookstore element where the price element's value is greater than 35.00. |
Selecting Unknown Nodes
XPath wildcards can be used to select unknown XML elements.
Wildcard | Description |
---|---|
* | Matches any element node. |
@* | Matches any attribute node. |
node() | Matches any type of node. |
In the table below, we list some path expressions and the results of those expressions:
Path Expression | Result |
---|---|
/bookstore/* | Selects all child elements of the bookstore element. |
//* | Selects all elements in the document. |
//title[@*] | Selects all title elements with any attribute. |
Selecting Multiple Paths
You can select multiple paths by using the "|" operator in the path expression.
In the table below, we list some path expressions and the results of those expressions:
| Path Expression | Result | | //book/title | //book/price | Select all title and price elements of the book element. | | //title | //price | Select all title and price elements in the document. | | /bookstore/book/title | //price | Select all title elements of the book element that belong to the bookstore element, and all price elements in the document. |