As increasing amounts of information are stored, exchanged, and presented using XML, the ability to intelligently query XML data sources becomes increasingly important. One of the great strengths of XML is its flexibility in representing many different kinds of information from diverse sources. To exploit this flexibility, an XML query language must provide features for retrieving and interpreting information from these diverse sources.
As increasing amounts of JSON are used for lightweight data-exchange, an XML query language for Web data needs to handle JSON as well as XML and HTML.
XQuery is designed to meet the requirements identified by the W3C XML Query Working Group [XQuery 3.1 Requirements]. It is designed to be a language in which queries are concise and easily understood. It is also flexible enough to query a broad spectrum of XML information sources, including both databases and documents. The Query Working Group has identified a requirement for both a non-XML query syntax and an XML-based query syntax. XQuery is designed to meet the first of these requirements. XQuery is derived from an XML query language called Quilt [Quilt], which in turn borrowed features from several other languages, including XPath 1.0 [XML Path Language (XPath) Version 1.0], XQL [XQL], XML-QL [XML-QL], SQL [SQL], and OQL [ODMG].
[Definition: XQuery 4.0 operates on the abstract, logical structure of an XML document or JSON object, rather than its surface syntax. This logical structure, known as the data model, is defined in [XQuery and XPath Data Model (XDM) 3.1].]
XQuery 4.0 is an extension of XPath 4.0. In general, any expression that is syntactically valid and executes successfully in both XPath 4.0 and XQuery 4.0 will return the same result in both languages. There are a few exceptions to this rule:
Because XQuery expands
predefined entity
references and character
references
and XPath does not, expressions containing these produce different
results in the two languages. For instance, the value of the string literal
"&"
is &
in XQuery,
and &
in XPath. (XPath is often embedded in other
languages, which may expand predefined entity references or character references
before the XPath expression is evaluated.)
If XPath 1.0 compatibility mode is enabled, XPath behaves differently from XQuery in a number of ways, which are discussed in [XML Path Language (XPath) Version 3.1].
Because these languages are so closely related, their grammars and language descriptions are generated from a common source to ensure consistency, and the editors of these specifications work together closely.
XQuery 4.0 also depends on and is closely related to the following specifications:
[XQuery and XPath Data Model (XDM) 3.1] defines the data model that underlies all XQuery 4.0 expressions.
The type system of XQuery 4.0 is based on XML Schema. It is implementation-defined whether the type system is based on [XML Schema 1.0] or [XML Schema 1.1].
The built-in function library and the operators supported by XQuery 4.0 are defined in [XQuery and XPath Functions and Operators 4.0].
XQuery also has an XML-based syntax, which is described in [XQueryX 3.1].
[Definition: An XQuery 3.1 Processor processes a query according to the XQuery 3.1 specification. ] [Definition: An XQuery 3.0 Processor processes a query according to the XQuery 3.0 specification. ] [Definition: An XQuery 1.0 Processor processes a query according to the XQuery 1.0 specification. ]
This document specifies a grammar for XQuery 4.0, using the same basic EBNF notation used in [XML 1.0]. Unless otherwise noted (see A.2 Lexical structure), whitespace is not significant in queries. Grammar productions are introduced together with the features that they describe, and a complete grammar is also presented in the appendix [A XQuery 4.0 Grammar]. The appendix is the normative version.
In the grammar productions in this document, named symbols are underlined and literal text is enclosed in double quotes. For example, the following productions describe the syntax of a static function call:
[156] | FunctionCall |
::= |
EQName
ArgumentList
|
/* xgc: reserved-function-names */ |
/* gn: parens */ | ||||
[136] | ArgumentList |
::= | "(" ((PositionalArguments ("," KeywordArguments)?) | KeywordArguments)? ")" |
The productions should be read as follows: A function call consists of an EQName followed by an ArgumentList. The argument list consists of an opening parenthesis, an optional list of one or more arguments (separated by commas), and a closing parenthesis.
This document normatively defines the static and dynamic semantics of XQuery 4.0. In this document, examples and material labeled as "Note" are provided for explanatory purposes and are not normative.
Certain aspects of language processing are described in this specification as implementation-defined or implementation-dependent.
[Definition: Implementation-defined indicates an aspect that may differ between implementations, but must be specified by the implementor for each particular implementation.]
[Definition: Implementation-dependent indicates an aspect that may differ between implementations, is not specified by this or any W3C specification, and is not required to be specified by the implementor for any particular implementation.]