Simply put, XPath is the way to select nodes in an XML file.
The so-called node, which is the smallest constituent unit of an XML file, is divided into 7 types.
-Element (Elements node)
-Attribute (attribute node)
-Text (textual node)
-Namespace (Namespace node)
-processing-instruction (Processing command node)
-Comment (note node)
-Root (Root node)
XPath can be used to select these 7 types of nodes. However, the following notes refer only to the first element, which is most commonly used, so you can treat the nodes and elements below as synonyms.
First, the basic format of the XPath expression
XPath selects the node through the path expression. In form, "path expressions" are very similar to traditional file systems.
# slash (/) as a separator inside the path.
# The same node has the absolute path and the relative path two kinds of wording.
# Absolute path (absolute path) must be preceded by "/", followed by the root node, such as/step/step/....
# The relative path (relative path) is written in addition to the absolute path, such as Step/step, which means "/" is not used.
# "." Represents the current node.
# ".." Represents the parent node of the current node
Ii. basic rules for selecting nodes
-NodeName (node name): Indicates that all child nodes of the node are selected
-"/": Indicates select root node
-"//": Indicates that a node is selected anywhere
-"@": Indicates the selection of a property
Iii. examples of selecting nodes
Let's look at an XML instance document:
<?XML version= "1.0" encoding= "Iso-8859-1"?><Bookstore> < Book> <titleLang= "Eng">Harry Potter</title> < Price>29.99</ Price> </ Book> < Book> <titleLang= "Eng">Learning XML</title> < Price>39.95</ Price> </ Book></Bookstore>
[Example 1]
Bookstore: Selects all child nodes of the bookstore element.
[Example 2]
/bookstore: Select the root node bookstore, which is the absolute path notation.
[Example 3]
Bookstore/book: Selects all book elements that belong to the bookstore child element, which is the relative path notation.
[Example 4]
Book: Selects all book child elements, regardless of their position in the document.
[Example 5]
Bookstore//book: Selects all book elements that belong to descendants of the bookstore element, regardless of where they are located under bookstore.
[Example 6]
@lang: Select All properties named Lang.
IV. predicate conditions for XPath (predicate)
The so-called "predicate condition" is an additional condition on the path expression.
All the conditions are written in square brackets "[]", which means further filtering of the nodes.
[Example 7]
/BOOKSTORE/BOOK[1]: Represents the first book child element of the selection bookstore.
[Example 8]
/bookstore/book[last ()]: Indicates the last book child element of the selection bookstore.
[Example 9]
/bookstore/book[last ()-1]: Represents the second-to-last book child element of the selection bookstore.
[Example 10]
/bookstore/book[position () <3]: Represents the first two book child elements of the selection bookstore.
[Example 11]
title[@lang]: Indicates that all the title nodes with the lang attribute are selected.
[Example 12]
title[@lang = ' eng ']: Indicates that the value of the Select all lang attribute equals the title node of "Eng".
[Example 13]
/bookstore/book[price]: Represents the book child element that selects Bookstore, and the selected book element must have the price child element.
[Example 14]
/BOOKSTORE/BOOK[PRICE>35.00]: Represents the book child element that selects Bookstore, and the price child element value of the selected book element must be greater than 35.
[Example 15]
/bookstore/book[price>35.00]/title: Indicates in example 14 result set, select Title Child element.
[Example 16]
/BOOKSTORE/BOOK/PRICE[.>35.00]: Represents the price child element that selects "/bookstore/book" with a value greater than 35.
Five, wildcard characters
# "*" means matching any element node.
# "@*" means matching any property value.
# node () indicates that any type of node is matched.
[Example 17]
*: Selects all ELEMENT nodes in the document.
[Example 18]
/*/*: Represents the selection of all second-level element nodes.
[Example 19]
/bookstore/*: Represents the selection of all element child nodes of the bookstore.
[Example 20]
Title[@*]: Indicates that all title elements with attributes are selected.
Six, select multiple paths
with "|" Select multiple parallel paths.
[Example 21]
Book/title | Book/price: Indicates that both the title child element and the price child element of the book element are selected.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Note: This article is reproduced in Nanyi's blog (http://www.ruanyifeng.com/blog/2009/07/xpath_path_expressions.html), thanks to the original author!
XPath path expression note (reprint)