May 28, 2021 XPath
XPath uses path expressions to select nodes or sets of nodes in an XML document. A node is selected by following a path or steps.
We'll use this XML document in the following example.
XPath uses path expressions to pick nodes in an XML document. T he node is selected by following a path or step. The most useful path expressions are listed below:
The expression | Describe |
---|---|
nodename | Select all child nodes for this node. |
/ | Select from the root node. |
// | Select nodes in the document from the current node selected by the match, regardless of their location. |
. | Select the current node. |
.. | Select the parent of the current node. |
@ | Select the property. |
In the table below, we have listed some path expressions and the results of the expressions:
The path expression | Results |
---|---|
bookstore | Select all the child nodes of the bookstore element. |
/bookstore |
Select the root element bookstore. Note: If the path starts with a forward slash ( / ), the path always represents the absolute path to an element! |
bookstore/book | Select all book elements that belong to child elements of bookstore. |
book | Select all book child elements, regardless of their location in the document. |
bookstore//book | Select all book elements that belong to descendants of the bookstore element, regardless of where they are located under the bookstore. |
@lang | Select all properties named lang. |
Tip: If XPath starts with a slash (/) it represents an absolute path. If the beginning is two slashes (//) indicating that all elements in the file that fit the pattern are selected, even at different levels in the tree.
A predicate is used to find a particular node or a node that contains a specified value.
The predicate is embedded in square brackets.
In the table below, we list some path expressions with predicates, as well as the results of expressions:
The path expression | Results |
---|---|
/bookstore/book[1] | Select the first book element that belongs to the bookstore child element. |
/bookstore/book[last()] | Select the last book element that belongs to the bookstore child element. |
/bookstore/book[last()-1] | Select the penultimate book element that belongs to the bookstore child element. |
/bookstore/book[position()<3] | Select the first two book elements that belong to the child elements of the bookstore element. |
title[@lang] | Select all title elements that have properties named lang. |
title[@lang='eng'] | Select all title elements, and they have lang properties with a value of eng. |
/bookstore/book[price>35.00] | Select all book elements of the bookstore element, and the value of the price element there must be greater than 35.00. |
/bookstore/book[price>35.00]/title | Select all title elements of the book element in the bookstore element, and the value of the price element there must be greater than 35.00. |
The XPath wildcard can be used to select unknown XML elements.
Wildcard | Describe |
---|---|
* | Matches any element node. |
@* | Matches any property node. |
node() | Matches any type of node. |
In the table below, we list some path expressions and the results of those expressions:
The path expression | Results |
---|---|
/bookstore/* | Select all child elements of the bookstore element. |
//* | Select all the elements in the document. |
title[@*] | Select all title elements with properties. |
By using The New | expression operator, you can select several paths.
In the table below, we list some path expressions and the results of those expressions:
The path expression | Results |
---|---|
book/title | book/price | Select all title and price elements of the book element. |
title | price | Select all the title and price elements in the document. |
/bookstore/book/title | price | Select all title elements that belong to the book element of the bookstore element, as well as all price elements in the document. |
In the next section, you'll learn about the axis of XPath.