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.
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.
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
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.
An attribute of an element node in the XML document retrieved and checked by using the @attribute-name of an element.
Text of an element node in the XML document, retrieved and checked by name of an element.
Example of comment
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.
Nil Manoj Sharma
Book ID Book Name Author Publication Price
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.
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.
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-
- What is XPath in Selenium?
- What is XML?
- New Career Path
- Information Security Career Path
The post What is XPath? appeared first on EDUCBA.