Skip to content

XPath Axes

How XPath axes like ancestor, descendant, child, and attribute help navigate XML trees by defining node relationships for precise path expressions.

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

List of various Axis Values

Axis Description
ancestor Represents the ancestors of the current node which include the parents up to the root node.
ancestor-or-self Represents the current node and it's ancestors.
attribute Represents the attributes of the current node.
child Represents the children of the current node.
descendant Represents the descendants of the current node. Descendants include the node's children upto the leaf node(no more children).
descendant-or-self Represents the current node and it's descendants.
following Represents all nodes that come after the current node.
following-sibling Represents the following siblings of the context node. Siblings are at the same level as the current node and share it's parent.
namespace Represents the namespace of the current node.
parent Represents the parent of the current node.
preceding Represents all nodes that come before the current node (i.e. before it's opening tag).
preceding-sibling Represents all siblings before the current node.
self Represents 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/...

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.

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:

Example Result
child::book Selects all book nodes that are children of the current node
attribute::lang Selects 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::book Selects all book descendants of the current node
ancestor::book Selects all book ancestors of the current node
ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is a book node
child::*/child::price Selects all price grandchildren of the current node
Navigation