XPath study notes (b)

XPath syntax:

XPath path expressions used to select XML node or group of nodes.

1, example:
<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book>
  <title lang="en">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="en">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore> 

 

2, select the node:

The following table lists the most useful path expressions:

expression description
Node name Select all nodes having the node name is
/ Select from the root (absolute path)
// From the current node selection regardless of where
. Selecting the current node
.. Selecting a parent node
@ Select Properties

Applied to the previous example, the following results:

Path expression result
bookstore Select all nodes named "bookstore" of
/bookstore Select the root element bookstore
bookstore/book Select all book nodes from child nodes of the bookstore
//book Select all book nodes from the text regardless of where
bookstore//book Select all nodes in the book in subsequent bookstore
// @ lang Select all "lang" attribute in the text

 

3, predicate (Predicates):

The predicate is used to query a particular node or a node having a particular value.

Predicate always framed by a pair of square brackets.

Following table:

Path Expression Result
/bookstore/book[1] Select a bookstore first sub-node named book
/bookstore/book[last()] Bookstore select last child node named book
/bookstore/book[last()-1] Select bookstore penultimate sub-node named book
/bookstore/book[position()<3] Bookstore book select the first two child nodes
//title[@lang] Select all nodes having title "lang" attribute
// title [@ lang = 'and'] Select all nodes having title "lang" attribute and is 'en' is
/bookstore/book[price>35.00] Select bookstore child node in price> book node 35.00
/bookstore/book[price>35.00]/title Select the sub-node title of the bookstore price> book node 35.00

 

4, select the unknown node:
Tsuhaifu description
* Matches any element node
@* Attribute node matches any
node() Matches any node of any type

As follows:

Path expression result
/bookstore/* Select all the child nodes bookstore
//* Select all of the text elements in nodes
//title[@*] Select an element having a title attribute

 

5, a plurality of selected paths:

Using | operator to multi-path selection.

As follows:

Path expression result
//book/title | //book/price Select all book nodes in the title and price node
//title | //price Selected title and price text node
/bookstore/book/title | //price Select the title elements of the book element of the bookstore and all price elements

 

 

Reference: https://www.w3schools.com/xml/xpath_syntax.asp

Guess you like

Origin www.cnblogs.com/hilovexy-blog/p/11258400.html