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.[2] 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
Two versions of XPath are currently in use. The current version of the language is 2.0, but version 1.0 is still more widely used.[3]
- XPath 1.0 became a Recommendation on 16 November 1999 and is widely implemented and used, either on its own (called via an API from languages such as Java, C#, Python or JavaScript), or embedded in languages such as XSLT, XProc, XML Schema or XForms.
- XPath 2.0
is the current version of the language; it became a Recommendation on
23 January 2007. A number of implementations exist but are not as widely
used as XPath 1.0. The XPath 2.0 language specification is much larger
than XPath 1.0 and changes some of the fundamental concepts of the
language such as the type system.
- The most notable change is that XPath 2.0 is built around the XQuery and XPath Data Model which has a much richer type system.[4] Every value is now a sequence (a single atomic value or node is regarded as a sequence of length one). XPath 1.0 node-sets are replaced by node sequences, which may be in any order.
- To support richer type sets, XPath 2.0 offers a greatly expanded set of functions and operators.
- XPath 2.0 is in fact a subset of XQuery 1.0. They share the same data model (XDM). It offers a
for
expression which is a cut-down version of the "FLWOR" expressions in XQuery. It is possible to describe the language by listing the parts of XQuery that it leaves out: the main examples are the query prolog, element and attribute constructors, the remainder of the "FLWOR" syntax, and thetypeswitch
expression.
- XPath 3.0 is a working draft as of 2012.[5] For details about what's new about XPath 3.0, see[6]
Syntax and semantics
- This section describes 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 axis
- a node test
- zero or more predicates.
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.