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 ).
The XPath axis (XPath Axes) defines a node set relative to the current node:
1. child Selects all child elements of the current node
2. parent select the parent node of the current node
3. descendant Selects all descendant elements (child, sun, etc.) of the current node)
4. ancestor Selects all the ancestors of the current node (parent, grandfather, etc)
5. descendant-or-self Selects all descendant elements (child, sun, etc.) of the current node and the current node itself.
6. ancestor-or-self Selects all the ancestors of the current node (parent, grandfather, etc.) and the current node itself
7. preceding-sibling: select all peer nodes before the current node
8. following-sibling Selects all peer nodes after the current node
9. preceding Selects all nodes before the start label of the current node in the document.
10. following select all nodes after the end label of the current node in the document
11. Select the current node by self
12. attribute Selects all attributes of the current node.
13. Select All namespace nodes of the current node.
Descendant Selects all descendant elements (including child nodes and child nodes…) of the current node ...), The descendant (descendant) axis contains the descendant of the context node. A descendant is a subnode or a subnode of the subnode.
/Descendant: * select all descendants of the root element of the document. That is, all elements are selected.
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
<CCC>
<DDD>
<EEE>
<DDD>
<FFF/>
</DDD>
</EEE>
</DDD>
</CCC>
</AAA>
/AAA/BBB/descendant: * select all descendant elements of/AAA/BBB
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
<CCC>
<DDD>
<EEE>
<DDD>
<FFF/>
</DDD>
</EEE>
</DDD>
</CCC>
</AAA>
// CCC/descendant: * select all descendant elements of all CCC elements.
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
<CCC>
<DDD>
<EEE>
<DDD>
<FFF/>
</DDD>
</EEE>
</DDD>
</CCC>
</AAA>
// CCC/descendant: select all the DDD elements whose CCC is the ancestor element.
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
<CCC>
<DDD>
<EEE>
<DDD>
<FFF/>
</DDD>
</EEE>
</DDD>
</CCC>
</AAA>