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 examples below.
Example
<
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
bookstoreXPath uses path expressions to select nodes in an XML document. Nodes are selected by following a path or steps. The most useful path expressions are listed below:
| Expression | Description |
|---|---|
| nodename | Selects all child nodes of this node. |
| / | Selects from the root node (takes child nodes). |
| // | Selects nodes in the document from the current node that matches the selection, regardless of their position (takes descendant nodes). |
| . | Selects the current node. |
| .. | Selects the parent node 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 nodes named bookstore. |
| /bookstore |
Selects the root element bookstore. Note: if a path starts with a forward slash ( / ), it 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, no matter where they are in the document. |
| bookstore//book | Selects all book elements that are descendants of the bookstore element, no matter where they sit below bookstore. |
| //@lang | Selects all attributes named lang. |
Predicates
Predicates are used to find a specific node or a node containing 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 bookstore. |
| /bookstore/book[last()] | Selects the last book element that is a child of bookstore. |
| /bookstore/book[last()-1] | Selects the second-to-last book element that is a child of bookstore. |
| /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 whose price element has a value greater than 35.00. |
| /bookstore/book[price>35.00]//title | Selects all title elements of the book elements in bookstore whose price element has a value 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 that have attributes. |
Selecting several paths
By using the "|" operator in a path expression, you can select several paths.
In the table below we list some path expressions and the results of those expressions:
| Path expression | Result |
|---|---|
| //book/title | //book/price | Selects all title and price elements of the book element. |
| //title | //price | Selects all title and price elements in the document. |
| /bookstore/book/title | //price | Selects all title elements of the book elements belonging to bookstore, and all price elements in the document. |