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.5 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:

  1. Create a Processor (which can also be used for XSLT, XQuery, and XSD processing)

  2. Use the newXPathCompiler() method to create an XPathCompiler, and use its methods to set the static context for the expression

  3. Call setLanguageVersion("3.0") if support for XPath 3.0 is required

  4. Use 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)

  5. Call methods on the resulting XPathSelector object to set the context item and the values of any external variables for evaluating the expression

  6. Call another method to evaluate the expression. Because XPathSelector is a Java Iterable, you can simply use the Java for-each construct to iterate over the results of the expression.

  7. 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.

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. 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.