Skip to main content

XPath Axes

An axis represents a relationship to the context (current) node and it is used to locate nodes related to that node on the tree.

note

Axes are so named because they refer to the axis on which the elements related to an element are located.

List of various Axis Values

AxisDescription
ancestorRepresents the ancestors of the current node which include the parents up to the root node.
ancestor-or-selfRepresents the current node and it's ancestors.
attributeRepresents the attributes of the current node.
childRepresents the children of the current node.
descendantRepresents the descendants of the current node. Descendants include the node's children upto the leaf node(no more children).
descendant-or-selfRepresents the current node and it's descendants.
followingRepresents all nodes that come after the current node.
following-siblingRepresents the following siblings of the context node. Siblings are at the same level as the current node and share it's parent.
namespaceRepresents the namespace of the current node.
parentRepresents the parent of the current node.
precedingRepresents all nodes that come before the current node (i.e. before it's opening tag).
preceding-siblingRepresents all siblings before the current node.
selfRepresents the current node.

Axis and Location Path Expression

As we explained in previous chapter XPath Paths, a location path can be absolute or relative.

An absolute location path starts with a slash ( / ) and a relative location path does not.

In both cases the location path consists of one or more steps, each separated by a slash:

  • Absolute path: /step/step/...
  • Relative path step/step/...
note

Each step is evaluated against the nodes in the current node-set.

Each step must follow this syntax:

axisname::nodetest[predicate]

where:

  • axisname is an axis that defines the tree-relationship between the selected nodes and the current node.
  • nodetest identifies a node within an axis.
  • predicatezero or more predicates to further refine the set of selected nodes.
note

You can learn more about predicates in XPath Predicate Chapter.

Example

Consider the following XML document:

<?xml version="1.0"  encoding="UTF-8"?>

<bookstore>
<book>
<title lang="en">A Great Book</title>
<author>Tom Nolan</author>
<price>19.99</price>
</book>

<book>
<title lang="en">Learning XML</title>
<author>Tutorial Reference</author>
<price>29.99</price>
</book>
</bookstore>

In the following table there are some examples of selection using axes:

ExampleResult
child::bookSelects all book nodes that are children of the current node
attribute::langSelects the lang attribute of the current node
child::*Selects all element children of the current node
attribute::*Selects all attributes of the current node
child::text()Selects all text node children of the current node
child::node()Selects all children of the current node
descendant::bookSelects all book descendants of the current node
ancestor::bookSelects all book ancestors of the current node
ancestor-or-self::bookSelects all book ancestors of the current node - and the current as well if it is a book node
child::*/child::priceSelects all price grandchildren of the current node
note

Learn more about *, text(), node() and more in XPath Operators Chapter.