XPath Expression Syntax
This document is an informal guide to the syntax of XPath 2.0 and 3.0 expressions, which are used in Saxon both within XSLT stylesheets, and in the Java API. XPath is also a subset of XQuery. For formal specifications, see the XPath 2.0 and XPath 3.0 specifications, except where differences are noted here.
There is also a summary of new features introduced in XPath 3.0 (originally published as XPath 2.1).
Saxon 9.6 implements XPath extensions to define maps: these extensions have been proposed by the XSLT Working Group for use in XSLT 3.0, and they are expected to become a standard part of XPath 3.1 and XQuery 3.1 in due course. Details are given here: Maps in XPath 3.0
XPath expressions may be used either in an XSLT stylesheet, or as a parameter to various Java interfaces. The syntax is the same in both cases. Saxon supports XPath either through the standard JAXP interface (which is somewhat limiting as it is designed for XPath 1.0 and is not well integrated with interfaces for XSLT and XQuery), or via Saxon's own s9api interface ("snappy"). In s9api, the steps are:
-
Create a
Processor
(which can also be used for XSLT, XQuery, and XSD processing) -
Use the
newXPathCompiler()
method to create anXPathCompiler
, and use its methods to set the static context for the expression -
Call
setLanguageVersion("3.0")
if support for XPath 3.0 is required -
Use the
compile()
method to compile the expression, and theload()
method to instantiate it for use (you can compile the method once and load it as many times as you like for execution) -
Call methods on the resulting
XPathSelector
object to set the context item and the values of any external variables for evaluating the expression -
Call another method to evaluate the expression. Because
XPathSelector
is a JavaIterable
, you can simply use the Java for-each construct to iterate over the results of the expression. -
If the results are sequences of nodes, as is often the case, they are returned as instances of the Saxon class
XdmNode
which provides methods to perform further processing of the results.
Saxon allows XPath expressions to be evaluated either against its own native tree models of XML (the tiny tree and linked tree), and also against trees built using the DOM, JDOM, JDOM2, DOM4J, XOM, or AXIOM tree models. (It's also possible to build adapters for other tree models, though this is not for the faint-hearted.)
In XPath 2.0 (and 3.0) all values are considered as sequences. A sequence consists of zero or
more items; an item may be a node or an atomic value, or in 3.0 it may also be a function
item, or a map. Examples of atomic values are integers, strings, booleans, and dates. A single value
such as a number is considered as a sequence of length 1. The empty sequence is written as
()
; a singleton sequence may be written as "a"
or
("a")
, and a general sequence is written as ("a", "b", "c")
.
The node-sets of XPath 1.0 are replaced in XPath 2.0 by sequences of nodes. Path expressions will return node sequences whose nodes are in document order with no duplicates, but other kinds of expression may return sequences of nodes in any order, with duplicates permitted.
This section summarizes the syntactic constructs and operators provided in XPath 2.0. The functions provided in the function library are listed separately: see the Functions section.