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.
Copyright © 2000 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and document use rules apply.
XML is a versatile markup language, capable of labeling the information content of diverse data sources, including structured and semi-structured documents, relational databases, and object repositories. A query language that uses the structure of XML intelligently can express queries across all these kinds of data, whether physically stored in XML or viewed as XML via middleware. This specification describes a query language called XQuery, which is designed to be broadly applicable across many types of XML data sources.
A list of changes made since XQuery 3.1 can be found in J Change Log.
This is a draft prepared by the QT4CG (officially registered in W3C as the XSLT Extensions Community Group). Comments are invited.
The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).
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 XQuery 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 XQuery 4.0 Grammar].
[Definition: A query consists of one or more modules.] If a query is executable, one of its modules has a Query Body containing an expression whose value is the result of the query. An expression is represented in the XQuery grammar by the symbol Expr.
Expr | ::= | (ExprSingle ++ ",") |
ExprSingle | ::= | FLWORExpr |
ExprSingle | ::= | FLWORExpr |
FLWORExpr | ::= | InitialClauseIntermediateClause* ReturnClause |
QuantifiedExpr | ::= | ("some" | "every") (QuantifierBinding ++ ",") "satisfies" ExprSingle |
SwitchExpr | ::= | "switch" SwitchComparand (SwitchCases | BracedSwitchCases) |
TypeswitchExpr | ::= | "typeswitch" "(" Expr ")" (TypeswitchCases | BracedTypeswitchCases) |
IfExpr | ::= | "if" "(" Expr ")" (UnbracedActions | BracedAction) |
TryCatchExpr | ::= | TryClause ((CatchClause+ FinallyClause?) | FinallyClause) |
OrExpr | ::= | AndExpr ("or" AndExpr)* |
The XQuery 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 FLWORExpr,QuantifiedExpr, SwitchExpr, TypeswitchExpr, IfExpr, TryCatchExpr, and OrExpr. Each of these expressions is described in a separate section of this document.
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 XQuery 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 XQuery 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 XQuery 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.
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]
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.
Note:
Methods are often useful in conjunction with named record types: see 5.20.3 Using Methods in Records.
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().
A record type representing a rectangle containing nested rectangles might be defined in XQuery 4.0 like this:
declare record my:rectangle (
height as xs:double,
width as xs:double,
children as my:rectangle*
:= (),
area as fn(my:rectangle) as xs:double
:= fn{?height × ?width},
resize as fn(my:rectangle, xs:double) as my:rectangle
:= fn($rect, $factor) {
$rect => map:put('height', $rect?height × $factor)
=> map:put('width', $rect?width × $factor)
=> map:put('children', $rect?children =?> resize($factor))
},
contents as fn(my:rectangle) as my:rectangle*
:= fn{?children}
);