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

<?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. 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 of the current node.
@ Selects attributes.

In the table below, we have listed some path expressions and the results of the expressions:

Path Expression Result
bookstore Selects all nodes named bookstore.
/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 where they are located 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 have listed some path expressions with predicates, and the results of the 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 a value of eng.
/bookstore/book[price>35.00] Selects all book elements of the bookstore element, and the value of their price element must be greater than 35.00.
/bookstore/book[price>35.00]//title Selects all title elements of book elements in the bookstore element, and the value of their price element must be 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 have listed some path expressions and the results of these 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 path expressions, you can select several paths.

In the table below, we have listed some path expressions and the results of these expressions:

Path Expression Result
//book/title | //book/price Selects all title and price elements of book elements.
//title | //price Selects all title and price elements in the document.
/bookstore/book/title | //price Selects all title elements of book elements that belong to the bookstore element, as well as all price elements in the document.
Other Extensions