Simply put, xpath is the method of selecting nodes in XML files.
The so-called node is the smallest constituent unit of an XML file, divided into 7 types in total.
- element (element node)
- attribute (attribute node)
- text (text node)
- namespace (namespace node)
- processing-instruction (processing command node)
- comment (Comment Node)
- root (root node)
xpath can be used to select these 7 nodes. However, the following notes only involve the first element (element node), which is most commonly used, so the nodes and elements below can be regarded as synonyms.
1. The basic format of xpath expression
xpath selects nodes through "Path Expression". In form, "path expression" is very similar to traditional file systems.
# Slash (/) is used as a splitter inside the path.
# There are two ways to write absolute paths and relative paths for the same node.
# The absolute path must start with "/" and be followed by the root node, such as /step/step/...
# Relative path is a way to write other than absolute paths, such as step/step, which means that you do not use "/".
# "." means the current node.
# ".." means the parent node of the current node
2. Basic rules for selecting nodes
- nodename: indicates all children of the node that are selected
- "/": means to select the root node
- "//": means selecting a node at any location
- "@": means to select a certain attribute
3. Select an instance of a node
Let's look at an XML instance document first.
<?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>
[Example 1]
bookstore: Select all children of the bookstore element.
[Example 2]
/bookstore: Select the root node bookstore, this is the absolute path writing method.
[Example 3]
bookstore/book: Select book elements that belong to bookstore's child elements. This is the relative path writing method.
[Example 4]
//book: Select all book child elements regardless of their location in the document.
[Example 5]
bookstore//book: Select all book elements that belong to descendants of the bookstore element, regardless of where they are located under the bookstore.
[Example 6]
//@lang: Select all attributes named lang.
4. Predicate condition of xpath (Predicate)
The so-called "predicate condition" is an additional condition to the path expression.
All conditions are written in square brackets "[]", indicating further filtering of nodes.
[Example 7]
/bookstore/book[1]: Indicates the first book child element of the bookstore selected.
[Example 8]
/bookstore/book[last()]: indicates the last book child element of the bookstore selected.
[Example 9]
/bookstore/book[last()-1]: indicates the second-last book child element of the bookstore selected.
[Example 10]
/bookstore/book[position()<3]: indicates that the first two book child elements of the bookstore are selected.
[Example 11]
//title[@lang]: means selecting all title nodes with lang attributes.
[Example 12]
//title[@lang='eng']: indicates that the title node with the value of all lang attributes equal to "eng".
[Example 13]
/bookstore/book[price]: means that the book child element of the bookstore is selected, and the selected book element must have a price child element.
[Example 14]
/bookstore/book[price>35.00]: indicates that the book child element of the bookstore is selected, and the price child element value of the selected book element must be greater than 35.
[Example 15]
/bookstore/book[price>35.00]/title: In the result set of Example 14, select the title child element.
[Example 16]
/bookstore/book/price[.>35.00]: indicates the price child element of "/bookstore/book" with a value greater than 35.
5. Wild Card
# "*" means matching any element node.
# "@*" means matching any attribute value.
# node() means matching any type of node.
[Example 17]
//*: Select all element nodes in the document.
[Example 18]
/*/*: Indicates that all element nodes of the second layer are selected.
[Example 19]
/bookstore/*: indicates that all element children of the bookstore are selected.
[Example 20]
//title[@*]: means selecting all title elements with attributes.
6. Select multiple paths
Use "|" to select multiple parallel paths.
[Example 21]
//book/title | //book/price: indicates that the title child element and price child element of the book element are selected at the same time.