XPath Axes and Operators
XPath Axes are pre-defined location paths that are used to select nodes based on their relative position to other nodes in the XML document.
- There are several types of XPath Axes that are commonly used, including:
- Ancestor Axes
- Descendant Axes
- Parent Axes
- Child Axes
- Following Axes
- Following-Sibling Axes
- Preceding Axes
- Preceding-Sibling Axes
- Self Axes
Syntax
The syntax to use the XPath Axes is as follows:
AxisName::NodeTest
Where,
AxisName
is the type of axis being usedNodeTest
is the type of node being selected
Example
Consider the following XML document:
<root>
<employee id="1">
<name>John Doe</name>
<department>IT</department>
</employee>
<employee id="2">
<name>Jane Smith</name>
<department>HR</department>
</employee>
</root>
The following XPaths demonstrate the use of different XPath Axes:
ancestor::department
- selects all ancestors of the department elementdescendant::employee
- selects all descendants of the employee elementparent::name
- selects the parent of the name elementchild::department
- selects all children of the department elementfollowing::employee
- selects all following siblings of the employee elementfollowing-sibling::employee
- selects all following siblings of the employee elementpreceding::employee
- selects all preceding siblings of the employee elementpreceding-sibling::employee
- selects all preceding siblings of the employee elementself::employee
- selects the employee element itself
Output
The output of the above XPaths is as follows:
ancestor::department
- returns nothing as department does not have any ancestorsdescendant::employee
- returns both employee elements and their childrenparent::name
- returns the employee elementchild::department
- returns the department elementfollowing::employee
- returns the employee element with id="2"following-sibling::employee
- returns the employee element with id="2"preceding::employee
- returns no nodes as the employee element with id="1" has no preceding siblingspreceding-sibling::employee
- returns no nodes as the employee element with id="1" has no preceding siblingsself::employee
- returns both employee elements
Explanation
The different XPath Axes allow us to traverse the XML document and select specific nodes based on their relationships with other nodes. An axis specifies a direction in the document, such as up or down, and a node test specifies the type of node we want to select.
Use
XPath Axes are used extensively in XSLT transformations, as well as in XML and HTML parsing applications. They are an important tool for selecting and manipulating elements in an XML document.
Important Points
- XPath Axes allow us to navigate an XML document based on the relationships between nodes
- There are nine types of XPath Axes, including ancestor, descendant, parent, child, following, following-sibling, preceding, preceding-sibling and self
- XPath Axes are used to select specific nodes based on their relationships to other nodes
- XPath Axes are an important tool for parsing and transforming XML documents
Summary
XPath Axes and Operators are essential tools for navigating and selecting nodes from an XML document. Using XPath Axes allows you to select nodes based on their relationships with other nodes, providing an easy way to traverse and extract information from an XML document.