EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XPath Axes
Secondary Sidebar
file_put_contents in PHP

is_array in PHP

Introduction to Python

Python Competitive Programming

Java NIO SocketChannel

Python Books

XPath Axes

By Priya PedamkarPriya Pedamkar

XPath Axes

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,950 ratings)

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:

XPath Axes 1

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:

XPath Axes 2

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:

Descendant-or-self Axis

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:

XPath Axes 4

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:

XPath Axes 5

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:

Parent axis

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:

With XSLT and xml

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:

Using namespace.

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 –

  1. XPath Operators
  2. What is XPath?
  3. What is XPath in Selenium?
  4. XML Parsers
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more