XML technologies / XPath / XPath 2.0: language reference / Path expressions: axes

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, chapter 4.)

Navigation through a document tree follows axes. Each step of a path expression can select only a subset of the nodes on its specified axis.

XPath and XQuery support different numbers of axes. XQuery supports only some XPath axes; among reverse axes, only parent is available. In particular, the namespace axis is not supported in XQuery. XPath support depends on the version: XPath 1.0 fully supports the namespace axis; XPath 2.0 deprecates it (deprecated), although applications may retain support.

Axis directions

Axes are classified as forward or reverse. Those proceeding from the context node in document order are forward (forward axes); those proceeding against document order are reverse (reverse axes). An axis’s orientation is called its principal direction.

Axis direction affects the context position assigned to an item in the sequence selected by the axis step.

Principal direction and sequence order (item numbering):
A sequence on a forward axis numbers its nodes in document order; otherwise, numbering is reversed.
As usual in XPath, the first item has position 1. If the document node is included, it is first for a forward axis and last for a reverse axis.

Forward axes

An axis containing only the context node, or the context node and nodes following it in document order, is forward.

The following axes are forward:

  • Self axis

  • Child axis

  • Descendant axis

  • Descendant-or-self axis

  • Following axis

  • Following-sibling axis

These axes primarily contain element nodes, their principal node kind, but also comment nodes or processing instructions as well as text nodes in the document.

The preceding axes cannot access attribute nodes or namespace nodes. Both node kinds have their own axes, where they are the principal node kind.

Both axes are also forward:

  • Attribute axis

  • Namespace axis

Reverse axes

An axis containing nodes preceding the context node in document order, possibly including the context itself, is reverse. Recall that self, containing only the context node, is forward. Reverse axes’ principal direction is opposite to document order.

Reverse axes include:

  • Parent axis

  • Ancestor axis

  • Preceding-sibling axis

  • Preceding axis

  • Ancestor-or-self axis

Complete coverage of the document tree

The hierarchical tree is fully covered by forward axes self, descendant and following plus reverse axes ancestor and preceding. These five axes do not overlap and together contain every node, excluding attributes and namespaces outside the hierarchy.

Here and in subsequent diagrams, the document node’s special role is shown by a dashed circle. The arbitrarily chosen context node has a light-gray background.

Complete document-tree coverage using five axes

Figure: complete document-tree coverage using five axes.

   

   

back to top