Reference Document 1: https: //www.w3school.com.cn/xpath/xpath_syntax.asp
Reference Document 2: https://www.runoob.com/xpath/xpath-tutorial.html
XML instance document
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 an XML document. Along the path through the node, or to select a step.
Listed below are the most useful path expressions:
expression | description |
---|---|
nodename | Select all the child nodes of this node. |
/ | From the root node selected. |
// | From match to select the document node selection of the current node, regardless of their location . |
. | Select the current node. |
.. | Select the parent of the current node. |
@ | Select Properties . |
Examples
In the table below, we have listed the results of some path expressions and expressions:
Path expression | result |
---|---|
bookstore | Selects 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 is always representative of the absolute path to an element! |
bookstore/book | Select the sub-elements belonging to the bookstore of the book all the elements. |
//book | Select all book sub-elements, regardless of their position in the document. |
bookstore//book | Select all book elements that belong to the descendants of the bookstore element, and no matter what position they are located below the bookstore. |
// @ lang | Select all of the property named lang. |
Predicate (Predicates)
Predicate is used to search for a particular node or contains a value specified node.
Predicate is embedded in square brackets in.
Examples
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 an element belonging to the first sub bookstore book element. |
/bookstore/book[last()] | Select the sub-elements belonging to the bookstore last book element. |
/bookstore/book[last()-1] | Select the sub-elements belonging to the reciprocal bookstore's second book element. |
/bookstore/book[position()<3] | Select book element belonging to two sub-elements of the bookstore element foremost. |
//title[@lang] | Select all of lang has a property named title element. |
// title [@ lang = 'What'] | Select all the title elements, and these elements have a lang attribute value of eng. |
/bookstore/book[price>35.00] | Select all the book elements bookstore element, and wherein the price element value must be greater than 35.00. |
/bookstore/book[price>35.00]/title | Select all the title elements of the book element bookstore element, and wherein the value of the price element must be greater than 35.00. |
Select the unknown node
XPath wildcards can be used to select unknown XML elements.
Tsuhaifu | description |
---|---|
* | Matches any element node. |
@* | Matches any attribute node. |
node() | Match any type of node. |
Examples
In the table below, we have listed some path expressions and the result of these expressions:
Path expression | result |
---|---|
/bookstore/* | Select the bookstore element of all child elements. |
//* | Select all elements in the document. |
//title[@*] | Select all the title elements with attributes. |
Select several paths
By using the path expression "|" operator, you can select several paths.
Examples
In the table below, we have listed some path expressions and the result 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 title and price elements in the document. |
/bookstore/book/title | //price | Select book element of the bookstore element belonging to all the title elements, as well as document all the price elements. |