With the expansion of XML as a markup language for cross-platform data exchange, the need for a standard that would allow non-XML-based applications to perform complex queries on XML documents increased..
Extensible Markup Language (XML) is a language used to represent hierarchically structured data in text format. The XML language is readable by both humans and computers and is used, among others, to exchange data between two computer systems on the Internet.
With XQuery and XSLT, the W3C developed the necessary standards for software access to XML documents. They make programming interfaces available for applications to access and transform XML documents or query content. The necessary condition in these cases is a standard that allows the location of elements in XML documents, that is, a path description language: Xpath..
Next, we explain the Xpath Data Model (XDM) and introduce the syntax on which Xpath expressions are based for locating XML elements.
XML Path Language (Xpath) is a path description language for XML documents developed by the W3C. This language makes available to users a non-XML-based syntax that allows finding specific elements in an XML document..
Xpath is generally built into a host language that allows processing of XML elements. XQuery, for example, is used to extract information from XML elements located with Xpath. XSLT, for its part, uses the query language in the transformation of XML documents.
The current Xpath 3.1 version is specified in the W3C recommendation of March 21, 2017.
Despite the updates, many XSLT processors, web browsers, and applications only support the 1999 Xpath 1.0 standard.
Xpath is based on a data model that interprets the XML document as a sequence of elements arranged in a tree structure . This structure of the Xpath data model is comparable to that of the Document Object Model (DOM), which operates as an interface between HTML and JavaScript in the web browser.
Localization of XML elements is done based on the Unix registry system, in the form of path expressions. The basic elements of these locate paths are nodes, axes, node tests, and predicates.
Each of the elements of an XML structure is called a node. The classification of the nodes is defined both by the order of appearance in the document and by the interlacing of the XML elements.
The Xpath data model distinguishes seven types of nodes with different functions:
Let's look at the node types of the Xpath data model through an example. The following XML document serves the exchange of data in a book order and contains the seven types of nodes.
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE Order SYSTEM "order.dtd"> <?xml-stylesheet type="text/css" href="style.css"?> <!--That is a comment!--> <order date="2019-02-01"> <address xmlns:shipping="http://localhost/XML/delivery" xmlns:billing="http://localhost/XML/billing"> <shipping:name>Ellen Adams</shipping:name> <shipping:street>123 Maple Street</shipping:street> <shipping:city>Mill Valley</shipping:city> <shipping:state>CA</shipping:state> <shipping:zip>10999</shipping:zip> <shipping:country>USA</shipping:country> <billing:name>Mary Adams</billing:name> <billing:street>8 Oak Avenue</billing:street> <billing:city>Old Town</billing:city> <billing:state>PA</billing:state> <billing:zip>95819</billing:zip> <billing:country>USA</billing:country> </address> <comment>Please use gift wrapping!</comment> <items> <book isbn="9781408845660"> <title>Harry Potter and the Prisoner of Azkaban</title> <quantity>1</quantity> <priceus>22.94</priceus> <comment>Please confirm delivery date until Christmas.</comment> </book> <book isbn="9780544003415"> <title>The Lord of the Rings</title> <quantity>1</quantity> <priceus>17.74</priceus> </book> </items> </order>
In the Xpath tree, each of the elements that make up the XML document constitutes an element node, with the exception of the XML declaration and the definition of the document type, located at the beginning of the document.
XML declaration:
<?xml version="1.0"? encoding="utf-8"?>
Document type definition (DTD):
<!DOCTYPE Order SYSTEM "order.dtd">
Item nodes start with a start tag and end with an end tag and are typically interlocking.
The first element in the document is called the root element .
In the previous example, the root element is the order node , which acts as the parent element of the subset of address , comment and items nodes , which, in turn, also contain child elements.
The document is the root of the tree structure. In the XML document, this node is not visible or represented by text; Rather, it is a conceptual node that encloses all the elements of the document. The child elements of the document node are both the root element and, if necessary, the processing instruction node and the comment node.
The attribute of an XML element is represented in the Xpath data model as an attribute node. Each of these nodes consists of an identifier and a subordinate value.
In the sample code, the first book element contains the isbn attribute with the value 9781408845660 .
<book isbn="9781408845660">
The attribute node belongs to the element node; however, he is not considered a son.
The characters encapsulated between the tags of an element node called text node.
In the example, the title element node contains the text node Harry Potter and the Prisoner of Azkaban .
Harry Potter and the Prisoner of Azkaban
The element and attribute names of a properly constituted XML document are assigned a namespace. Usually this mapping is set by the DTD at the beginning of the document.
If elements or attributes with different namespaces are used in an XML document, the namespace in question will be explicitly defined using the xmlns attribute or the xmlns prefix in the start tag of the referred element. The value of the xmlns attribute must be a Uniform Resource Identifier (URI) that indicates which namespace an element is mapped to. Assigning a namespace to an xmlns prefix can be done with both the element and all children. In the tree structure, each of the namespaces corresponds to a namespace node.
In the sample code, two different namespaces have been defined for the XML address element : xmlns: shipping and xmlns: billing. The children of the address element have the corresponding prefix for their classification.
<address xmlns:shipping="http://localhost/XML/delivery" xmlns:billing="http://localhost/XML/ billing"> <shipping:name>Ellen Adams</shipping:name> <shipping:street>123 Maple Street</shipping:street> <shipping:city>Mill Valley</shipping:city> <shipping:state>CA</shipping:state> <shipping:zip>10999</shipping:zip> <shipping:country>USA</shipping:country> <billing:name>Mary Adams</billing:name> <billing:street>8 Oak Avenue</billing:street> <billing:city>Old Town</billing:city> <billing:state>PA</billing:state> <billing:zip>95819</billing:zip> <billing:country>USA</billing:country> </address>
Ultimately, the xmlns prefix makes it possible to clearly differentiate elements from different namespaces, so, in the example, the street element with the shipping prefix contains the information about the shipping address, while the street element with the billing prefix does reference to the billing address.
Processing instructions for XML documents are outside of the document tree and are called processing instruction nodes. The processing instruction node begins with <? and ends with ?> .
In the example above, we find the following processing instruction:
<?xml-stylesheet type="text/css" href="style.css"?>
The syntactic construction of the XML declaration at the beginning of the file is similar to that of a processing instruction, although it is not considered a processing instruction node in the sense of the Xpath data model.
In an XML document, Xpath processes the content marked as a comment as a comment node. This node includes only the highlighted characters and not the markup.
In the example above, we find the following comment node:
That is a comment!
The search for nodes is carried out with the help of so-called location paths. A locate path is an Xpath expression used to navigate through the tree and select the desired set of nodes.
Locate paths are analyzed from left to right and can be absolute or relative . An absolute path begins at the document node and is specified by the forward slash (/). In contrast, relative location paths can start at any other node within the tree structure, in which case the starting point is called a context node.
The path expression consists of steps separated by slashes (/), similar to addressing files in a registry system.
Each step in a path expression consists of up to three parts: axis, node test, and any number of predicates.
The steps for locating an Xpath expression are defined according to the following syntax:
Eje::prueba de nodo[predicado1][predicado2]?
The Xpath syntax allows navigating through the following axes.
The axes marked with an asterisk (*) are regressive applications that, according to version 1.0 of the Xpath specification, constitute an optional element that must not be supported by standard applications.
The following graphic shows a schematic representation of the most important axes in the Xpath data model, starting from the context node (in red).
The child :: axis , for example, returns all the child elements of context node D: the node set comprises nodes E, H, and I.
With the node test, you filter the set of nodes selected by the axis. According to the Xpath specification, there are two possible criteria.
In the example code above, all descendants of the document node that have the name book could be selected with the following path expression .
/descendant::book
However, if among all the element nodes with the name book, you want to filter only those with the isbn attribute , you need a path expression with two steps.
/descendant::book/attribute::isbn
If you want to define a node type as a filter criterion to select a set of nodes, use the following functions:
Xpath 1.0 defines 25 functions, and as of Xpath 2.0, 111 functions are available for the description of location paths. You will find a compendium in the W3C Xpath and XQuery Functions and Operators 3.1 recommendation of March 21, 2017.
If you use the spacer * (asterisk) instead of node tests, all nodes that correspond to the main node type of the selected axis are returned. That is, if an axis contains an element node, this will be the main node type of the axis. This applies to all axes, with the exception of attributes and namespaces, since the main node type in these cases would be the attribute node and namespace node, respectively.
The following stream expression, for example, returns all the attributes of the current context node:
attribute::*
For frequently used axes and locating steps, abbreviations have been defined that can be used in place of the full path expression in English.
Predicates allow you to fine-tune the node search performed using the axis and node test.
They are one third of a locate step, are optional, and are included using square brackets. Filter criteria in brackets are formulated as expressions containing, among others, path expressions, functions, operators, and strings.
Xpath supports both general and numeric predicates.
The general predicates filter the set of selected nodes through the y-axis of the node test, assigning a Boolean value ( true or false ) to each node, so that only nodes that meet the condition and have a true value are included in the result.
The formulation of expressions for general predicates is done by means of operators . These are used to select nodes with specific content or characteristics, such as, for example, all nodes with certain characters, an attribute or a preset child element (if possible, in a certain position).
In the following tables, you can see a summary of the available operators, which are classified into arithmetic, logical and comparison operators.
In the following example, the predicate [title = "Harry Potter and the Prisoner of Azkaban"] limits the results to an element node named book, whose child element title contains the character string Harry Potter and the Prisoner of Azkaban .
The example uses the Xpath 3 syntax, which, if necessary, may not be supported by all online tools. The query examples that we present here are easily seen with the following online tester : http://videlibri.sourceforge.net/cgi-bin/xidelcgi.
/order/items/book[title="Harry Potter and the Prisoner of Azkaban"]
Here, we have selected the book item node , which contains the data for the Harry Potter book.
<book isbn="9781408845660"> <title>Harry Potter and the Prisoner of Azkaban</title> <quantity>1</quantity> <priceus>22.94</priceus> <comment>Please confirm delivery date until Christmas.</comment> </book>
Another child element of this element node is comment . The path expression would only have to be extended with two more steps if we want to select its content.
/order/items/book[title="Harry Potter and the Prisoner of Azkaban"]/comment/text()
With the comment step (short for child :: comment ) we navigate to the child of the book element of the same name and select its text node with the text () function . This corresponds to the following string of characters:
Please confirm delivery date until Christmas.
When a single path expression is used in a predicate, it is a proof of existence . With the path expression shown below, it was checked whether the above XML document contains one or more nodes with the name of comment .
Abbreviated notation:
//book[comment]
Standard notation:
/descendant-or-self::node()/child::book[child::comment]
The // book [comment] path returns all nodes with the name book that have a child element named comment .
Numeric predicates allow you to locate nodes by indicating their position . The path expression below returns the second node based on its position in the document; the node name is book .
//book[2]
Strictly speaking, the predicate [2] is an abbreviation of [position () = 2] . With this, Xpath first selects all nodes with the name book and then filters the nodes where the position () = 2 function has a boolean value of true .
Unlike other programming languages, Xpath enumerations begin with 1.
On the W3C website, you will find an overview of the development levels XML Path Language is at, as well as all standards and drafts.
Similarly, you will find free tools and information for using Xpath with web applications at MDN Web Docs and on the Microsoft Developer Network.