XPath 2.0 Expression Syntax
This document is an informal guide to the syntax of XPath 2.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 specification, 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.4 implements XPath extensions to define maps: these extensions have been proposed by the XSL Working Group, but not yet accepted by the XQuery Working Group. 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) -
User the
newXPathCompiler()
method to create anXPathCompiler
, and use its methods to set the static context for the expression -
User the
compile()
method to compile the expression, and the load() 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.
An important change in XPath 2.0 is that all values are now considered as sequences. A sequence
consists of zero or more items; an item may be a node or a simple-value. Examples of simple-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.