Latest web development tutorials

XPath syntax

XPath uses path expressions to select nodes in an XML document or set of nodes. By node along the path (path) or step (steps) to the selected.


XML instance documents

We will use this XML document in the examples below.

<?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 node

XPath uses path expressions to select nodes in an XML document. Or along a path through the node to select the step. Listed below are the most useful path expressions:

expression description
nodename Select all the child nodes of this node.
/ Select from the root node.
// Select the document from the matching node currently selected node, regardless of their location.
. Select the current node.
.. Select the parent of the current node.
@ Select Properties.

In the table below, we've listed some of the path expressions and the result of the expression:

Path expression result
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 is always representative of the absolute path to an element!

bookstore / book Select the sub-elements belonging to the bookstore all book elements.
// Book Select all book sub-elements, regardless of their position in the document.
bookstore // book Selects all book elements that belong to the descendants of the bookstore element, no matter what position they are in and under the bookstore.
// @ Lang Select all of the properties named lang.


Predicate (Predicates)

Predicate is used to find a specific node or a node that contains the value specified.

Predicate is embedded in square brackets.

In the table below, we have listed some path expressions with predicates and the result of the expression:

Path expression result
/ Bookstore / book [1] Select the sub-elements belonging bookstore first book element.
/ Bookstore / book [last ()] Select the sub-elements belonging bookstore last book element.
/ Bookstore / book [last () - 1] Select the sub-elements belonging to the reciprocal bookstore second book element.
/ Bookstore / book [position () <3] Select the first two book elements of the bookstore element belonging to sub-elements.
// Title [@lang] Select all attribute named lang has a title element.
// Title [@ lang = 'eng'] Select all the title elements, and these elements have value eng lang attribute.
/bookstore/book[price>35.00] Select all the book elements of the bookstore element, and the value of the price element which must be greater than 35.00.
/bookstore/book[price>35.00]/title Select all the title elements of the book elements of the bookstore element, and wherein the value of the price element shall be greater than 35.00.


Select 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, as well as the results of these expressions:

Path expression result
/ Bookstore / * Select the bookstore element of all child elements.
// * Select all elements in the document.
// Title [@ *] Select all elements with title attribute.


Select several paths

By using the path expression "|" operator, you can select several paths.

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

Path expression result
// Book / title | // book / price Select the book title and price elements of all the elements.
// Title | // price Select all documents of title and price elements.
/ Bookstore / book / title | // price Select book bookstore element elements belonging to all the title elements, and document all the price elements.