Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is XPath?

what is xpath

Introduction to XPath

XPath is a prime and core component of the XSLT standard. Xpath can be used to traverse the elements, attributes, text, processing-instruction, comment, namespace and document in an Extensible Markup Language (XML) document. It is a W3C recommendation that contains a library over 200 built-in functions. XPath is the syntax for defining parts of an Xml Document.  XSLT is the style sheet language for XML files. With XSLT you can transform XML documents into other formats, like XHTML. XQuery is about querying XML data. XQuery is designed to query anything that can appear as XML, including databases. Linking in XML is divided into two parts: XLink and XPointer. XLink and XPointer define a standard way of creating hyperlinks in XML documents.

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.

  1. Use Wildcard (/*): To select the root node
  2. Use Name (/class):  To select the root node by name
  3. Use Name with a wildcard (/class/*): To select all elements under the root node

Code:






2. Element

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

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

Code:






3. Attributes

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

Code:


4. Text

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

Code:


5. Comment

Example of comment

Code:

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:






Deep Work: Rule for success in a Distracted world
Cal Newport
White Paper
115


The Heart of Success
Nil Manoj Sharma
The Print
95


The King and The Queen
Yashpal Singh
Lotus
190

2. library.xsl

Code:


xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">


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

The post What is XPath? appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

What is XPath?

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×