XPath Syntax

XPath uses path expressions to select nodes or node sets in an XML document. Nodes are selected by following a path (path) or step (steps).

XML Example Document

We will use this XML document in the following examples.

<?xml version="1.0" encoding="ISO-8859-1"?>
<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>

Select nodes

XPath uses path expressions to select nodes in an XML document. Nodes are selected by following a path or step.

The following lists the most useful path expressions:

Expression Description
nodename Select all child nodes of this node.
/ Select from the root node.
// Select nodes from the document that match the current selection, regardless of their position.
. Select the current node.
.. Select the parent node of the current node.
@ Select attributes.

Example

In the following table, we have listed some path expressions and their results:

XPath expression Result
bookstore Select all child nodes of the bookstore element.
/bookstore

Select the root element bookstore.

Note: If the path starts with a forward slash (/), then this path always represents an absolute path to an element!

bookstore/book Select all book elements that are child elements of the bookstore.
//book Select all book child elements, regardless of their position in the document.
bookstore//book Select all book elements that are descendants of the bookstore element, regardless of their position under bookstore.
//@lang Select all attributes named lang.

Predicates (Predicates)

Predicates are used to find a specific node or a node that contains a specified value.

Predicates are enclosed in square brackets.

Example

In the following table, we list some path expressions with predicates and their results:

XPath expression Result
/bookstore/book[1] Select the first book element that is a child of the bookstore element.
/bookstore/book[last()] Select the last book element that is a child of the bookstore element.
/bookstore/book[last()-1] Select the second-to-last book element that is a child of the bookstore element.
/bookstore/book[position()<3] Select the first two book elements that are children of the bookstore element.
//title[@lang] Select all title elements that have an attribute named lang.
//title[@lang='eng'] Select all title elements that have a lang attribute with the value of eng.
/bookstore/book[price>35.00] Select all book elements within the bookstore element, where the value of the price element must be greater than 35.00.
/bookstore/book[price>35.00]/title Select all title elements of the book element within the bookstore element, where the value of the price element must be greater than 35.00.

Select unknown nodes

XPath wildcards can be used to select unknown XML elements.

Wildcard Description
* Match any element node.
@* Match any attribute node.
node() Match any type of node.

Example

In the following table, we list some XPath expressions and their results:

XPath expression Result
/bookstore/* Select all child elements of the bookstore element.
//* Select all elements in the document.
//title[@*] Select all title elements with attributes.

Select multiple paths

By using the "|" operator in path expressions, you can select multiple paths.

Example

In the following table, we list some XPath expressions and their results:

XPath expression Result
//book/title | //book/price Select all title and price elements of the book element.
//title | //price Select all title and price elements within the document.
/bookstore/book/title | //price Select all title elements that belong to the book element within the bookstore element, as well as all price elements within the document.