XPath Axes

Navigate XML document relationships with axes

🧭 What are XPath Axes?

XPath axes define the relationship between nodes in an XML tree. They allow you to navigate from a current node to related nodes like parents, children, siblings, and ancestors.


<!-- Example XML -->
<library>
    <book>
        <title>XML Guide</title>
        <author>John Doe</author>
    </book>
</library>

<!-- XPath with axis -->
//book/child::title
                                    

Result:

Selects: <title>XML Guide</title>

Common XPath Axes

👶

child

Selects all children of current node

book/child::title
👴

parent

Selects the parent of current node

title/parent::book
👥

sibling

Selects siblings of current node

title/following-sibling::author
🌳

descendant

Selects all descendants

library/descendant::title

🔹 Child Axis

The child axis selects all direct children of the current node. It's the most commonly used axis and can be abbreviated with a forward slash.

<!-- Sample XML -->
<bookstore>
    <book>
        <title>Learning XML</title>
        <price>29.99</price>
    </book>
</bookstore>

<!-- Full syntax -->
/bookstore/child::book/child::title

<!-- Abbreviated syntax -->
/bookstore/book/title

Result:

Selects: <title>Learning XML</title>

🔹 Parent Axis

The parent axis selects the parent node of the current node. It moves up one level in the XML tree hierarchy.

<!-- Sample XML -->
<catalog>
    <product>
        <name>Laptop</name>
    </product>
</catalog>

<!-- Select parent of name -->
//name/parent::product

<!-- Abbreviated with .. -->
//name/..

Result:

Selects: <product>...</product>

🔹 Ancestor and Descendant Axes

Ancestor axis selects all ancestors (parent, grandparent, etc.) while descendant axis selects all descendants (children, grandchildren, etc.) of the current node.

<!-- Sample XML -->
<company>
    <department>
        <team>
            <employee>Alice</employee>
        </team>
    </department>
</company>

<!-- Select all ancestors of employee -->
//employee/ancestor::*

<!-- Select all descendants of company -->
//company/descendant::*

Result:

Ancestors: team, department, company

Descendants: department, team, employee

🔹 Sibling Axes

Following-sibling selects all siblings after the current node, while preceding-sibling selects all siblings before the current node at the same level.

<!-- Sample XML -->
<menu>
    <item>Home</item>
    <item>About</item>
    <item>Contact</item>
</menu>

<!-- Select siblings after "About" -->
//item[text()='About']/following-sibling::item

<!-- Select siblings before "About" -->
//item[text()='About']/preceding-sibling::item

Result:

Following: <item>Contact</item>

Preceding: <item>Home</item>

🔹 Attribute Axis

The attribute axis selects all attributes of the current node. Attributes contain additional information about elements.

<!-- Sample XML -->
<book id="101" lang="en">
    <title>XML Basics</title>
</book>

<!-- Select all attributes -->
//book/attribute::*

<!-- Select specific attribute (abbreviated) -->
//book/@id

Result:

All attributes: id="101", lang="en"

Specific: id="101"

🧠 Test Your Knowledge

Which axis selects all children of the current node?