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

XQuery 4.0: An XML Query Language

W3C Editor's Draft 23 February 2026

This version:
https://qt4cg.org/specifications/xquery-40/
Most recent version of XQuery:
https://qt4cg.org/specifications/xquery-40/
Most recent Recommendation of XQuery:
https://www.w3.org/TR/2017/REC-xquery-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

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.

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


A XQuery 4.0 Grammar

A.5 Precedence Order (Non-Normative)

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

#OperatorAssociativity
1, (comma)eitherassociative
2FLWOR,some, every, switch,typeswitch,try,ifNA
3oreitherassociative
4andeitherassociative
5eq, ne, lt, le, gt, ge, =, !=, <, <=, >, >=, is, <<, >>NA
6otherwiseeitherassociative
7||left-to-rightassociative
8toNA
9+, - (binary)left-to-right
10*, div, idiv, modleft-to-right
11union, |eitherassociative
12intersect, exceptleft-to-right
13instance ofNA
14treat asNA
15castable asNA
16cast asNA
17=>, =!>left-to-right
18-, + (unary)right-to-left
19!left-to-right
20/, //left-to-right
21a[ 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.