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.
XQuery provides a versatile expression called a FLWOR expression that may contain multiple clauses. The FLWOR expression can be used for many purposes, including iterating over sequences, joining multiple documents, and performing grouping and aggregation. The name FLWOR, pronounced "flower", is suggested by the keywords for, let, where, order by, and return, which introduce some of the clauses used in FLWOR expressions (but this is not a complete list of such clauses.)
The overall syntax of a FLWOR expression is shown here, and relevant parts of the syntax are expanded in subsequent sections.
FLWORExpr | ::= | InitialClauseIntermediateClause* ReturnClause |
InitialClause | ::= | ForClause | LetClause | WindowClause |
ForClause | ::= | "for" (ForBinding ++ ",") |
LetClause | ::= | "let" (LetBinding ++ ",") |
WindowClause | ::= | "for" (TumblingWindowClause | SlidingWindowClause) |
IntermediateClause | ::= | InitialClause | WhereClause | WhileClause | GroupByClause | OrderByClause | CountClause |
WhereClause | ::= | "where" ExprSingle |
WhileClause | ::= | "while" ExprSingle |
GroupByClause | ::= | "group" "by" (GroupingSpec ++ ",") |
OrderByClause | ::= | "stable"? "order" "by" (OrderSpec ++ ",") |
CountClause | ::= | "count" VarName |
ReturnClause | ::= | "return" ExprSingle |
The semantics of FLWOR expressions are based on a concept called a tuple stream. [Definition: A tuple stream is an ordered sequence of zero or more tuples.] [Definition: A tuple is a set of zero or more named variables, each of which is bound to a value that is an XDM instance.] Each tuple stream is homogeneous in the sense that all its tuples contain variables with the same names and the same static types. The following example illustrates a tuple stream consisting of four tuples, each containing three variables named $x, $y, and $z:
($x = 1003, $y = "Fred", $z = <age>21</age>) ($x = 1017, $y = "Mary", $z = <age>35</age>) ($x = 1020, $y = "Bill", $z = <age>18</age>) ($x = 1024, $y = "John", $z = <age>29</age>)
Note:
In this section, tuple streams are represented as shown in the above example. Each tuple is on a separate line and is enclosed in parentheses, and the variable bindings inside each tuple are separated by commas. This notation does not represent XQuery syntax, but is simply a representation of a tuple stream for the purpose of defining the semantics of FLWOR expressions.
Tuples and tuple streams are not part of the data model. They exist only as conceptual intermediate results during the processing of a FLWOR expression.
Conceptually, the first clause generates a tuple stream. Each clause between the first clause and the return clause takes the tuple stream generated by the previous clause as input and generates a (possibly different) tuple stream as output. The return clause takes a tuple stream as input and, for each tuple in this tuple stream, generates an XDM instance; the final result of the FLWOR expression is the ordered concatenation of these XDM instances.
The initial clause in a FLWOR expression may be a for, let, or window clause. Intermediate clauses may be for, let, window, count, where, group by, or order by clauses. These intermediate clauses may be repeated as many times as desired, in any order. The final clause of the FLWOR expression must be a return clause. The semantics of the various clauses are described in the following sections.
The value bound to a variable in a let clause is now converted to the declared type by applying the coercion rules. [Issue 189 PR 254 29 November 2022]
Sequences, arrays, and maps can be destructured in a let clause to extract their components into multiple variables. [Issue 37 PR 2055 17 June 2025]
LetClause | ::= | "let" (LetBinding ++ ",") |
LetBinding | ::= | LetValueBinding | LetSequenceBinding | LetArrayBinding | LetMapBinding |
LetValueBinding | ::= | VarNameAndType ":=" ExprSingle |
VarNameAndType | ::= | "$" EQNameTypeDeclaration? |
EQName | ::= | QName | URIQualifiedName |
TypeDeclaration | ::= | "as" SequenceType |
SequenceType | ::= | ("empty-sequence" "(" ")") |
ExprSingle | ::= | FLWORExpr |
LetSequenceBinding | ::= | "$" "(" (VarNameAndType ++ ",") ")" TypeDeclaration? ":=" ExprSingle |
LetArrayBinding | ::= | "$" "[" (VarNameAndType ++ ",") "]" TypeDeclaration? ":=" ExprSingle |
LetMapBinding | ::= | "$" "{" (VarNameAndType ++ ",") "}" TypeDeclaration? ":=" ExprSingle |
The purpose of a let clause is to bind values to one or more variables. Each variable is bound to the result of evaluating an expression.
If a let clause declares multiple variables separated by commas, it is semantically equivalent to multiple let clauses, each containing a single variable. For example, the clause
let $x := $expr1, $y := $expr2
is semantically equivalent to the following sequence of clauses:
let $x := $expr1 let $y := $expr2
After performing this expansion, the effect of a let clause is as follows:
If the let expression uses multiple variables, it is first expanded to a set of nested let expressions, each of which uses only one variable. Specifically, any separating comma is replaced by let.
In a LetValueBinding such as let $V as T := EXPR:
The variable V is declared as a range variable.
The sequence type T is called the declared type. If there is no declared type, then item()* is assumed.
The expression EXPR is evaluated, and its value is converted to the declared type by applying the coercion rules. The resulting value forms the binding sequence for the range variable.
In a LetSequenceBinding such as let $( $A1 as T1, $A2 as T2, ... , $An as Tn ) as ST := EXPR:
The sequence type ST is called the declared sequence type. If there is no declared sequence type, then item()* is assumed.
The expression EXPR is evaluated, and its value is converted to the declared sequence type ST by applying the coercion rules. Call the resulting (coerced) value V.
Each variable Ai (for i in 1 to n-1) is effectively replaced by a LetValueBinding of the form let Ai as Ti := items-at(V, i). That is, a range variable named Ai is declared, whose binding sequence is the item V[ i ], after coercion to the type Ti if specified. If Ti is absent, no further coercion takes place (the default is effectively item()?).
The last variable An is effectively replaced by a LetValueBinding of the form let An as Tn := subsequence(V, n). That is, the last variable is bound to the rest of the binding sequence (or to the empty sequence if the binding sequence has fewer items than the number of variables).
Note:
For any variable Ai, including the last, if i exceeds the length of the sequence V, then Ai is bound to an empty sequence. This will cause a type error if type Ti does not permit an empty sequence.
Note:
It is permissible to bind several variables with the same name; all but the last are occluded. A useful convention is therefore to bind items in the sequence that are of no interest to the variable $_: for example let $( $_, $_, $x ) := EXPR effectively binds $x to the subsequence starting at the third item, while causing the first two items to be ignored.
The expression:
let $( $a, $b as xs:integer, $local:c ) := (2, 4, 6) return $a + $b + $local:c
is expanded to:
let $temp := (2, 4, 6) let $a := fn:items-at($temp, 1) let $b as xs:integer := fn:items-at($temp, 2) let $local:c := fn:subsequence($temp, 3) return $a + $b + $local:c
where $temp is some variable name that is otherwise unused.
Consider the element $E := <e A="p q r" B="x y z"/>, where $E has been validated against a schema that defines both attributes A and B as being lists of strings.
Then the expression:
Then the expression:
let $( $a, $b ) := $E/(@A, @B)binds $a to the attribute node A="p q r", and $b to the attribute node B="x y z".
If type declarations are added to the individual variables, thus:
let $( $a as xs:string, $b as xs:string ) := $E/(@A, @B)then the attributes are atomized, so that $a is bound to the string "p q r", and $b to the string "x y z".
The same result is achieved if a type declaration is added for the sequence as a whole:
let $( $a, $b ) as xs:string* := $E/(@A, @B)Now consider the case where the same element $E is validated against a schema that defines both attributes as lists of strings.
Then the raw expression, without type declarations:
let $( $a, $b ) := $E/(@A, @B)still binds $a and $b to the two attribute nodes (the only difference being that the typed value of each attribute is now a sequence of three strings).
If type declarations are added to the individual variables, thus:
let $( $a as xs:string*, $b as xs:string* ) := $E/(@A, @B)
binds $a to the sequence ("p", "q", "r") and $b to the sequence ("x", "y", "z"). The evaluation of the expression $E/(@A, @B) returns a sequence of two attribute nodes; the value of $a is formed by atomizing the first of these nodes, while $b is formed by atomizing the second.
then $a is bound to the result of atomizing the first attribute (that is, to the sequence of three strings ("p", "q", "r")), while $b is bound to the result of atomizing the second attribute (that is, to the sequence of three strings ("x", "y", "z")).
If however, a type declaration is added for the sequence as a whole, namely:
let $( $a, $b ) as xs:string* := $E/(@A, @B)then the binding sequence is a sequence of six strings, ("p", "q", "r", "x", "y", "z"). The variable $a is bound to the first string, "p", while $b is bound to the remaining five strings, ("q", "r", "x", "y", "z").
Consider transforming the string "Nf3 Nf6 c4 g6 Nc3 Bg7 d4 O-O Bf4 d5" (notation for the start of a chess game) to the form (["Nf3", "Nf6",], ["c4", "g6"], [ "Nc3", "Bg7"], [ "d4", "O-O"], ["Bf4", "d5"]). This can be achieved as follows:
declare function local:grouped-moves($moves) {
if (exists($moves)) {
let $($m1, $m2, $rest) := $moves
return [$m1, $m2], local:grouped-moves($rest)
}
};
local:grouped-moves(tokenize("Nf3 Nf6 c4 g6 Nc3 Bg7 d4 O-O Bf4 d5"))declare function local:grouped-moves($moves) {
if (exists($moves)) {
let $($m1, $m2, $rest) := $moves
return [$m1, $m2], local:grouped-moves($rest)
}
};
local:grouped-moves(tokenize("Nf3 Nf6 c4 g6 Nc3 Bg7 d4 O-O Bf4 d5"))In a LetArrayBinding such as let $[ $A1 as T1, $A2 as T2, ... , $An as Tn ] as AT := EXPR:
The sequence type AT is called the declared array type. If there is no declared array type, then array(*) is assumed.
The expression EXPR is evaluated, and its value is converted to the declared array type AT by applying the coercion rules. A type error [err:XPTY0004] is raised if the result is not a singleton array. Call the resulting (coerced) value V.
Each variable Ai (for i in 1 to n) is effectively replaced by a LetValueBinding of the form let Ai as Ti := array:get(V, i). That is, a range variable named Ai is declared, whose binding sequence is the array member V ? i, after coercion to the type Ti if specified. If Ti is absent, no further coercion takes place (the default is effectively item()*).
Note:
If i exceeds the length of the array V, then an error [err:FOAR0001]FO is raised.
Note:
It is permissible to bind several variables with the same name; all but the last are occluded. A useful convention is therefore to bind items in the sequence that are of no interest to the variable $_: for example let $( $_, $_, $x ) := EXPR effectively binds $x to the third item in the sequence and causes the first two items to be ignored.
The expression:
let $[ $a, $b as xs:integer, $local:c ] := [ 2, 4, 6 ] return $a + $b + $local:c
is expanded to:
let $temp := [ 2, 4, 6 ] let $a := array:get($temp, 1, ()) let $b as xs:integer := array:get($temp, 2) let $local:c := array:get($temp, 3, ()) return $a + $b + $local:c
where $temp is some variable name that is otherwise unused.
In a LetMapBinding such as let ${ $A1 as T1, $A2 as T2, ... , $An as Tn } as MT := EXPR:
The sequence type MT is called the declared map type. If there is no declared map type, then map(*) is assumed.
The expression EXPR is evaluated, and its value is converted to the declared map type MT by applying the coercion rules. A type error [err:XPTY0004] is raised if the result is not a singleton map. Call the resulting (coerced) value V.
Each variable Ai (for i in 1 to n) is effectively replaced by a LetValueBinding of the form let Ai as Ti := map:get(V, "Ni", ()), where Ni is the local part of the name of the variable Ai. That is, a range variable named Ai is declared, whose binding sequence is the value of the map entry in V whose key is an xs:string (or xs:anyURI or xs:untypedAtomic) equal to the local part of the variable name, after coercion to the type Ti if specified. If Ti is absent, no further coercion takes place (the default is effectively item()*).
Note:
If there is no entry in the map with a key corresponding to the variable name, then the variable Ai is bound to an empty sequence. This will cause a type error if type Ti does not permit an empty sequence.
Note:
It is not possible to use this mechanism to bind variables to values in a map unless the keys in the map are strings in the form of NCNames.
The expression:
let ${ $a, $b as xs:integer, $local:c } := { "a": 2, "b": 4, "c": 6, "d": 8 }
return $a + $b + $local:cis expanded to:
let $temp := { "a": 2, "b": 4, "c": 6 }
let $a := map:get($temp, "a", ())
let $b as xs:integer := map:get($temp, "b", ())
let $local:c := map:get($temp, "c", ())
return $a + $b + $local:cwhere $temp is some variable name that is otherwise unused.
The effect of the LetClause is to add one or more variable bindings to the tuple stream. Specifically, each range variable declared within the LetClause is bound to its corresponding binding sequence, and the resulting variable binding is added to the current tuple, replacing any existing variable binding with the same variable name.
If the LetClause is the initial clause in a FLWOR expression, it creates an initial tuple for the tuple stream, containing these variable bindings. This tuple stream serves as input to the next clause in the FLWOR expression.
If the LetClause is an intermediate clause in a FLWOR expression, it adds the relevant variable bindings to each tuple in the input tuple stream. The resulting tuples become the output tuple stream of the let clause.
The number of tuples in the output tuple stream of an intermediate let clause is the same as the number of tuples in the input tuple stream. The number of variable bindings in the output tuples is always greater than the number of variable bindings in the input tuples, unless the input tuples already contain bindings for every variable binding created by the LetClause; in this case, the new binding for any given variable name occludes (replaces) an earlier binding for that variable name, and the number of bindings is unchanged.
The semantics of type declarations are further defined in 4.13.1 Variable Bindings.
The following code fragment illustrates how a for clause and a let clause can be used together. The for clause produces an initial tuple stream containing a binding for variable $d to each department number found in a given input document. The let clause adds an additional binding to each tuple, binding variable $e to a sequence of employees whose department number matches the value of $d in that tuple.
for $d in doc("depts.xml")/depts/deptno
let $e := doc("emps.xml")/emps/emp[deptno eq $d]