XPath Axes

Welcome to The Coding College! This tutorial covers XPath Axes, one of the most powerful features of XPath that allows you to traverse and select nodes in an XML document relative to the current node. XPath axes are essential for navigating complex XML structures with precision and flexibility.

What Are XPath Axes?

An XPath axis defines a relationship between the current (context) node and the nodes you want to select. Using axes, you can move through the XML document in multiple directions—up, down, and sideways—based on hierarchical relationships.

List of XPath Axes

XPath defines 13 axes. Here’s an overview:

AxisDescription
childSelects all direct child nodes of the current node (default axis).
descendantSelects all descendant nodes of the current node (children, grandchildren, etc.).
parentSelects the parent of the current node.
ancestorSelects all ancestors (parent, grandparent, etc.) of the current node.
following-siblingSelects all sibling nodes that appear after the current node.
preceding-siblingSelects all sibling nodes that appear before the current node.
followingSelects everything in the document after the current node.
precedingSelects everything in the document before the current node.
selfSelects the current node.
descendant-or-selfSelects the current node and all its descendants.
ancestor-or-selfSelects the current node and all its ancestors.
attributeSelects all attributes of the current node.
namespaceSelects all namespace nodes of the current node.

Example XML Document

We will use the following XML document to explore XPath axes:

<library>
  <book id="1" genre="Programming">
    <title>XML Basics</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book id="2" genre="Programming">
    <title>Learn JavaScript</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</library>

XPath Axes Examples

1. Child Axis (child::)

Selects all direct child nodes of the current node.

XPath Expression:

/library/child::book

Result:
Selects all <book> elements under <library>:

<book id="1" genre="Programming">
  ...
</book>
<book id="2" genre="Programming">
  ...
</book>

2. Descendant Axis (descendant::)

Selects all descendant nodes of the current node, including children, grandchildren, etc.

XPath Expression:

/library/descendant::title

Result:
Selects all <title> elements:

<title>XML Basics</title>
<title>Learn JavaScript</title>

3. Parent Axis (parent::)

Selects the parent of the current node.

XPath Expression:

//title/parent::book

Result:
Selects the <book> element that is the parent of the <title>:

<book id="1" genre="Programming">
  ...
</book>
<book id="2" genre="Programming">
  ...
</book>

4. Ancestor Axis (ancestor::)

Selects all ancestors of the current node, including parent, grandparent, etc.

XPath Expression:

//price/ancestor::library

Result:
Selects the <library> element, which is an ancestor of <price>:

<library>
  ...
</library>

5. Following-Sibling Axis (following-sibling::)

Selects all sibling nodes that come after the current node.

XPath Expression:

//title/following-sibling::author

Result:
Selects the <author> element that comes after <title>:

<author>John Doe</author>
<author>Jane Smith</author>

6. Preceding-Sibling Axis (preceding-sibling::)

Selects all sibling nodes that come before the current node.

XPath Expression:

//author/preceding-sibling::title

Result:
Selects the <title> element that comes before <author>:

<title>XML Basics</title>
<title>Learn JavaScript</title>

7. Following Axis (following::)

Selects everything in the document after the current node.

XPath Expression:

//title[1]/following::price

Result:
Selects all <price> elements that appear after the first <title>:

<price>29.99</price>
<price>39.99</price>

8. Preceding Axis (preceding::)

Selects everything in the document before the current node.

XPath Expression:

//price[2]/preceding::title

Result:
Selects all <title> elements that appear before the second <price>:

<title>XML Basics</title>
<title>Learn JavaScript</title>

9. Self Axis (self::)

Selects the current node itself.

XPath Expression:

//title/self::title

Result:
Selects all <title> elements:

<title>XML Basics</title>
<title>Learn JavaScript</title>

10. Descendant-or-Self Axis (descendant-or-self::)

Selects the current node and all its descendant nodes.

XPath Expression:

/library/descendant-or-self::book

Result:
Selects all <book> elements and their descendants:

<book id="1" genre="Programming">
  ...
</book>
<book id="2" genre="Programming">
  ...
</book>

11. Ancestor-or-Self Axis (ancestor-or-self::)

Selects the current node and all its ancestor nodes.

XPath Expression:

//price/ancestor-or-self::library

Result:
Selects the <library> node and its ancestors:

<library>
  ...
</library>

12. Attribute Axis (@)

Selects attributes of the current node.

XPath Expression:

//book/attribute::id

Result:
Selects the id attribute of each <book>:

1  
2

13. Namespace Axis (namespace::)

Selects all namespace nodes of the current node. This axis is rarely used but can be useful in XML documents with namespaces.

Practical Use Case

Example XML:

<catalog>
  <product id="1" category="Electronics">
    <name>Smartphone</name>
    <price>699</price>
  </product>
  <product id="2" category="Appliances">
    <name>Blender</name>
    <price>99</price>
  </product>
</catalog>

XPath Expression to Select the Product Name with id="2":

/catalog/product[@id="2"]/child::name

Result:

<name>Blender</name>

Conclusion

XPath axes give you unparalleled flexibility to traverse and query XML documents. By mastering axes like child, descendant, ancestor, and others, you can create efficient queries to extract exactly what you need.

Leave a Comment