XPath path expression

XPath uses path expressions to select nodes or node sets in XML documents. Nodes are selected by following paths or steps.

XML example documents

We will use this XML document in the following example.

<?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 XML documents. Nodes are selected by following a path or step.

The most useful path expressions are listed below:

expression description
nodename Select all child nodes of this node.
/ Select from the root node.
// Select the nodes in the document from the current node that matches the selection, regardless of their location.
. Select the current node.
.. Select the parent node of the current node.
@ Select attributes.

Examples

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

Path 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 the absolute path to an element!

bookstore/book Select all book elements that are child elements of 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 where they are located under the bookstore.
// @ lang Select all attributes named lang.

Predicates

The predicate is used to find a specific node or a node that contains a specified value.

The predicate is embedded in square brackets.

Examples

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

Path expression result
/bookstore/book[1] Select the first book element that is a child element of bookstore.
/bookstore/book[last()] Select the last book element that is a child element of bookstore.
/bookstore/book[last()-1] Select the penultimate book element that is a child element of the bookstore.
/bookstore/book[position()<3] Select the first two book elements that are child elements of the bookstore element.
// title [@lang] Select all title elements that have an attribute named lang.
// title [@ lang = 'eng'] Select all title elements, and these elements have a lang attribute 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 must be greater than 35.00.
/bookstore/book[price>35.00]/title Select all the title elements of the book element in the bookstore element, and the value of the price element must be greater than 35.00.

Select unknown node

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.

Examples

In the following table, we list some path expressions and the results of these expressions:

Path 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 several paths

You can choose several paths by using the "|" operator in the path expression.

Examples

In the following table, we list some path expressions and the results of these expressions:

Path expression result
//book/title | //book/price Select all the 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, and all price elements in the document.
Published 210 original articles · praised 37 · 170,000 views +

Guess you like

Origin blog.csdn.net/u012757419/article/details/103802248