XPath is a language used to search for information in XML documents. XPath can be used to traverse elements and attributes in XML documents.
XPath is the main element of W3C XSLT standards, and XQuery and XPointer are also built on XPath expressions.
We recommend a pretty good site: there are good examples in the http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html, the red font in the following example represents the elements (or attributes) obtained using the corresponding syntax ).
In the previous article, I learned XPath and used path expressions to select nodes in the XML document. The slash "/" is selected from the root node, and the double slash "//" selects the nodes in the document from the current node that matches the selected node, regardless of their location, point ". "select the current node, two points ".. "select the parent node of the current node, and" @ "is the selection attribute.
Predicates are an important part of XPath. They are used to find a specific node or a node that contains a specified value. The predicates are embedded in square brackets.
1. Select the first BBB element syntax under AAA:/AAA/BBB [1]
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>
2. Select the last BBB element syntax under AAA:/AAA/BBB [last ()]
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>
3. Select the last and second BBB element syntax under AAA:/AAA/BBB [last ()-1]
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>
4. Select the first two BBB element syntaxes under AAA:/AAA/BBB [position () <3]
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>
5. Obtain the CCC element syntax with the s attribute under BBB under AAA:/AAA/BBB/CCC [@ s]
<AAA>
<BBB>
<CCC s = "1"/>
<CCC s = "2"/>
<CCC s = "3"/>
<CCC s = "4"/>
<CCC s = "5"/>
<CCC r = "a"/>
</BBB>
<CCC>
<BBB r = "a"/>
<BBB r = "B"/>
<BBB r = "c"/>
</CCC>
<BBB/>
<BBB/>
</AAA>
6. Select the book element of all the bookstore elements, and the value of the price element must be greater than 35.00. Syntax: bookstores/book [price> 35.00]
<Bookstore>
<Book>
<Title land = "eng"> Book1 </title>
<Price> 29.99 </tilte>
</Book>
<Book>
<Title land = "eng"> Book2 </title>
<Price> 39.55 </tilte>
</Book>
</Bookstore>
7. Select the title element of the book element in all bookstore elements, and the value of the price element must be greater than 35.00. Syntax:/bookstore/book [price> 35.00]/title
<Bookstore>
<Book>
<Title land = "eng"> Book1 </title>
<Price> 29.99 </tilte>
</Book>
<Book>
<Title land = "eng"> Book2 </title>
<Price> 39.55 </tilte>
</Book>
</Bookstore>
This article describes the XPath predicates. More complex predicates can be used with other XPath functions.