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.
The ancestor axis contains the ancestor nodes of the upper and lower nodes, which are composed of the parent nodes of the context nodes and the parent nodes of the parent nodes, therefore, the ancestor axis always contains the root node, unless the context node is the root node itself.
/AAA/BBB/DDD/CCC/EEE/ancestor: * select all nodes in an absolute path.
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
<CCC>
<DDD>
<EEE>
<DDD>
<FFF/>
</DDD>
</EEE>
</DDD>
</CCC>
</AAA>
// FFF/ancestor: * select all ancestor nodes of the FFF element.
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
<CCC>
<DDD>
<EEE>
<DDD>
<FFF/>
</DDD>
</EEE>
</DDD>
</CCC>
</AAA>