XPath Axes

Example:

<?xml version="1.0" encoding="UTF-8"?>
 
<bookstore>
 
<book>
  <title lang="en">ABC</title>
  <price>100.00</price>
</book>
 
<book>
  <title lang="en">IT Basics</title>
  <price>300.00</price>
</book>
 
</bookstore>

XPath Axes:

The relationship to the context (current) node is represented by an axis. The path axes are used to locate nodes relative to that node on the tree.

AxisName Result
ancestor To select all the ancestors (parent, grandparent, etc.) of the current node.
ancestor-or-self To select all the ancestors (parent, grandparent, etc.) of the current node and the current node itself.
attribute To select all the attributes of the current node.
child To select all the children of the current node.
descendant To select all the descendants (children, grandchildren, etc.) of the current node.
descendant-or-self To select all the descendants (children, grandchildren, etc.) of the current node and the current node itself.
following To select everything in the document after the closing tag of the current node.
following-sibling To select all the siblings after the current node.
namespace To select all the namespace nodes of the current node.
parent To select all the parents of the current node.
preceding To select all the nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes.
preceding-sibling To select all the siblings before the current node.
self To select the current node.

Location Path Expression:

A location path can be either of the two types:

Absolute location path:

An absolute location path starts with a slash ( / ). It consists of one or more steps, each separated by a slash.

Syntax: 

/step/step/...

Relative location path:

A relative location path does not start with a slash ( / ). It also consists of one or more steps, each separated by a slash.

Syntax:

step/step/...

The evaluation of the steps is done against the nodes in the current node-set. The different parts of a step are:

  • To define the tree-relationship between the selected nodes and the current node: axis
  • To identify a node within an axis: node-test
  • To further refine the selected node-set: zero or more predicates

Syntax: Location step:

axisname::nodetest[predicate]

Examples:

Example Result
child::book To select all the book nodes that are children of the current node.
attribute::lang To select the lang attribute of the current node.
child::* To select all the element children of the current node.
attribute::* To select all the attributes of the current node.
child::text() To select all the text node children of the current node.
child::node() To select all the children of the current node.
descendant::book To select all the book descendants of the current node.
ancestor::book To select all the book ancestors of the current node.
ancestor-or-self::book To select all the book ancestors of the current node – and the current as well if it is a book node.
child::*/child::price To select all the price grandchildren of the current node.
Please Share