XPath Syntax

XPath gebruikt padexpressies om knopen of knopenverzamelingen in een XML-document te selecteren. Knopen worden geselecteerd door te volgen van een pad (pad) of stappen (stappen).

XML voorbeelddocument

We zullen in de volgende voorbeelden deze XML-document gebruiken.

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

Selecteer knopen

XPath gebruikt padexpressies om knopen in een XML-document te selecteren. Knopen worden geselecteerd door te volgen van een pad of een stap.

The following lists the most useful path expressions:

Expression Description
nodename Select all child nodes of this node.
/ Select from the root node.
// From the matching selected current node, select nodes in the document without considering 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 corresponding results:

Path Expression Results
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 children of the bookstore element.
//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 their corresponding results:

Path Expression Results
/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 eng.
/bookstore/book[price>35.00] Select all book elements within the bookstore element, and 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, and 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 path expressions and the results of these expressions:

Path Expression Results
/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 path expressions and the results of these expressions:

Path Expression Results
//book/title | //book/price Select all title and price elements of the book element.
//title | //price Select all 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.