Xpath Axes in XML
XPath Axes in XML
What are XPath Axes?
XPath Axes define the node-set relative to the current node in an XML document.
They let you navigate around nodes beyond just children or descendants.
Useful to select parents, siblings, ancestors, following nodes, etc.
Common XPath Axes
| Axis Name | Description | Example XPath Expression |
|---|---|---|
child | Selects children of the current node (default axis) | child::book or simply book |
parent | Selects the parent of the current node | parent::bookstore |
ancestor | Selects all ancestors (parent, grandparent, etc.) | ancestor::bookstore |
descendant | Selects all descendants (children, grandchildren, etc.) | descendant::title |
following-sibling | Selects siblings after the current node | following-sibling::book |
preceding-sibling | Selects siblings before the current node | preceding-sibling::book |
following | Selects everything in the document after the current node | following::title |
preceding | Selects everything in the document before the current node | preceding::book |
self | Selects the current node | self::node() |
descendant-or-self | Selects current node and all descendants | descendant-or-self::book |
ancestor-or-self | Selects current node and all ancestors | ancestor-or-self::bookstore |
Example XML
<bookstore> <book id="b1"> <title>Book One</title> <author>Author A</author> </book> <book id="b2"> <title>Book Two</title> <author>Author B</author> </book></bookstore>Example Usage
Select the parent of
<title>node:/bookstore/book/title/parent::bookSelect all ancestor nodes of
<title>:/bookstore/book/title/ancestor::bookstoreSelect following sibling books after the first book:
/bookstore/book[1]/following-sibling::bookSelect preceding sibling of the second book:
/bookstore/book[2]/preceding-sibling::book
Summary
XPath axes let you navigate XML nodes in various directions.
Useful for complex XML queries beyond simple child or descendant selections.
Axes have descriptive names like
parent,ancestor,following-sibling, etc.
If you want, I can help you write XPath queries using axes for your XML!