Understanding XPath Axes: Navigating XML Document Relationships
Master XML navigation using XPath axes. This tutorial explains how XPath axes define node relationships (ancestor, descendant, following, preceding, etc.) relative to the context node, enabling you to create precise and efficient XPath expressions for selecting and manipulating specific nodes within complex XML structures.
Understanding XPath Axes
XPath axes define node relationships within an XML document, enabling you to select nodes based on their position relative to a specified node (the context node). This is an essential part of writing efficient and targeted XPath expressions.
XPath Axes: Navigating XML Relationships
XPath axes let you select nodes based on their relationship to the context node. The context node is the node currently being processed by the XPath expression.
Axis | Description |
---|---|
ancestor |
Selects all ancestor nodes (parent, grandparent, etc., up to the root). |
ancestor-or-self |
Selects the context node and all its ancestor nodes. |
attribute |
Selects attributes of the context node. |
child |
Selects all children of the context node. |
descendant |
Selects all descendant nodes (children, grandchildren, etc.). |
descendant-or-self |
Selects the context node and all its descendant nodes. |
following |
Selects all nodes following the context node in document order. |
following-sibling |
Selects all following sibling nodes of the context node. |
namespace |
Selects namespaces declared in scope of the context node. |
parent |
Selects the parent node of the context node. |
preceding |
Selects all nodes preceding the context node in document order. |
preceding-sibling |
Selects preceding sibling nodes of the context node. |
self |
Selects the context node itself. |
Example: Using XPath Axes
(Note: This example requires an XML file named `employee.xml` and an XSLT stylesheet named `employee.xsl`. Screenshots from the original text are not included here. Please refer to the original document for visual verification of the XPath expressions' results and the XSLT output. The descriptions below aim to convey the information present in those screenshots.)
This example uses XPath axes to select and display data from an XML file. The XML data would be in an `employee.xml` file, and an XSLT stylesheet `employee.xsl` would be used to transform the XML based on XPath expressions.
`employee.xml` (Example XML Data)
<employees>
<employee>
<firstName>Abhiram</firstName>
<lastName>Kushwaha</lastName>
<nickName>Manoj</nickName>
<salary>15000</salary>
</employee>
<!-- ... more employee elements ... -->
</employees>
`employee.xsl` (Example XSLT Stylesheet)
(The original content includes the content of the `employee.xsl` file. Since we cannot display the file here, please refer to the original document for the file content.)
(The original content includes a screenshot showing the output. Since we cannot display images, please refer to the original document for the visual representation of the output. The description below aims to convey the information in the screenshot.)
The output shows the first employee's information, demonstrating how XPath axes can target specific nodes based on their relationship to other nodes.