What is XPath XML?
XML XPath is a well-known expression language curated to transform XML documents and support queries. It helps to navigate through any document and address it. XPath is used to identify and manage the parts of an XML document, such as elements and attributes. In XML, these elements and attributes are termed ‘nodes.’ The XPath expression can search across any XML document and extract data from a specific node. This enables to query or alter XML documents.
Key Highlights
- XPath is used to identify the nodes in an XML document and extract data from them.
- It is utilized in both the Software Testing and Software Development industries.
- XPath consists of expressions, which are strings of Unicode characters.
- It plays a crucial role in Automation testing. In manual testing, the knowledge of XPaths will help understand the ongoing process at the application’s backend.
XPath XML Path Language
- The basic building block of XPath is a string of Unicode characters known as expressions.
- There are many types of expressions that you can construct by combining operands, symbols, and keywords.
- Typically, the operands are other expressions, and nesting of expressions is allowed.
- The names of the expressions can be identical to the language keywords except for specific unprefixed names of functions.
- XML XPath language is case-sensitive, and the keywords used are in lowercase, as the main intention of using XPath is to address the parts of the XML document.
- For this, XPath has more than 200 inbuilt functions for sequence manipulation, node manipulation, date and time comparison, Boolean values, numeric values, and string values, among others.
- A non-XML, compact syntax is used in XPath to make it easy to deal with URI and XML attribute values.
- The name includes the term ‘path’ because its path notation is quite similar to URLs.
- The World Wide Web Consortium (W3C) has recommended XPath versions 1.0, 2.0, and 3.0 on November 16, 1999; January 23, 2007; and April 8, 2014, respectively. XPath is a prime part of the XSLT standard.
Generate XPath from XML
- XPath adheres to the Document Object Model (DOM).
- DOM treats every XML document as a tree of nodes.
- These nodes are categorized into the following types: comment, processing instruction, namespace, text, attribute, element, and root.
The following is an XML document.
Code:
<?xml version=”3.0” encoding=ISO-9588-2”?>
<colleges>
<college type=”arts”>
<name>abc</name>
<principal>ABC</principal>
<students>500</students>
</college>
<college type=”commerce”>
<name>def</name>
<principal>DEF</principal>
<students>1000</students>
</college>
</colleges>
- ‘Colleges’ is the root node. ‘College’ is an element with the attribute ‘type.’
- ‘Name,’ ‘principal,’ and ‘students’ are elements.
- To generate the XPath document, you need to use location-path expressions.
- A slash is used to separate the child nodes. //colleges/college returns all college elements.
- A double slash returns all elements that match the search criteria. //colleges/college/students return all the student’s elements.
- Square brackets are used to return elements with specific values or child nodes. //colleges/college[name=” ABC”]
XPath XML Finder
The following are some of the XPath Finder extensions for Google Chrome:
- XPath Analyzer evaluates the XPath/XSLT expressions against the XML that exists at a specific URL.
- Esky can assist you in creating locators in the point-and-capture method. You need to click any web element on the webpage to create an XPath/CSS Selector.
- XPath is supportive of XPath 2.0. It displays the matching nodes on the sidebar. You can find the matched node on the page when you click the node on the sidebar.
- XPath Helper Plus generates XPaths that are less likely to break in case of website changes and are short. In case the parent XPath filed has an XPath, then relative to this parent XPath, the subsequent XPaths are created.
- Relative XPath Helper can determine the relative XPath expression of two web elements. You must click the first and second elements to create the relative XPath.
- ChroPath generates and validates unique selectors, such as partiallinktext, linkText, cssSelectors, absolute xpath, and relative xpath, after a single click. It facilitates writing, editing, extracting, and evaluating XPath queries on any webpage.
- XPath Helper makes extracting, modifying and evaluating XPath queries on any webpage very easy. You need to click the extension, and this opens the console to write or modify the XPath.
- SelectorsHub can write XPath and cssSelector in less than five seconds. It provides autosuggestions for the text, all attributes, and other data for completion.
- While using this, you need not copy and paste the values of the attributes from DOM to generate the XPath and cssSelector.
- If there is something erroneous in the XPath and cssSelector, this finder displays the precise error message. It is the singular tool that supports shadowDOM selectors.
XPath XML Example
Following is an XML document.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<films>
<film category="action">
<title lang="en">ABC</title>
<director>XYZ</director>
<year>1995</year>
<budget>1000</budget>
</film>
<film category="horror">
<title lang="en">DEF</title>
<director>MNO</director>
<year>1990</year>
<budget>2000</budget>
</film>
<film category="love">
<title lang="en">GHI</title>
<director>JKL</director>
<director>PQR</director>
<director>STU</director>
<director>JLD</director>
<year>1992</year>
<budget>1500</budget>
</film>
<film category="comedy">
<title lang="en">QRS</title>
<director>ZSD</ director>
<year>1999</year>
<price>2500</price>
</film>
</films>
The XPath syntax is the following.
- To select all title nodes, use /films/film/title.
- To select the first film’s title, use /films/film[1]/title.
- To select all prices, use /films/film/price[text()].
- To select all price nodes with a price more than 1500, use /films/film[price>1500]/price.
- To select all title nodes with a price of more than 1500, use /films/film[price>1500]/title.
Conclusion
As discussed, the XML path comes in handy for navigating through XML documents. Here navigation means going ahead in an XML document in any direction, going to any element, attribute, or text node. If you want to browse through this document and extract selective data from these nodes, XPath is the best option. To create XPath files, you can take the assistance of XPath finders.
FAQ
Q1. What is XPath in XML example?
Answer: XPath utilizes path expressions to pick nodes or node sets in any XML document. Such path expressions can be assumed as the expressions we come across while working on any traditional computer. XPath expressions are useful in JavaScript, XML Schema, Java, PHP, Python, and other languages.
Q.2 What is the use of XPath in XML?
Answer: The XPath or XML Path Language uniquely addresses or identifies the parts of an XML document. The Xpath expression is used to navigate through any XML document and collect information from any section of the document that can be an attribute or element (nodes).
Q.3 What is XPath in Selenium?
Answer: XPath is considered a Selenium technique that is used to operate through the HTML structure of any page. It helps the testers to operate through the XML structure of any document for using it on both XML and HTML documents.
Q.4 What are the types of XPath?
Answer: There are two types of XPath that are named Absolute and Relative XPaths. Here, Absolute XPath helps to find the element directly. Whereas for Relative XPath, the path begins from the mid of the HTML DOM structure.
Recommended Articles
This article entails information about the XPath XML. It explains the language, its examples, and much more. You can learn more about similar topics from the following articles,