How to use XPath Axes in Xml

In this article I will explain how to use the axes in XPath.
  • 2261

Syntax of Axis

We can  use an axis by using it's name, along with a node test, and  one or more predicates which is optional. The axis and node test are separated by ::, and the predicates are enclosed within the square bracket [].

axisname::nodetest[predicate]

In this syntax the axisname denotes the name of axis, some of the axis name are:

  • ancestor  :  It is used to select the ancestor of the current node that means the parent node of the current node, and the parent node of the parent node that means the grandparent of current node.
  • ancestor-or-self  :  It can used to select the ancestor of the current node and the current node itself.
  • attribute  :  It is used to select all the attributes of the current node.
  • child  :  It is used to select all the children of the current node.
  • descendant  :  It is used to select the descendant of the current node that means the child node of the current node, and the child node of the child node that means the grandchild of current node.
  • descendant-or-self  :  It can used to select the descendant of the current node and the current node itself.
  • following  :  It is used to select all the nodes after the closing tag of the current node.
  • following-sibling  :  Used to select all the siblings after the current node.
  • namespace  :  It is used to select the namespace of the current node.
  • parent  :  Used to select the parent node of the current node.
  • preceding  :  It is used to selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes.
  • preceding-sibling  :  Used to select all the siblings before the current node.
  • self  :  It is used to select the current node.

Some of the examples of axis are:

  • child :: Trainee  :  Select all the Trainee nodes that are children of current node.
  • attribute :: street  :  Select all the street attributes of the current node.
  • child  :: *  :  Select all the child elements of the current node.
  • attributes  :: *  : Select all the attributes of the current node.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.