EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

What is XPath?

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Software Testing Tutorial » What is XPath?

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.

  • 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

Popular Course in this category
Selenium Automation Testing Training (9 Courses, 4+ Projects, 4 Quizzes)9 Online Courses | 4 Hands-on Projects | 38+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (6,079 ratings)
Course Price

View Course

Related Courses
Software Testing Training (9 Courses, 2 Projects)Penetration Testing Training Program (2 Courses)TestNG Training (4 Courses, 2 Project)
  • /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 of XPath

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 of Xpath:

  • 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 XPath queries return unique nodes, not repeated nodes.
  • In many contexts, XPath is used, to provide links to nodes, for finding repositories & many other applications.
  • For the programmers, XPath 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

Selenium Automation Testing Training (9 Courses, 4+ Projects)

9 Online Courses

4 Hands-on Projects

38+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Software Testing Tutorial
  • Advance
    • Cyclomatic Complexity
    • Decision Table Testing
    • Decision Tree Algorithm
    • What is Continuous Integration
    • Mantis Bug Tracker
    • Equivalence Partitioning
    • Gantt Chart Software
    • Install TestNG
    • Install Unity
    • Defect Management Process
    • Test Plan Template
    • Testing Interview Questions
    • Testing of Mobile application
    • What is Test Automation Frameworks
    • Application of Automation
    • Test Automation Process
    • What is Cucumber?
    • 15 Best Popular Bug Reporting Tools
    • What is Automated Testing?
    • Software Maintenance Types
    • Types of Penetration Testing
    • Software Reliability
    • Best Gantt Chart Software
    • Code Coverage
    • Branch Coverage
    • Decision Coverage
    • Statement Coverage
    • What is Test Case
    • Types of Test Case
    • What is Test Scenario
    • Formal Review
    • Alpha Beta Pruning
    • What is Cyclomatic Complexity?
    • Test Coverage
    • How to Write Test Case
    • Testing Documentation
    • Performance Testing Life Cycle
    • What is Xpath
    • Test Harness
    • Test Strategy
    • Software Incident Management
    • What is Debugging
    • What is Defect?
    • Listeners in TestNG
  • Basics
    • What is Software Testing
    • Careers in Software Testing
    • Defect Life Cycle in Software Testing
    • Levels of Software Testing
    • Software Testing Life Cycle
    • Software Tester Work
    • Software Testing Principles
    • Testing Methodologies
    • Grey Box Testing
    • Types of Software Testing
    • What is a Bug in Software Testing
    • Benefits of Automation Testing
    • What is Automation Testing?
    • Types of Automation
    • Automation Testing Process
    • Mobile Automation Testing
    • Automation Testing Life Cycle
    • Software Quality Assurance
    • Software Quality Assurance
    • What is Test Environment?
    • Verification and Validation Testing
  • Types of Testing
    • Adhoc Testing
    • Agile Testing
    • What is Agile Testing
    • Alpha and Beta Testing
    • Application Testing
    • Automation Testing
    • Benchmark Testing
    • Black Box Testing
    • Domain Testing
    • Dynamic Testing
    • Ecommerce Testing
    • Fuzz Testing
    • Gray Box Testing
    • GUI Testing
    • Installation Testing
    • Interface Testing
    • Interoperability Testing
    • Mainframe Testing
    • Manual Testing
    • Mutation Testing
    • Monkey Testing
    • Negative Testing
    • Penetration Testing
    • Protocol Testing
    • Recovery Testing
    • Regression Testing
    • Accessibility Testing
    • Sanity Testing
    • Scalability Testing
    • Security Testing
    • Spike Testing
    • Stability Testing
    • State Transition Testing
    • Static Testing
    • Gatling Load Testing
    • System Integration Testing
    • Structural Testing
    • Locust Load Testing
    • System Testing
    • Control Flow Testing
    • Unit Testing
    • Volume Testing
    • Web Testing Application
    • What is Exploratory Testing
    • What is Stress Testing
    • What is Usability Testing
    • White Box Testing
    • Types of White Box Testing
    • Compatibility Testing 
    • Use Case Testing
    • Beta Testing
    • Integration Testing
    • Non Functional Testing
    • What is Functional Testing
    • Cookie Testing
    • Alpha Testing
    • Boundary Value Testing
    • Equivalence Class Testing
    • Glass Box Testing
    • SOA Testing
    • Smoke Testing
    • Visual Testing
    • Visual Paradigm
    • Model-Based Testing
  • Testing techniques
    • Software Testing Methodologies
    • Black Box Testing Techniques
    • Static Testing Techniques
    • Test Case Design Techniques
    • What is Static Analysis
  • Testing tools
    • Automation Testing Tools
    • Functional Testing Tools
    • GUI Testing Tools
    • Penetration Testing Tools
    • Performance Testing Tools
    • SOA Testing Tools
    • Accessibility Testing Tools
    • What is QTP
    • Regression Testing Tools
    • Security Testing Tools
    • Test Management Tools
    • Code Coverage Tools
    • Test Coverage Tools
    • Defect Tracking Tools
    • Continuous Integration Tools
    • Install Bugzilla
    • Test data generation tool
    • Unit Testing Tools
    • Web Testing Tools
    • Stress Testing Tools
    • Performance Monitoring Tools
    • Mobile Testing Tools
    • Responsive Testing Tool
    • Cross Browser Testing Tools
    • Risk Based Testing
    • Database Testing Tools
    • WinRunner
    • What is Squish?
    • CubicTest
    • What is WinRM?
    • Bugzilla Tool
    • Code review tools
    • Penetration Testing Open Source Tools
  • Inteview Questions
    • Automation Testing Interview Questions
    • Manual Testing Interview Questions
    • ISTQB Interview Questions
    • Cucumber Interview Questions
    • Software Testing Interview Questions
    • Penetration Testing Interview Questions

Related Courses

Software Testing Course

Penetration Training Course

TestNG Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • 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

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

EDUCBA Login

Forgot Password?

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
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
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

Special Offer - Selenium Automation Testing Training (9 Courses, 4+ Projects) Learn More