Updated April 13, 2023
Introduction to XPath Axes
XPath Axes is defined as the location path which accesses current node context (Starting point of XPath) to search for the different nodes in the XML document. These axes identify elements with the help of elements like a child, ancestors. To make simple, Path axes intimate the XPath processor about the direction to navigate around the tree of nodes in the hierarchical fashion. XPath node tests tell the XPath where to find the set of nodes in which XPath expressions are selected.
How do XPath Axes work?
XPath Location which is similar to the directory path which starts with the root node of the XML document and remaining in the next order.
General Syntax of declaring predefined XPath Axes is as follows:
AxesName :: current node[predicate]
Example: /country/state/address [2]/text ()
When we initiate with the location path with (/ or //) then it is termed as an absolute path and other than a root node. It means with the preceding context node is termed as relative. The term Axes- Every location step consists of an axis of a node with predicates.
For example, if suppose a location step is child::country
Here child is the axis which refers to all the <country> elements that are a child of the following nodes. And there are many different ways to perform node tests. One by using the wild card (*) to select attributes and elements like attribute:: * and another one is to use names of the node. All these could be done in the XPath Visualizer to perform their actions.
Secondly, it is necessary to learn predicates a subset of location step.
List of XPath Axes Values
Given below are different Axes values which are illustrated below.
It has a total of 13 axes values.
Sr.no | Axis name | Description |
1 | ancestor | This contains all the elements like grandparents, parents’ ancestor of the contextnode. In simple terms, this axis contains the root of the node. |
2 | ancestor-or-self | This refers to all the ancestor’s nodes and the root node itself (context node). |
3 | child | It refers to the child of the current node in the document during the traverse. |
4 | attribute | Refers to attributes of the current node in the specified document. |
5 | descendant | This refers to the descendants of the context node in the XML document. Namespaces or attributes are not included here. |
6 | descendant-or-self | This refers to the descendants of the context node in the XML document and the context node itself. |
7 | following | This contains all the nodes after the context node in a specific order but excluding context nodes descendants. |
8 | following-sibling | It contains the next subsequent nodes siblings followed by the root node. |
9 | self | Specifies their present node in the tree. |
10 | parent | It contains only one parent of the current node. |
11 | namespace | It refers to the namespace node of the root node with xmlns. And the axis is empty only when the context node is an element. |
12 | preceding | It contains all the nodes that come before the current tags(node). |
13 | preceding-sibling | The node traversed by the current context node to a previous sibling tag. |
Examples of XPath Axes
Given below are the examples mentioned:
Example #1
Code:
axes.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="shape.xsl" ?>
<shapes>
<shape type="general">
<name> Circle</name>
<area units="area">3.14</area>
<radius units="centimeters">123</radius>
</shape>
<shape type="general">
<name> Square</name>
<area units="area"> 56874</area>
<radius units="square centimeters">5896</radius>
</shape>
</shapes>
shape.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="area">
<xsl:for-each select="ancestor::*">
<xsl:value-of select="./name"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="shape">
<xsl:apply-templates select="area"/>
</xsl:template>
</xsl:stylesheet>
Output:
Example #2
Descendant- Axes.
Here it shows the result matching the ancestors’ grade for the child elements first name, last name. Therefore, the other values like grade or stud are invisible in the output.
Code:
Std.xml
<?xml version = "1.0" encoding="utf-8"?>
<grade>
<stud id = "545">
<firstname>nivas</firstname>
<lastname>raksh</lastname>
<score>85</score>
</stud>
<stud id = "622">
<firstname>britiandou</firstname>
<lastname>wasles tom</lastname>
<score>90</score>
</stud>
<stud id= "354">
<firstname>Ales river</firstname>
<lastname>Stefen</lastname>
<score>82</score>
</stud>
</grade>
Output:
Example #3
Descendant-or-self Axis.
For the above example, the Descendant self-Axis path is given. Here the result lists the names of all the descendant node of stud, not the grade node.
Output:
Example #4
Following axis.
Here we could get all elements following Aerostar with the descendant child elements of it (Type flight, contact, email) for the first second element due to this attribute Aerostar[2].
Code:
<?xml version="1.0" encoding="utf-8"?>
<Airline_db>
<Aerostar Emplo_id="11">
<Name>Johana joe</Name>
<Type_flight>American Airline</Type_flight>
<Contact>01-1218-58645</Contact>
<Email>johanamwims@ hotmail.com</Email>
</Aerostar>
<Aerostar Emplo_id="12">
<Name>antonybrutt</Name>
<Type_flight>Air India</Type_flight>
<Contact>04-1512-8622</Contact>
<Email>astius_17@ hotmail.com</Email>
</Aerostar>
</Airline_db>
Output:
Similarly, you can check with the aerostar[1] in the path to see the variant in the result.
Example #5
Following-siblings.
Displays the context node with the following next node of it. The above example is taken for this axis too.
Output:
Example #6
Parent axis.
It displays the parent node. If it is one it picks up the parent element of the timehour which is clocks.
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Clocks timezone="GMT">
<timehour>10</timehour>
<timeminute>30</timeminute>
<timesecond>45</timesecond>
<timemeridian>a.m.</timemeridian>
</Clocks>
Output:
Example #7
With XSLT and xml let’s see how the child axes works.
Code:
emp.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<division>
<supervisor id = "011">
<firstname>Abhilash</firstname>
<lastname>varun</lastname>
<address>Delhi</address>
<salary>18000</salary>
</supervisor>
<supervisor id = "012">
<firstname>Manuu</firstname>
<lastname>Sanjay</lastname>
<address>Mumbai</address>
<salary>36000</salary>
</supervisor>
<supervisor id = "013">
<firstname>Rawat</firstname>
<lastname>Kaushik</lastname>
<address>Uttarkhand</address>
<salary>50000</salary>
</supervisor>
</division>
employee.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0">
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/" >
<html>
<body>
<xsl:value-of select = "/division/supervisor/preceding-sibling::comment()"/>
<br/>
<xsl:text>First Supervisor: </xsl:text>
<xsl:value-of select = "/division/supervisor/child::firstname" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Example #8
Using namespace.
Code:
emp.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<division xmlns="http://www.xpathsample.com">
<supervisor id = "011">
<firstname>Abhilash</firstname>
<lastname>varun</lastname>
<address>Delhi</address>
<salary>18000</salary>
</supervisor>
<supervisor id = "012">
<firstname>Manuu</firstname>
<lastname>Sanjay</lastname>
<address>Mumbai</address>
<salary>36000</salary>
</supervisor>
<supervisor id = "013">
<firstname>Rawat</firstname>
<lastname>Kaushik</lastname>
<address>Uttarkhand</address>
<salary>50000</salary>
</supervisor>
</division>
employee.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0">
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match = "division" >
<html>
<body>
<xsl:value-of select = "namespace::*"/>
<br/>
<xsl:text>First Supervisor: </xsl:text>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Conclusion
That’s it for the article on location XPath axes. We saw how XPath Axes works with using location steps as the axis indicates how to search for a particular node. XPath Visualizer helps in making an XPath location absolutely in the XML document. Therefore, we have seen all the axes with the examples as well.
Recommended Articles
This is a guide to XPath Axes. Here we discuss the how do XPath axes work, list of XPath values with respective examples. You may also have a look at the following articles to learn more –