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


E Error Conditions

Note:

Historically, codes starting "XP" were for XPath errors, "XQ" for XQuery errors. This was followed by "ST" for static errors, "TY" for type errors, or "DY" for dynamic errors. Over time, some errors have been reclassified, but the error codes have been retained for compatibility reasons. Applications should therefore attach no significance to the choice of codes.

err:XPST0001

It is a static error if analysis of an expression relies on some component of the static context that is absentDM.

err:XPDY0002

It is a type error if evaluation of an expression relies on some part of the dynamic context that is absentDM.

Note:

In version 4.0 this has been reclassified as a type error rather than a dynamic error. This change allows a processor to report the error during static analysis where possible; for example if the body of a user-defined function is written as fn($x) { @code }. The error code is prefixed XPDY rather than XPTY for backwards compatibility reasons.

err:XPST0003

It is a static error if an expression is not a valid instance of the grammar defined in A.1 EBNF.

err:XPTY0004

It is a type error if, during the static analysis phase, an expression is found to have a static type that is not appropriate for the context in which the expression occurs, or during the dynamic evaluation phase, the dynamic type of a value does not match a required type as specified by the matching rules in 3.1.2 Sequence Type Matching.

err:XPTY0006

During the analysis phase, an expression is classified as implausible if the inferred static typeS and the required type R are substantively disjoint; more specifically, if neither of the types is a subtype of the other, and if the only values that are instances of both types are one or more of: the empty sequence, the empty map, and the empty array.

err:XPST0008

It is a static error if an expression refers to an element name, attribute name, schema type name, or variable name that is not defined in the static context, except for an ElementName in an ElementTest or an AttributeName in an AttributeTest.

err:XQST0009

An implementation that does not support the Schema Aware Feature must raise a static error if a Prolog contains a schema import.

err:XQST0012

It is a static error if the set of definitions contained in all schemas imported by a Prolog do not satisfy the conditions for schema validity specified in Sections 3 and 5 of Part 1 of [XML Schema 1.0] or [XML Schema 1.1].

err:XQST0013

It is a static error if an implementation recognizes a pragma but determines that its content is invalid.

err:XPST0017

It is a static error if the expanded QName and number of arguments in a static function call do not match the name and arity range of a function definition in the static context, or if an argument keyword in the function call does not match a parameter name in that function definition, or if two arguments in the function call bind to the same parameter in the function definition.

err:XPTY0018

It is a type error if the result of a path operator contains both nodes and non-nodes.

err:XPTY0019

It is a type error if E1 in a path expression E1/E2 does not evaluate to a sequence of nodes.

err:XPTY0020

It is a type error if, in an axis step, the context item is not a node.

err:XPST0021

It is a static error if two fields in a record declaration have the same name.

err:XQST0022

It is a static error if a namespace declaration attributecontains an EnclosedExpr.

err:XPST0023

It is a static error if a recursive record type cannot be instantiated (typically because it contains a self-reference that is neither optional nor emptiable). Processors are not required to detect this error.

err:XQTY0024

It is a type error if the content sequence in an element constructor contains an attribute node following a node that is not an attribute node.

err:XQDY0025

It is a dynamic error if any attribute of a constructed element does not have a name that is distinct from the names of all other attributes of the constructed element.

err:XQDY0026

It is a dynamic error if the result of the content expression of a computed processing instruction constructor contains the string "?>".

err:XQDY0027

In a validate expression, it is a dynamic error if the root element information item in the PSVI resulting from validation does not have the expected validity property: valid if validation mode is strict, or either valid or notKnown if validation mode is lax.

err:XQTY0030

It is a type error if the argument of a validate expression does not evaluate to exactly one document or element node.

err:XQST0031

It is a static error if the version number specified in a version declaration is not supported by the implementation.

err:XQST0032

A static error is raised if a Prolog contains more than one base URI declaration.

err:XQST0033

It is a static error if a module contains multiple bindings for the same namespace prefix.

err:XQST0034

It is a static error if multiple functions declared or imported by a module have the same expanded QName and overlapping arity ranges (the arity range of a function declaration is M to M+N, where M is the number of required parameters and N is the number of optional parameters).

err:XQST0035

It is a static error to import two schema components that both define the same name in the same symbol space and in the same scope.

err:XQST0038

It is a static error if a Prolog contains more than one default collation declaration, or the value specified by a default collation declaration is not present in statically known collations.

err:XQST0039

It is a static error for a function declaration oran inline function expression to have more than one parameter with the same name.

err:XQST0040

It is a static error if the attributes specified by a direct element constructor do not have distinct expanded QNames.

err:XQDY0041

It is a dynamic error if the value of the name expression in a computed processing instruction constructor cannot be cast to the type xs:NCName.

err:XQDY0044

It is a dynamic error the node-name of a node constructed by a computed attribute constructor has any of the following properties:

  • Its namespace prefix is xmlns.

  • It has no namespace prefix and its local name is xmlns.

  • Its namespace URI is http://www.w3.org/2000/xmlns/.

  • Its namespace prefix is xml and its namespace URI is not http://www.w3.org/XML/1998/namespace.

  • Its namespace prefix is other than xml and its namespace URI is http://www.w3.org/XML/1998/namespace.

err:XQST0045

It is a static error if the name of a variable annotation, a function annotation, or the function name in a function declaration is in a reserved namespace.

err:XQST0046

An implementation MAY raise a static error if the value of a URILiteral or a BracedURILiteral is of nonzero length and is neither an absolute URI nor a relative URI.

err:XQST0047

It is a static error if multiple module imports in the same specify the same target namespace.

err:XQST0048

It is a static error if a function, variable, or item type declared in a library module is not in the target namespace of the library module.

err:XQST0049

It is a static error if two or more variables declared or imported by a module have equal expanded QNames (as defined by the eq operator.)

err:XPDY0050

It is a type error if the dynamic type of the operand of a treat expression does not match the sequence type designated by the treat expression.

Note:

This error might also be raised by a path expression beginning with / or // if the context node is not in a tree that is rooted at a document node. This is because a leading / or // in a path expression is an abbreviation for an initial step that includes the clause treat as document-node().

err:XPST0051

It is a static error if an expanded QName used as an ItemType in a is not defined in the static context either as a named item type in the in-scope named item types, or as a generalized atomic type in the in-scope schema type.

err:XQST0052

The type named in a cast or castable expression must be the name of a type defined in the in-scope schema types, and the type must be simple.

err:XQDY0054

It is a dynamic error if a cycle is encountered in the definition of a module’s dynamic context components, for example because of a cycle in variable declarations.

err:XQST0055

It is a static error if a Prolog contains more than one copy-namespaces declaration.

err:XQST0057

It is a static error if a schema import binds a namespace prefix but does not specify a target namespace other than a zero-length string.

err:XQST0058

It is a static error if multiple schema imports specify the same target namespace.

err:XQST0059

It is a static error if an implementation is unable to process a schema or module import by finding a schema or module with the specified target namespace.

err:XQST0060

It is a static error if the name of a function in a function declaration is not in a namespace (expanded QName has a null namespace URI).

err:XQDY0061

It is a dynamic error if the operand of a validate expression is a document node whose children do not consist of exactly one element node and zero or more comment and processing instruction nodes, in any order.

err:XQDY0064

It is a dynamic error if the value of the name expression in a computed processing instruction constructor is equal to XML (in any combination of upper and lower case).

err:XQST0065

A static error is raised if a Prolog contains more than one ordering mode declaration.

err:XQST0066

A static error is raised if a Prolog contains more than one default element/type namespace declaration, or more than one default function namespace declaration.

err:XQST0067

A static error is raised if a Prolog contains more than one construction declaration.

err:XQST0068

A static error is raised if a Prolog contains more than one boundary-space declaration.

err:XQST0069

A static error is raised if a Prolog contains more than one empty order declaration.

err:XQST0070

A namespace declaration must not define a binding for the prefix xml or xmlns, nor for the URI http://www.w3.org/XML/1998/namespace or http://www.w3.org/2000/xmlns/.

The same rule applies to namespace declaration attributes (see 4.12.1.2 Namespace Declaration Attributes), except that in this case it is permitted to provide a redundant namespace declaration attribute in the form xmlns:xml="http://www.w3.org/XML/1998/namespace".

err:XQST0071

A static error is raised if the namespace declaration attributes of a direct element constructor do not have distinct names.

err:XQDY0072

It is a dynamic error if the result of the content expression of a computed comment constructor contains two adjacent hyphens or ends with a hyphen.

err:XQDY0074

It is a dynamic error if the value of the name expression in a computed element or attribute constructor cannot be converted to an expanded QName (for example, because it contains a namespace prefix not found in statically known namespaces.)

err:XQST0075

An implementation that does not support the Schema Aware Feature must raise a static error if it encounters a validate expression.

err:XQST0076

It is a static error if a collation subclause in an order byor group by clause of a FLWOR expression does not identify a collation that is present in statically known collations.

err:XQST0079

It is a static error if an extension expression contains neither a pragma that is recognized by the implementation nor an expression enclosed in curly braces.

err:XPST0080

It is a static error if the target type of a cast or castable expression is xs:NOTATION, xs:anySimpleType, or xs:anyAtomicType.

err:XPST0081

It is a static error if a QName used in a query contains a namespace prefix that cannot be expanded into a namespace URI by using the statically known namespaces.

err:XQDY0084

It is a dynamic error if the element validated by a validate statement does not have a top-level element declaration in the in-scope element declarations, if validation mode is strict.

err:XQST0085

It is a static error if the namespace URI in a namespace declaration attribute is a zero-length string, and the implementation does not support [XML Names 1.1].

err:XQTY0086

It is a type error if the typed value of a copied element or attribute node is namespace-sensitive when construction mode is preserve and copy-namespaces mode is no-preserve.

err:XQST0087

It is a static error if the encoding specified in a Version Declaration does not conform to the definition of EncName specified in [XML 1.0].

err:XQST0088

It is a static error if the literal that specifies the target namespace in a module import or a module declaration is of zero length.

err:XQST0089

It is a static error if a variable bound in a for or window clause of a FLWOR expression, and its associated positional variable, do not have distinct names (expanded QNames).

err:XQST0090

It is a static error if a character reference does not identify a valid character in the version of XML that is in use.

err:XQDY0091

An implementation MAY raise a dynamic error if an xml:id error, as defined in [XML ID], is encountered during construction of an attribute named xml:id.

err:XQDY0092

An implementation MAY raise a dynamic error if a constructed attribute named xml:space has a value other than preserve or default.

err:XQST0094

The name of each grouping variable must be equal (by the eq operator on expanded QNames) to the name of a variable in the input tuple stream.

err:XQDY0096

It is a dynamic errorif the node-name of a node constructed by a computed element constructor has any of the following properties:

  • Its namespace prefix is xmlns.

  • Its namespace URI is http://www.w3.org/2000/xmlns/.

  • Its namespace prefix is xml and its namespace URI is not http://www.w3.org/XML/1998/namespace.

  • Its namespace prefix is other than xml and its namespace URI is http://www.w3.org/XML/1998/namespace.

err:XQST0097

It is a static error for a decimal-format to specify a value that is not valid for a given property, as described in statically known decimal formats

err:XQST0098

It is a static error if, for any named or unnamed decimal format, the properties representing characters used in a picture string do not each have distinct values. The following properties represent characters used in a picture string: decimal-separator, exponent-separator, grouping-separator, percent, per-mille, the family of ten decimal digits starting with zero-digit, digit, and pattern-separator.

err:XQST0099

No module may contain more than one ContextItemDecl.

err:XQDY0101

An error is raised if a computed namespace constructor attempts to do any of the following:

  • Bind the prefix xml to some namespace URI other than http://www.w3.org/XML/1998/namespace.

  • Bind a prefix other than xml to the namespace URI http://www.w3.org/XML/1998/namespace.

  • Bind the prefix xmlns to any namespace URI.

  • Bind a prefix to the namespace URI http://www.w3.org/2000/xmlns/.

  • Bind any prefix (including the empty prefix) to a zero-length namespace URI.

err:XQDY0102

In an element constructor, if two or more namespace bindings in the in-scope bindings would have the same prefix, then an error is raised if they have different URIs; if they would have the same prefix and URI, duplicate bindings are ignored.

If the name of an element in an element constructor is in no namespace, creating a default namespace for that element using a computed namespace constructor is an error.

err:XQST0103

All variables in a window clause must have distinct names.

err:XQST0104

A TypeName that is specified in a validate expression must be found in the in-scope schema definitions

err:XQTY0105

It is a type error if the content sequence in an element constructor contains a function .

err:XQST0106

It is a static error if a function declaration contains both a %private and a %public annotation.

err:XQST0108

It is a static error if an output declaration occurs in a library module.

err:XQST0109

It is a static error if the local name of an output declaration in the http://www.w3.org/2010/xslt-xquery-serialization namespace is not one of the serialization parameter names listed in B.1 Static Context Components, or if the name of an output declaration is use-character-maps.

err:XQST0110

It is a static error if the same serialization parameter is used more than once in an output declaration.

err:XQST0111

It is a static error for a query prolog to contain two decimal formats with the same name, or to contain two default decimal formats.

err:XQST0113

Specifying a VarValue or VarDefaultValue for a context item declaration in a library module is a static error.

err:XQST0114

It is a static error for a decimal format declaration to define the same property more than once.

err:XQST0115

It is a static error if the document specified by the option Q{http://www.w3.org/2010/xslt-xquery-serialization}parameter-document raises a serialization error.

err:XQST0116

It is a static errorif a variable declaration contains both a %private and a %public annotation, more than one %private annotation, or more than one %public annotation.

err:XPTY0117

When applying the coercion rules, if an item is of type xs:untypedAtomic and the expected type is namespace-sensitive, a type error [err:XPTY0117] is raised.

err:XQST0118

In a direct element constructor, the name used in the end tag must exactly match the name used in the corresponding start tag, including its prefix or absence of a prefix.

err:XQST0119

It is a static error if the implementation is not able to process the value of an output:parameter-document declaration to produce an XDM instance.

err:XQST0125

It is a static error if an inline function expression is annotated as %public or %private.

err:XPDY0130

An implementation-dependent limit has been exceeded.

err:XQST0134

The namespace axis is not supported.

err:XQDY0137

No two keys in a map may have the same key value.

err:XQST0140

It is a static error if a named item type declaration is recursive, unless it satisfies the conditions defined in 3.2.8.3.1 Recursive Record Types.

err:XPTY0141

In a forclause, when the keyword member is present, the value of the binding collection must be a single array; and when either or both of the keywords key and value are present, the value of the binding collection must be a single map.

err:XPTY0144

During the analysis phase, an axis step is classified as implausible if the combination of the inferred context item type, the choice of axis, and the supplied node test, is such that the axis step will always return an empty sequence.

err:XPTY0145

During the analysis phase, a unary or postfix lookup expression is classified as implausible if the combination of the inferred type of the left-hand operand (or the context item type in the case of a unary expression) and the choice of key specifier is such that the lookup expression will always return an empty sequence.

err:XQST0146

It is a static error if two or more item types declared or imported by a module have equal expanded QNames (as defined by the eq operator.)

err:XQST0148

It is a static error if an optional parameter in a function declaration is followed by a parameter that does not have a default value.

err:XQST0149

It is a static error if the schemas imported by different modules of a query are not compatible as defined in [XDM 4.0] section 4.1.2 Schema Consistency.

err:XQST0151

It is a static error if a node name supplied as a string literal in a computed element or attribute constructor does not take the form of an EQName.

err:XPST0152

It is a static error if a key type named in a is not a generalized atomic type.

err:XQTY0153

The expression of a finally clause must return an empty sequence.

err:XPST0154

It is a static error if a URIQualifiedName includes a prefix but no URI.