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).
The grammar in A.1 EBNF normatively defines built-in precedence among the operators of XQuery. These operators are summarized here to make clear the order of their precedence from lowest to highest. The associativity column indicates the order in which operators of equal precedence in an expression are applied (further details below).
| # | Operator | Associativity |
|---|---|---|
| 1 | , (comma) | eitherassociative |
| 2 | FLWOR,some, every, switch,typeswitch,try,if | NA |
| 3 | or | eitherassociative |
| 4 | and | eitherassociative |
| 5 | eq, ne, lt, le, gt, ge, =, !=, <, <=, >, >=, is, <<, >> | NA |
| 6 | otherwise | eitherassociative |
| 7 | || | left-to-rightassociative |
| 8 | to | NA |
| 9 | +, - (binary) | left-to-right |
| 10 | *, div, idiv, mod | left-to-right |
| 11 | union, | | eitherassociative |
| 12 | intersect, except | left-to-right |
| 13 | instance of | NA |
| 14 | treat as | NA |
| 15 | castable as | NA |
| 16 | cast as | NA |
| 17 | =>, =!> | left-to-right |
| 18 | -, + (unary) | right-to-left |
| 19 | ! | left-to-right |
| 20 | /, // | left-to-right |
| 21 | a[ b], a?[b], a?b, a??b, a(b) | left-to-right |
| 22 | ? (unary) | NA |
In the "Associativity" column, "either" indicates that all the operators at that level have the associative property (i.e., (A op B) op C is equivalent to A op (B op C)), so their associativity is inconsequential. "NA" (not applicable) indicates that the EBNF does not allow an expression that directly contains multiple operators from that precedence level, so the question of their associativity does not arise.
Entries in the Associativity column have the following meaning (where the symbol ⊙ represents any operator):
associative means that the order of evaluation is immaterial: for example a, b, c can be evaluated either as (a, b), c or as a, (b, c), producing the same result either way.
NA indicates that it is not possible to write an expression of the form a ⊙ b ⊙ c for this particular operator: for example, the grammar does not allow a = b = c.
left-to-right means that for expressions using these operators, a ⊙ b ⊙ c is evaluated as (a ⊙ b) ⊙ c. As a special case, the operators + and * are associative provided they are not mixed with other operators of the same precedence.
right-to-left is used only for unary operators, and indicates that ⊙ ⊙ a is evaluated as ⊙ (⊙ a)
These rules do not constrain the order in which the operands of an expression are evaluated (which might affect error behavior). See also 2.4 Error Handling.
Note:
Parentheses can be used to override the operator precedence in the usual way. Square brackets in an expression such as A[B]A?[B]or serve two roles: they act as an operator causing B to be evaluated once for each item in the value of A, and they act as parentheses enclosing the expression B.
Curly braces in an expression such as validate { E } or ordered { E } perform a similar bracketing role to the parentheses in a function call, but with the difference in most cases that E is an Expr rather than ExprSingle, meaning that it can use the comma operator.