EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial What is XPath?
Secondary Sidebar
XML Tutorial
  • Xpath
    • What is XPath?
    • XPath namespace
    • XPath for JSON
    • XPath Last Element
    • Xpath Substring
    • XPath First Element
    • XPath local-name
    • XPath Multiple Attributes
    • XPath Matches
    • XPath Position
    • XPath Index
    • XPath regex
    • XPath id contains
    • XPath innertext
    • XPath Multiple Conditions
    • XPath Helper
    • XPath Generator
    • XPath ID
    • XPath Locator
  • Basic
    • What is XML?
    • XML Tags
    • XML URL
    • XPath Sibling
    • XML root element
    • XML Encryption
    • XML Parsing php
    • xml parsing with java
    • Dataset XML
    • XML Parser in C#
    • XML Tree
    • XML boolean
    • XML sitemap
    • XML Array
    • XML reserved characters
    • XML Viewer
    • XML Uses
    • XML Special Characters
    • XML generator
    • XML file format
    • XML DOM
    • XML ampersand
    • XML Mapping
    • XML File
    • XML Element
    • XML HttpRequest
    • XML XSD
    • XML Schema
    • XML Namespaces
    • XML Comments
    • XML Attributes
    • XML Encoding
    • XML Validation
    • XML CDATA
    • XML Database
    • XML Technologies
    • XML Error
    • XML DTD
    • XML Date
    • XML Parsers
    • String in XML
    • XML with CSS
    • XML Versions
    • XML Features
    • XML Commands
    • Servlet web.xml
    • XPath Injection
    • XPath Functions
    • XPath starts-with
    • XPath Selector
    • XPath Count
    • XPath XML
    • XML Parsing in Oracle
    • XML parsing in python

What is XPath?

By Priya PedamkarPriya Pedamkar

what is xpath

What is XPath?

XPath is a word that describes how objects can be identified and processed by using a syntax addressing a path through the logical structure or hierarchy of the document in Extensive Markup Language (XML) documents. It makes it easier to write programming phrases than to understand the XML-style and sequence of each word in a document. XPath can also allow the programmer in a higher level of abstraction to handle the text. XPath is a language that both Extensible Language Transformations (XSLT), as well as XPointer, uses and defines. XPath is an extensible language. The abstraction of information defined in the XML Information Set is used.

Linking in XML is divided into two parts: XLink and XPointer. XLink and XPointer define a standard way of creating hyperlinks in XML documents.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

xpath

Expression of XPath

XPath allows different types of expressions to retrieve relevant information from the XML document. XPath addresses a specific part of document. It models an XML document as a tree of nodes. An expression of XPath is a technique for navigating through and selecting nodes from the document.

XPath expressions can be used in C, C++, Python, Java, JavaScript, PHP, XML Schema and many other languages. An XPath expression refers to a pattern to select a set of nodes. XPointer use these patterns for addressing purpose or to perform transformations by XSLT. The XPath expression specifies seven types of nodes which can be the result of execution.

1. Root

Root element of an XML document. Using the following ways root elements can be found.

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,198 ratings)
  • Use Wildcard (/*): To select the root node
  • Use Name (/class):  To select the root node by name
  • Use Name with a wildcard (/class/*): To select all elements under the root node

Code:

<!—-Using Wildcard-->
<p><xsl:value-of select = “name(/*)”/></p>
<!—-Using Name-->
<p><xsl:value-of select = “name(/class”/></p>
<!—-Using Both-->
<p><xsl:value-of select = “name(/class/*)”/></p>

2. Element

Element node of an XML document. Below are the ways to find element

  • /class/*: used to select all elements under the root node.
  • /class/library: used to select all the library elements from the root node.
  • //library: used to select the entire library element from the document.

Code:

<!—-Using /class/*-->
<xsl:for-each select = "/class/*">
<!—-all library element from root node -->
<xsl:for-each select = "/class/library">
<!—entire library element from document-->
<xsl:for-each select = "//library">

3. Attributes

An attribute of an element node in the XML document retrieved and checked by using the @attribute-name of an element.

Code:

<!—To get value of attribute Book ID -->
<td><xsl:value-of select = "@bookId"/></td>

4. Text

Text of an element node in the XML document, retrieved and checked by name of an element.

Code:

<!—To get text value of Book Name -->
<td><xsl:value-of select = "bookname"/></td>

5. Comment

Example of comment

Code:

<!-- Comment: Library contains below books. -->

Node or List of the node from XML

Following are the list of useful expressions to select a node or list of the node from an XML document.

  • ‘/’: Using this selection start from the root node.
  • ‘//’: Using this selection starts from the current node which matches selection
  • ‘.’: To select current this expression used.
  • ‘..’: To select the parent node of the current node.
  • ‘@’: To select attributes.

Example

To understand an XPath expression, we’ve created an XML document, library.xml, and its style sheet document library.xsl which uses the XPath expressions under the select attribute of various XSL tags to get the values of book id, book name, author, publication, and price of each book node.

1. library.xml

Code:

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "library.xsl"?>
<class>
<!-- Comment: Library contains below books. -->
<library bookid = "291">
<bookname>Deep Work: Rule for success in a Distracted world</bookname>
<author>Cal Newport</author>
<publication>White Paper</publication>
<price>115</price>
</library>
<library bookid = "292">
<bookname>The Heart of Success</bookname>
<author>Nil Manoj Sharma </author>
<publication>The Print</publication>
<price>95</price>
</library>
<library bookid = "293">
<bookname>The King and The Queen</bookname>
<author>Yashpal Singh</author>
<publication>Lotus</publication>
<price>190</price>
</library>
</class>

2. library.xsl

Code:

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<!-- Declaration -->
<xsl:template match = "/">
<html>
<body>
<h2>Library</h2>
<table border = "2">
<tr bgcolor = "#afafaf">
<th>Book ID</th>
<th>Book Name</th>
<th>Author</th>
<th>Publication</th>
<th>Price</th>
</tr>
<!—For Loop -->
<xsl:for-each select = "class/library">
<tr>
<!—Table Details -->
<td> <xsl:value-of select = "@bookid"/></td>
<td><xsl:value-of select = "bookname"/></td>
<td><xsl:value-of select = "author"/></td>
<td><xsl:value-of select = "publication"/></td>
<td><xsl:value-of select = "price"/></td>
</tr>
</xsl:for-each>
<!—End of loop -->
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Output:

xpath example

Benefits of XPath

Below are the benefits :

  • XPath queries are simple to type and read and also are compact.
  • XPath syntax is easy for the common and simple cases.
  • The query strings are embedded in scripts, programs, & HTML or XML attributes easily.
  • The XPath queries are easily analyzed.
  • Any node can uniquely recognize in an XML document.
  • In an XML document, the occurrence of any path or any set of conditions for the nodes in path can be specified.
  • Queries return any number of results, including zero.
  • In an XML document, query conditions can be calculated at any level and are not supposed to traverse from the top node of an XML document.
  • The queries return unique nodes, not repeated nodes.
  • In many contexts, it is used, to provide links to nodes, for finding repositories & many other applications.
  • For the programmers, the queries are not procedural but more declarative. They define how elements should be traversed. To get efficient results, indexes and other structures must be used free by a query optimizer.

Conclusion

XPath is a query language used to traverse elements, attributes, text through an XML document. XPath is used widely to find particular elements or attribute with matching patterns. When a query is defined, then that XML data can be represented as a tree. The hierarchical representation of XML data is called a tree. The top of the tree is a root node. In a tree, each attribute, elements, text, comments, string, and processing instruction corresponds to one node. The relationships between the nodes can be represented by the tree.

Recommended Articles

This is a guide to What is XPath?. Here we discuss expression, list, examples, and benefits of Xpath. You can also go through our other related articles to learn more-

  1. What is XPath in Selenium?
  2. What is XML?
  3. New Career Path
  4. Information Security Career Path
Popular Course in this category
Selenium Automation Testing Training (11 Courses, 4+ Projects, 4 Quizzes)
  11 Online Courses |  4 Hands-on Projects |  45+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
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