View Old View New View Both View Only Previous Next

This draft contains only sections that have differences from the version that it modified.

W3C

XML Path Language (XPath) 4.0 WG Review Draft

W3C Editor's Draft 23 February 2026

This version:
https://qt4cg.org/specifications/xpath-40/
Most recent version of XPath:
https://qt4cg.org/specifications/xpath-40/
Most recent Recommendation of XPath:
https://www.w3.org/TR/2017/REC-xpath-31-20170321/
Editor:
Michael Kay, Saxonica <mike@saxonica.com>

Please check the errata for any errors or issues reported since publication.

See also translations.

This document is also available in these non-normative formats: XML.


Abstract

XPath 4.0 is an expression language that allows the processing of values conforming to the data model defined in [XDM 4.0]. The name of the language derives from its most distinctive feature, the path expression, which provides a means of hierarchic addressing of the nodes in an XML tree. As well as modeling the tree structure of XML, the data model also includes atomic items, function items, maps, arrays, and sequences. This version of XPath supports JSON as well as XML, and adds many new functions in [Functions and Operators 4.0].

XPath 4.0 is a superset of XPath 3.1. A detailed list of changes made since XPath 3.1 can be found in I Change Log.

Status of this Document

This is a draft prepared by the QT4CG (officially registered in W3C as the XSLT Extensions Community Group). Comments are invited.

Dedication

The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).

Michael was central to the development of XML and many related technologies. He brought a polymathic breadth of knowledge and experience to everything he did. This, combined with his indefatigable curiosity and appetite for learning, made him an invaluable contributor to our project, along with many others. We have lost a brilliant thinker, a patient teacher, and a loyal friend.


4 Expressions

This section discusses each of the basic kinds of expression. Each kind of expression has a name such as PathExpr, which is introduced on the left side of the grammar production that defines the expression. Since XPath 4.0 is a composable language, each kind of expression is defined in terms of other expressions whose operators have a higher precedence. In this way, the precedence of operators is represented explicitly in the grammar.

The order in which expressions are discussed in this document does not reflect the order of operator precedence. In general, this document introduces the simplest kinds of expressions first, followed by more complex expressions. For the complete grammar, see Appendix [A XPath 4.0 Grammar].

The highest-level symbol in the XPath grammar is XPath.

XPath::=(DefaultElementNamespaceDecl ";")? (NamespaceDecl ";")* Expr
DefaultElementNamespaceDecl::="declare" "default" "element" "namespace" URILiteral
NamespaceDecl::="declare" "namespace" NCName "=" URILiteral
Expr::=(ExprSingle ++ ",")

The effect of a DefaultElementNamespaceDecl or NamespaceDecl is described in 4.1 Namespace Declarations.

ExprSingle::=ForExpr
| LetExpr
| QuantifiedExpr
| IfExpr
| OrExpr
ForExpr::=ForClauseForLetReturn
LetExpr::=LetClauseForLetReturn
QuantifiedExpr::=("some" | "every") (QuantifierBinding ++ ",") "satisfies" ExprSingle
IfExpr::="if" "(" Expr ")" (UnbracedActions | BracedAction)
OrExpr::=AndExpr ("or" AndExpr)*

The XPath 4.0 operator that has lowest precedence is the comma operator, which is used to combine two operands to form a sequence. As shown in the grammar, a general expression (Expr) can consist of multiple ExprSingle operands, separated by commas.

The name ExprSingle denotes an expression that does not contain a top-level comma operator (despite its name, an ExprSingle may evaluate to a sequence containing more than one item.)

The symbol ExprSingle is used in various places in the grammar where an expression is not allowed to contain a top-level comma. For example, each of the arguments of a function call must be a ExprSingle, because commas are used to separate the arguments of a function call.

After the comma, the expressions that have next lowest precedence are ForExpr, LetExpr, QuantifiedExpr, IfExpr, and OrExpr. Each of these expressions is described in a separate section of this document.

4.14 Maps and Arrays

Most modern programming languages have support for collections of key/value pairs, which may be called maps, dictionaries, associative arrays, hash tables, keyed lists, or objects (these are not the same thing as objects in object-oriented systems). In XPath 4.0, we call these maps. Most modern programming languages also support ordered lists of values, which may be called arrays, vectors, or sequences. In XPath 4.0, we have both sequences and arrays. Unlike sequences, an array is an item, and can appear as an item in a sequence.

Note:

The XPath 4.0 specification focuses on syntax provided for maps and arrays, especially constructors and lookup.

Some of the functionality typically needed for maps and arrays is provided by functions defined in [Functions and Operators 4.0] section 14 Processing maps and [Functions and Operators 4.0] section 15 Processing arrays, including functions used to read JSON to create maps and arrays, serialize maps and arrays to JSON, combine maps to create a new map, remove map entries to create a new map, iterate over the keys of a map, convert an array to create a sequence, combine arrays to form a new array, and iterate over arrays in various ways.

4.14.4 Method Calls

Changes in 4.0  

  1. A method call invokes a function held as the value of an entry in a map, supplying the map implicitly as the value of the first argument.  [Issue 2143  4 August 2025]

MethodCall::=PostfixExpr "=?>" NCNamePositionalArgumentList
PostfixExpr::=PrimaryExpr | FilterExpr | DynamicFunctionCall | LookupExpr | MethodCall | FilterExprAM
PositionalArgumentList::="(" PositionalArguments? ")"
PositionalArguments::=(Argument ++ ",")
Argument::=ExprSingle | ArgumentPlaceholder
ExprSingle::=ForExpr
| LetExpr
| QuantifiedExpr
| IfExpr
| OrExpr
ArgumentPlaceholder::="?"

A method call combines accessing a map M to look up an entry whose value is a function item F, and calling the function item F supplying the map M as the implicit value of the first argument.

For example, given the variable:

let $rectangle := {
  'height':    3,
  'width':     4,
  'area':      fn ($rect) { 
                  $rect?height × $rect?width 
               },
  'perimeter': fn ($rect) { 
                 2 × ($rect?height + $rect?width) 
               },
  'resize':    fn ($rect, $factor) { 
                  $rect 
                     => map:put('height', $rect?height × $factor)
                     => map:put('width', $rect?width × $factor)
               }
}

The method call $rectangle =?> area() returns 12, while the method call $rectangle =?> perimeter() returns 14, and $rectangle =?> resize(2) returns a map representing a rectangle with height = 6 and width = 8.

An arity-one function can also be written as a focus function:

let $rectangle := {
  'height': 3,
  'width': 4,
  'area': fn { ?height × ?width }
}

The expression M =?> N(X, Y, ...) is by definition equivalent to:

for $map as map(*) in M 
return ($map?N treat as function(*))
           ($map, X, Y, ...)
for $map as map(*) in M
let $f as function(*) := $map?N                 
return $f($map, X, Y, ...)

(where $map isand some$f are otherwise unused variable namenames).

The argument list in a method call must not include an argument placeholder; that is, the call must not be a partial function application ([err:XPST0003]).

Note:

Implicit in this definition are the following rules:

  • The value of M must be a sequence of zero or more maps;

  • Each of those maps must have an entry with the key N (as an instance of xs:string, xs:untypedAtomic, or xs:anyURI);

  • The value of that entry must be a single function item;

  • That function item must have an arity equal to one plus the number of supplied arguments, and the signature of the function must allow a map to be supplied as the first argument.

The error codes raised if these conditions are not satisfied are exactly the same as if the expanded code were used directly.

Note:

Although methods mimic some of the capability of object-oriented languages, the functionality is more limited:

  • There is no encapsulation: the entries in a map are all publicly exposed.

  • There is no class hierarchy, and no inheritance or overriding.

  • Methods within a map can be removed or replaced in the same way as any other entries in the map.

Note:

Methods can be useful when there is a need to write inline recursive functions. For example:

let $lib := {
  'product': fn($map as map(*), $in as xs:double*) {
    if (empty( $in ))
    then 1
    else head($in) × $map =?> product(tail($in))
  }
}
return $lib =?> product((1.2, 1.3, 1.4))

In an environment that supports XPath but not XQuery, this mechanism can be used to define all the functions that a particular XPath expression needs to invoke, and these functions can be mutually recursive.

Example: Chaining method calls

In the example above, $rectangle =?> area(), $rectangle is typically a single map, and area is the key of one of the entries in the map, the value of the entry being a function item that takes the map as its implicit first argument. The method call $rectangle =?> area() first performs a map lookup ($rectangle?area) to select the function item, and then calls the function item, supplying the containing map as the first (and in this case only) argument.

Such calls can be chained. For example, $rectangle =?> resize(2) returns a rectangle that is twice the size of the original, so $rectangle =?> resize(2) =?> area() returns the area of the enlarged rectangle.

Note:

Note how the resize function is implemented using map:put, which ensures that the map entries holding function items (area, perimeter, and resize, are automatically present and unchanged in the modified map.

This kind of chaining extends to the case where a method returns zero or more maps. For example, suppose that rectangles are nested, and that $rectangle =?> contents() delivers a sequence of zero or more rectangles. Then the expression $rectangle =?> area() - sum($rectangle =?> contents() =?> area()) returns the difference between the area of the containing rectangle and the total area of the contained rectangles. This works because the dynamic function call $rectangle =?> contents() =?> area() applies the area function in each of the maps in the sequence returned by the expression $rectangle =?> contents().