xml
  1. xml-xpath-axes

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:
  1. Ancestor Axes
  2. Descendant Axes
  3. Parent Axes
  4. Child Axes
  5. Following Axes
  6. Following-Sibling Axes
  7. Preceding Axes
  8. Preceding-Sibling Axes
  9. Self Axes

Syntax

The syntax to use the XPath Axes is as follows:

AxisName::NodeTest

Where,

  • AxisName is the type of axis being used
  • NodeTest 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 element
  • descendant::employee - selects all descendants of the employee element
  • parent::name - selects the parent of the name element
  • child::department - selects all children of the department element
  • following::employee - selects all following siblings of the employee element
  • following-sibling::employee - selects all following siblings of the employee element
  • preceding::employee - selects all preceding siblings of the employee element
  • preceding-sibling::employee - selects all preceding siblings of the employee element
  • self::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 ancestors
  • descendant::employee - returns both employee elements and their children
  • parent::name - returns the employee element
  • child::department - returns the department element
  • following::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 siblings
  • preceding-sibling::employee - returns no nodes as the employee element with id="1" has no preceding siblings
  • self::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.

Published on: