XPath Axes

Get a brief introduction about axes provided in XPath in this lesson.

What are axes?

Axis (or plural form “axes”) represents the relationship between the current context node and the target node that we want to locate in the DOM hierarchy. We have already discussed the document tree and family-like hierarchy between nodes in previous lessons.

In this lesson, we’ll learn about using XPath axes such as ancestor, descendant, parent, child, siblings, etc. while writing the expressions.

HTML structure considered for demonstration

Let us consider the following HTML structure to learn about the different axes offered in XPath:

<html>
<body>
    <div>
        <table id='operators'>
            <tr>
                <td><span id="1">1</span></td>
                <td><span id="2">2</span></td>
                <td><span id="3">3</span></td>
            </tr>
            <tr>
                <td><span id="4">4</span></td>
                <td><span id="5">5</span></td>
                <td><span id="6">6</span></td>
            </tr>
            <tr>
                <td><span id="7">7</span></td>
                <td><span id="8">8</span></td>
                <td><span id="10">10</span></td>
            </tr>
        </table>
    </div>
</body>
</html>

List of axes

The table below shows the list of axes that can be used in XPath:

Get hands-on with 1200+ tech skills courses.