XPath Axes (Assen)

XML example document

We will use this XML document in the following example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>

  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>

XPath axis

Axes can define a node set relative to the current node. Result
Axis name ancestor
Select all ancestors (parent, grandfather, etc.) of the current node. ancestor-or-self
Select all ancestors (parent, grandfather, etc.) of the current node and the current node itself. Select all attributes of the current node.
attribute Select all child elements of the current node.
child descendant
Select all descendant elements (children, grandchildren, etc.) of the current node. descendant-or-self
Select all descendant elements (children, grandchildren, etc.) of the current node and the current node itself. following
Select all nodes after the end tag of the current node in the document. namespace
Select all namespace nodes of the current node. parent
Select the parent node of the current node. preceding
Select all nodes before the start tag of the current node in the document. preceding-sibling
Select all preceding sibling nodes of the current node. self

Select the current node.

Position path expression

The position path can be absolute or relative.

Absolute position path: The absolute path starts with a forward slash (/), while the relative path does not. In both cases, the position path includes one or more steps, each separated by a slash:

/step/step/...

Relative position path:

step/step/...

Each step is calculated based on the nodes in the current node set.

Step (step) includes:

Axis (axis)
Define the tree relationship between the selected node and the current node
Node test (node-test)
Identify nodes within a certain axis
Zero or more predicates (predicate)
Further refine the selected node set

Step syntax:

Axis name::node test[predicate]

Instance

Example Result
child::book Select all book nodes that belong to the child elements of the current node.
attribute::lang Select the lang attribute of the current node.
child::* Select all child elements of the current node.
attribute::* Select all attributes of the current node.
child::text() Select all text child nodes of the current node.
child::node() Select all child nodes of the current node.
descendant::book Selecteer alle book afstammelingen van het huidige knooppunt.
ancestor::book Selecteer alle book voorouders van het huidige knooppunt.
ancestor-or-self::book Selecteer alle book voorouders van het huidige knooppunt en het huidige knooppunt (als dit knooppunt een book knooppunt is)
child::*/child::price Selecteer alle price nakomelingen van de huidige knooppunt.