XPath

1

XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. Support for XPath exists in applications that support XML, such as web browsers, and many programming languages.

Overview

The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria. In popular use (though not in the official specification), an XPath expression is often referred to simply as "an XPath". Originally motivated by a desire to provide a common syntax and behavior model between XPointer and XSLT, subsets of the XPath query language are used in other W3C specifications such as XML Schema, XForms and the Internationalization Tag Set (ITS). XPath has been adopted by a number of XML processing libraries and tools, many of which also offer CSS Selectors, another W3C standard, as a simpler alternative to XPath.

Versions

There are several versions of XPath in use. XPath 1.0 was published in 1999, XPath 2.0 in 2007 (with a second edition in 2010), XPath 3.0 in 2014, and XPath 3.1 in 2017. However, XPath 1.0 is still the version that is most widely available.

Syntax and semantics (XPath 1.0)

The most important kind of expression in XPath is a location path. A location path consists of a sequence of location steps. Each location step has three components: An XPath expression is evaluated with respect to a context node. An Axis Specifier such as 'child' or 'descendant' specifies the direction to navigate from the context node. The node test and the predicate are used to filter the nodes specified by the axis specifier: For example, the node test 'A' requires that all nodes navigated to must have label 'A'. A predicate can be used to specify that the selected nodes have certain properties, which are specified by XPath expressions themselves. The XPath syntax comes in two flavors: the abbreviated syntax, is more compact and allows XPaths to be written and read easily using intuitive and, in many cases, familiar characters and constructs. The full syntax is more verbose, but allows for more options to be specified, and is more descriptive if read carefully.

Abbreviated syntax

The compact notation allows many defaults and abbreviations for common cases. Given source XML containing at least the simplest XPath takes a form such as that selects C elements that are children of B elements that are children of the A element that forms the outermost element of the XML document. The XPath syntax is designed to mimic URI (Uniform Resource Identifier) and Unix-style file path syntax. More complex expressions can be constructed by specifying an axis other than the default 'child' axis, a node test other than a simple name, or predicates, which can be written in square brackets after any step. For example, the expression selects the first child (' '), whatever its name, of every B element that itself is a child or other, deeper descendant (' ') of an A element that is a child of the current context node (the expression does not begin with a ' '). The predicate binds more tightly than the operator. To select the first node selected by the expression, write. Note also, index values in XPath predicates (technically, 'proximity positions' of XPath node sets) start from 1, not 0 as common in languages like C and Java.

Expanded syntax

In the full, unabbreviated syntax, the two examples above would be written Here, in each step of the XPath, the axis (e.g. or ) is explicitly specified, followed by and then the node test, such as or in the examples above. Here the same, but shorter: A//B/*[position=1]

Axis specifiers

Axis specifiers indicate navigation direction within the tree representation of the XML document. The axes available are: As an example of using the attribute axis in abbreviated syntax, selects the attribute called in elements anywhere in the document tree. The expression . (an abbreviation for self::node) is most commonly used within a predicate to refer to the currently selected node**.** For example, selects an element called in the current context, whose text content is.

Node tests

Node tests may consist of specific node names or more general expressions. In the case of an XML document in which the namespace prefix has been defined, will find all the elements in that namespace, and will find all elements, regardless of local name, in that namespace. Other node test formats are:

Predicates

Predicates, written as expressions in square brackets, can be used to filter a node-set according to some condition. For example, returns a node-set (all the elements which are children of the context node), and a[@href='help.php'] keeps only those elements having an attribute with the value. There is no limit to the number of predicates in a step, and they need not be confined to the last step in an XPath. They can also be nested to any depth. Paths specified in predicates begin at the context of the current step (i.e. that of the immediately preceding node test) and do not alter that context. All predicates must be satisfied for a match to occur. When the value of the predicate is numeric, it is syntactic-sugar for comparing against the node's position in the node-set (as given by the function ). So is shorthand for p[position=1] and selects the first element child, while is shorthand for p[position=last] and selects the last child of the context node. In other cases, the value of the predicate is automatically converted to a Boolean. When the predicate evaluates to a node-set, the result is true when the node-set is. Thus selects those elements that have an attribute named. A more complex example: the expression a[/html/@lang='en'][@href='help.php'][1]/@target selects the value of the attribute of the first element among the children of the context node that has its attribute set to , provided the document's top-level element also has a attribute set to. The reference to an attribute of the top-level element in the first predicate affects neither the context of other predicates nor that of the location step itself. Predicate order is significant if predicates test the position of a node. Each predicate takes a node-set returns a (potentially) smaller node-set. So a[1][@href='help.php'] will find a match only if the first child of the context node satisfies the condition , while a[@href='help.php'][1] will find the first child that satisfies this condition.

Functions and operators

XPath 1.0 defines four data types: node-sets (sets of nodes with no intrinsic order), strings, numbers and Booleans. The available operators are: The function library includes: Some of the more commonly useful functions are detailed below.

Node set functions

String functions

Boolean functions

Number functions

Usage examples

Expressions can be created inside predicates using the operators: and. Boolean expressions may be combined with brackets and the Boolean operators and as well as the function described above. Numeric calculations can use and. Strings can consist of any Unicode characters. //item[@price > 2*@discount] selects items whose price attribute is greater than twice the numeric value of their discount attribute. Entire node-sets can be combined ('unioned') using the vertical bar character |. Node sets that meet one or more of several conditions can be found by combining the conditions inside a predicate with ' '. will return a single node-set consisting of all the elements that have or child-elements, as well as all the elements that have child-elements, that were found in the current context.

Syntax and semantics (XPath 2.0)

Syntax and semantics (XPath 3)

Examples

Given a sample XML document The XPath expression /Wikimedia/projects/project/@name selects name attributes for all projects, and /Wikimedia//editions selects all editions of all projects, and /Wikimedia/projects/project/editions/edition[@language='English']/text selects addresses of all English Wikimedia projects (text of all elements where attribute is equal to English). And the following /Wikimedia/projects/project[@name='Wikipedia']/editions/edition/text selects addresses of all Wikipedias (text of all elements that exist under element with a name attribute of Wikipedia).

Implementations

Command-line tools

C/C++

Free Pascal

Implementations for database engines

Java

The Java package **** has been**** part**** of**** Java**** standard**** edition since Java**** 5 via the Java**** API for XML Processing****.**** Technically this is an XPath API rather than an XPath implementation, and it allows the programmer the ability to select a specific implementation that conforms to the interface.

JavaScript

.NET Framework

Perl

PHP

Python

Ruby

Scheme

SQL

Tcl

Use in schema languages

XPath is increasingly used to express constraints in schema languages for XML.

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article