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.

AxisNameResult
ancestorTo select all the ancestors (parent, grandparent, etc.) of the current node.
ancestor-or-selfTo select all the ancestors (parent, grandparent, etc.) of the current node and the current node itself.
attributeTo select all the attributes of the current node.
childTo select all the children of the current node.
descendantTo select all the descendants (children, grandchildren, etc.) of the current node.
descendant-or-selfTo select all the descendants (children, grandchildren, etc.) of the current node and the current node itself.
followingTo select everything in the document after the closing tag of the current node.
following-siblingTo select all the siblings after the current node.
namespaceTo select all the namespace nodes of the current node.
parentTo select all the parents of the current node.
precedingTo select all the nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes.
preceding-siblingTo select all the siblings before the current node.
selfTo 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:

ExampleResult
child::bookTo select all the book nodes that are children of the current node.
attribute::langTo 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::bookTo select all the book descendants of the current node.
ancestor::bookTo select all the book ancestors of the current node.
ancestor-or-self::bookTo select all the book ancestors of the current node – and the current as well if it is a book node.
child::*/child::priceTo select all the price grandchildren of the current node.
Please Share