XQuery 3.1 FLWOR Expressions

XQuery is to XML what SQL is to tables: a functional query language, a superset of XPath, that can also build new documents. XQuery 3.1 (W3C Recommendation, 21 March 2017) is current; 4.0 is a community draft. Its core is the FLWOR expression (for, let, where, order by, return, plus group by, count and window clauses since 3.0), which turns a stream of tuples of variable bindings into results, much as a SQL engine turns rows into a result set:

A FLWOR expression as a pipeline of tuple streams
A FLWOR expression as a pipeline of tuple streams

Saxon 726,956 runs these queries; BaseX 693,830 (github.com/BaseXdb/basex (https://github.com/BaseXdb/basex 768 ), BSD 3-Clause), an XML database with its own optimizer and indexes, runs them too. Install it alongside Saxon:

Installing BaseX 12.4Shell
cd ~/xml-tools && [ -f BaseX124.zip ] ||
  curl -fsSLO --retry 5 --retry-all-errors https://files.basex.org/releases/12.4/BaseX124.zip
unzip -oq BaseX124.zip && ln -sf ~/xml-tools/basex/bin/basex ~/bin/basex
basex -h 2>&1 | head -n 1
Output
BaseX 12.4 [Standalone]

Subsections


XQuery

The W3C describes the mission of the XML Query project as follows: "The mission of the XML Query project is to provide flexible query facilities to extract data from real and virtual documents on the World Wide Web, therefore finally providing the needed interaction between the Web world and the database world. Ultimately, collections of XML files will be accessed like databases."

XQuery was originally conceived as a query language for large collections of XML documents, but it is equally capable of transforming individual documents. Syntactically, XQuery shares some similarities with SQL.

XQuery is a functional language. Its file names conventionally use the extension .xq, .xql, or .xquery.

Some characters cannot be used literally inside XQuery string literals or constructed content and must be written using a numeric character reference:
CharacterEscape
(new line)

(space) 
(tab)	
{{
}}
&&
'"

Numeric character references commonly used to escape special characters in XQuery string literals.

This chapter covers the following topics: