#3.1 What XPath: Axes
XPath in Selenium: XPath in Selenium: Best Practices and Optimization
Table of contents
This table provides a detailed overview of XPath axes, their relationships, and how they can be combined. Use this as a reference to understand how to navigate XML/HTML document structures effectively.
Axis | Description | Relationship to Context Node | Inverse Axis | Node Test | Example |
self | Selects the context node | Is the context node | self | Any | self::div |
child | Selects all children of the context node | Direct children | parent | Element, Text, Comment, Processing Instruction | child::p |
parent | Selects the parent of the context node | Direct parent | child | Element | parent::div |
ancestor | Selects all ancestors of the context node | All nodes above in hierarchy | descendant | Element | ancestor::section |
ancestor-or-self | Selects the context node and all its ancestors | Self and all nodes above | descendant-or-self | Element | ancestor-or-self::div |
descendant | Selects all descendants of the context node | All nodes below in hierarchy | ancestor | Any | descendant::li |
descendant-or-self | Selects the context node and all its descendants | Self and all nodes below | ancestor-or-self | Any | descendant-or-self::p |
following-sibling | Selects all siblings after the context node | Same level, after | preceding-sibling | Element | following-sibling::div |
preceding-sibling | Selects all siblings before the context node | Same level, before | following-sibling | Element | preceding-sibling::h1 |
following | Selects everything after the closing tag of the context node | After in document order | preceding | Any | following::section |
preceding | Selects everything before the opening tag of the context node | Before in document order | following | Any | preceding::p |
attribute | Selects attributes of the context node | Attributes of the node | None | Attribute | attribute::href |
namespace | Selects namespace nodes of the context node | Namespace nodes | None | Namespace | namespace::* |
Additional Information:
Context Node: The node currently being processed or the node from which the path expression is anchored.
Document Order: The hierarchical order of elements as they appear in the XML document, from top to bottom.
Axis Specifiers: In XPath expressions, axis specifiers can be abbreviated:
child::
is the default axis (can be omitted)@
is short forattribute::
//
is short for/descendant-or-self::node()/
.
is short forself::node()
..
is short forparent::node()
Node Tests:
node()
: Matches any node of any typetext()
: Matches text nodescomment()
: Matches comment nodesprocessing-instruction()
: Matches processing instruction nodes
Predicate Usage: Predicates (filters) can be applied to any axis, e.g.,
ancestor::div[1]
selects the nearest ancestor that is adiv
.Axis Combinations: Axes can be combined in a single XPath expression, e.g.,
ancestor::div/following-sibling::p
selects allp
elements that are siblings of any ancestordiv
.Performance Considerations:
Forward axes (child, descendant, following, etc.) are generally faster than reverse axes (parent, ancestor, preceding, etc.).
descendant::
is usually slower thanchild::
because it searches deeper in the hierarchy.
Special Cases:
The
namespace
axis is not supported in all XPath processors and is less commonly used.The
attribute
axis cannot have children, descendants, or siblings.
This table and additional information provide a comprehensive overview of XPath axes and their relationships, suitable for both beginners and advanced users exploring XPath capabilities.