Main Page » XQuery » XQuery 4.0

XQuery 4.0

This article provides a summary of the most important features of the upcoming recommendation of XQuery 4.0 that are already available in BaseX.

Please note that the current specification is in draft status, so everything is subject to change until it is finalized. We have chosen to present only those features that appear sufficiently stable, but you may come across various other features in our implementation that we have not selected for documentation yet.



The otherwise operator has two operands. The value of the first operand will be returned only if non-empty; otherwise, the value of the second operand will be returned. The expression…
$value otherwise $fallback
…is equivalent to:
if (exists($value)) then $value else $fallback
The operator can also be chained:
$node/(@full-name otherwise @name otherwise @id)

Mapping Arrow

The mapping arrow operator =!> complements the XQuery 3.1 arrow operator =>. It applies a function to each item in a sequence. In the following example, an input string is tokenized, the strings are replaced by an integer indicating the string length, and the resulting tokens are concatenated as string:
'You never know'
=> tokenize()
=!> string-length()
=> string-join(', ')
The result is:
3, 5, 4


String Templates

Single backticks can be used to create literal strings with ampersands and angle brackets:
`<Jill & Jack>`
Curly braces can be used to include expressions in the string:
let $name := 'Shiva'
let $age := 43
return `Person: { $name }, { $age }`
The syntax represents a compact alternative for classical string concatenations…
'Person: ' || $name || ', ' || $age
…and the string constructor:
``[Person: `{ $name }`, `{ $age }`]``

Map Constructors

The keyword map in map constructors has become optional. The following expressions return the same map:
{ 1: 'one', 2: 'two' }
map { 1: 'one', 2: 'two' }
Note that the new syntax is similar to the JSON syntax. However, it is not identical, and it is more powerful because you can include arbitrary expressions that are evaluated when the map is generated. For example, you can use variables and arbitrary complex expressions to create the keys and values:
let $xml := document {
    <line>Hi there</line>
    <line>Bye there</line>
for $line at $pos in $xml//line
return { $pos: data($line) }

Braced if

In many programming languages, the branches of conditional expressions can be enclosed in curly braces. This syntax is now supported in XQuery as well, with the else branch becoming optional:
if ($a) { $b } else { $c }
if ($a) { if ($b) { $c } }
…is equivalent to:
if ($a) then $b else $c
if ($a) then (if ($b) then $c else ()) else ()
You may be aware that in BaseX the else branch can already be omitted in the classical syntax (see if without else), but the new syntax provides a standardized solution for this.

Numeric Literals

Underscores can be included in numeric literals to improve readability:
(: returns the decimal value 1001000.1001 :)
1_000_000 + 1_000 + 0.100_1
If numbers are prefixed with 0x or 0b, the input will be interpreted as a hexadecimal or a binary number:
(: returns (15, 15, 15) :)
(15, 0xF, 0b1111)

Lookup Operator

If the righthand operator of a loop expression is a variable or a string, the parentheses can be omitted:
$city?'city code'
…is equivalent to:
$city?('city code')


To simplify formatting, the cases of switch expressions can now be enclosed in curly braces. In addition, multiple values can be supplied in a case branch:
switch('tokyo') {
  case ('tokyo', 'kyoto')
    return 'Japan'
    return 'World'
If the comparand is omitted, it defaults to true(), and the construct can be used to specify multiple boolean conditions:
for $name in ('ANNA', 'Hanna', 'hannah')
return switch {
  case matches($name, '^[a-z]+$')
    return 'lower-case'
  case matches($name, '^[A-Z]+$')
    return 'upper-case'
    return 'mixed'


Similarly, curly braces can also be used for typeswitch:
typeswitch(<xml/>) {
  case node()
    return 'node'
    return 'no node'


The boolean functions true() and false() are now valid annotation values.

With the new fn:annotation-values function (which replaces inspect:function-annotations), you can retrieve the annotations of a function:

declare %local:deprecated(true()) function local:inc($n) { $n + 1 };

let $anns := function-annotations(local:get#1)
where map:get($anns, xs:QName('local:deprecated'))
return 'Deprecated!'

Node Tests

Multiple element/attribute names can be specified in the element() and attribute() tests of axis steps:
(: returns all child elements named 'a' or 'b' :)
let $xml := <xml><a/><b/></xml>
return $xml/element(a|b)

FLWOR Expressions

For member

Iterating over an array has been simplified. The member keyword can be used to bind the members of an array to a variable:
(: returns (0, 1, 5) :)
for member $m in [ (), 1, (2, 3) ]
return sum($m)

For key and value

Similarly, with the key and value keywords, the components of map entries can be bound to variables. The following example returns 0, 1 and 5:

for key $k value $v in { 1: 'one', 2: 'two', 3: 'three' }
return $k || ': ' || $v
It is possible to omit either key or value.

Let Clause

When variables are bound with the let clause, and when a type is specified, the input is now coerced (i.e., converted) to the target type:
let $name as xs:string := <name>Midori</name>
return $name
With XQuery 3.1, this expression would have been rejected, as the node could not be treated as string.

Window Clause

The window clause has become more flexible. The start condition can now be omitted, as well as the when keyword (which both default to true()). The following expression…
for tumbling window $w in (2, 4, 6, 8, 10, 12, 14)
  (: start when true() :)
  end $last when $last mod 3 = 0
return <window>{ $w }</window>
<window>2 4 6</window>
<window>8 10 12</window>
For the following query…
for sliding window $w in 1 to 3
  start at $s (: when true() :)
  end at $e when $e - $s eq 2
return array { $w }
…you get:
[ 1, 2, 3 ]
[ 2, 3 ]
[ 3 ]



With the new “Choice Item Type”, multiple types can be specified as a single type. An item matches this type if it matches one of the alternative target types. The following function accepts a string or an integer:
declare function local:number(
  $value  as (xs:integer | xs:string)
) as xs:integer {
  if ($value instance of xs:integer) then (
  ) else (
If none of the types match, the argument will be converted to the target types in the given order. Once a conversion succeeds, the converted value becomes the effective value. This means that the order is important in which the types are specified. For example, if the function above is invoked with the element <value>123</value>, the effective value will be an integer. If the two target types are swapped, the value will be a string.


The type system has been extended to restrict a string argument to an enumeration of strings. The following function can only be called with one of the specified continent names:
declare function local:country(
  $name       as xs:string,
  $continent  as enum('Africa', 'America', 'Asia', 'Australia', 'Europe')
) as element(country) {
  <country name='{ $name }' continent='{ $continent }'/>
local:country('Ghana', 'Africa')
Note that a string is not an instance of an enumeration. In the given example, the supplied string Africa is not an instance of enum('Africa', 'America', 'Asia', 'Australia', 'Europe'). Instead, it is converted to an instance of the enumeration type when the above function is called.


Built-in Functions

Numerous new functions have been added to the specification. Please visit the corresponding pages for more details:

With fn:invisible-xml, Invisible XML can parsed and evaluated.

Positional Access

An optional position parameter has been added to most higher-order functions. It can be used, for example, to enumerate results:

  ('one', 'two', 'three'),
  fn($item, $pos) { $pos || '. ' || $item }

Calling Higher-Order Functions

When a higher-order function expects another function as an argument, it is now possible to supply a function with fewer arguments than specified.

For example, the signature of fn:for-each is:

  $input   as item()*,
  $action  as fn(item(), xs:integer) as item()*
) as item()*

The type of the $action parameter indicates that a passed function will be called internally with two arguments: The first argument is the item currently being processed, and the second argument is the current position of this item. Even though the type defines two parameters, it is possible to pass a function with 1 or 0 parameters. The following function calls are all valid:

(: function argument with 2 parameters :)
for-each(1 to 5, fn($item, $pos) { $pos || '. ' || format-integer($item, 'w') }),

(: function argument with 1 parameter :)
for-each(1 to 5, fn($item) { $item * $item }),

(: function argument with 0 parameters :)
for-each(1 to 5, fn() { 42 })

Default Values

Function parameters can be enriched with default values, thus making these parameters optional. If the following function…
 : Creates a string representation of a value with a specific serialization method.
 : @param  $value   value to be serialized
 : @param  $method  serialization method (default: html)
declare function local:serialize(
  $method := 'html'
) {
  serialize($value, { 'method': $method })
…is called with with one argument, the default value is assigned to $method. If a second argument is supplied, the default value is ignored:
local:serialize(<html/>, 'xhtml')
If a default value is attached to a function parameter, all subsequent parameters must have default values, too.

Keyword Arguments

Code can become more readable if the parameters are adressed by their name. In the following example…
declare function local:coordinates(
  $x as xs:integer := 0,
  $y as xs:integer := 0,
  $z as xs:integer := 0
) {
  { "x" : $x, "y" : $y, "z": $z }
local:coordinates(y := 56)
…a map with three keys will be returned:
{ "x" : 0, "y" : 56, "z": 0 }

Inline Functions

Function items have been introduced with XQuery 3.0. As the existing syntax of inline functions is pretty verbose…
let $inc := function($n) { $n + 1 }
return $inc(99)
…a short fn keyword has been added to the language. It is a plain alias:
let $inc := fn($n) { $n + 1 }
return $inc(99)

Focus Functions

Many functions have a single parameter. The parenthesized parameter declaration can now be omitted, and the generated function item will take one argument that is bound to the context value and can be adressed with the dot syntax . in the function body. The example from the previous paragraph can be further simplified to:
let $inc := fn { . + 1 }
return $inc(99)
This syntax is particularly concise when passing function items as arguments in function calls:
filter(1 to 10, fn { . > 5 }),
array:build(1 to 10, fn { format-integer(., 'w') })


Coercion Rules

The coercion rules define the way how a value is converted to a target type. These rules have been enhanced to reduce the necessity of explicit type conversions: Strings/URIs, binary data and numbers are implicitly cast to the target type, or relabeled, if the input does not exceed the range of the target type. The following type assignments would all have raised an error with XQuery 3.1:
let $base64  as xs:base64Binary := xs:hexBinary('41')
let $uri     as xs:anyURI       := 'string'  (: decimal :)
let $integer as xs:integer      := 1.0  (: decimal :)
let $byte    as xs:byte         := 127  (: integer :)
return '✓'
The same rules apply when calling functions:
declare function local:f($value as xs:byte) { (: ... :) };
local:f(127)   (: succeeds :)
local:f(12345) (: fails :)

Context Value

The context item has been generalized to the context value. Sequences can either be bound to the context with the context value declaration…
(: the 'countries' database can contains zero, one, or more documents :)
declare context value := db:get('countries');
…or without external bindings.

As previously, the context value can be addressed with ., the context item reference. In BaseX, this was already possible in previous versions, and the generalization is now standardized.

Predeclared Namespaces

All implementations must now predeclare the namespace prefixes math, map, array, err, and output. Which means that nothing changes for users of BaseX.


Version 11.0
  • Added: First release with support for various new XQuery 4.0 features.

⚡Generated with XQuery