return map { $i: 'value' || $i }
)
</syntaxhighlightpre>
The function corresponding to the map has the signature {{Code|function($key as xs:anyAtomicType) as item()*}}. The expression {{Code|$map($key)}} returns the associated value; the function call {{Code|map:get($map, $key)}} is equivalent. For example, if {{Code|$books-by-isbn}} is a map whose keys are ISBNs and whose associated values are {{Code|book}} elements, then the expression {{Code|$books-by-isbn("0470192747")}} returns the {{Code|book}} element with the given ISBN. The fact that a map is a function item allows it to be passed as an argument to [[Higher-Order Functions]] that expect a function item as one of their arguments. As an example, the following query uses the higher-order function {{Code|fn:map($f, $seq)}} to extract all bound values from a map:
let $map := map { 'foo': 42, 'bar': 'baz', 123: 456 }
return fn:for-each(map:keys($map), $map)
</syntaxhighlightpre>
This returns some permutation of {{Code|(42, 'baz', 456)}}.
[ (1, 2) ], (: array with single member :)
[ 1 to 2, 3 ] (: array with two members; same as: [ (1, 2), 3 ] :)
</syntaxhighlightpre>
With the {{Code|array}} keyword and curly brackets, the inner expression is evaluated as usual, and the resulting values will be the members of the array:
array { (1, 2) }, (: array with two members; same as: array { 1, 2 } :)
array { 1 to 2, 3 } (: array with three members; same as: array { 1, 2, 3 } :)
</syntaxhighlightpre>
The function corresponding to the array has the signature {{Code|function($index as xs:integer) as item()*}}. The expression {{Code|$array($index)}} returns an addressed member of the array. The following query returns the five array members {{Code|48 49 50 51 52}} as result:
for $i in 1 to array:size($array)
return $array($i)
</syntaxhighlightpre>
Like all other values, arrays are immutable. For example, the {{Function|Array|array:reverse}} function creates a new array containing a re-ordering of the members of an existing array, but the existing array is not changed by the operation. Like sequences, arrays have no identity. It is meaningful to compare the contents of two arrays, but there is no way of asking whether they are "the same array": two arrays with the same content are indistinguishable.
<a>{ [ 1, 2 ] }</a> (: returns <a>1 2</a> :)
array { 1 to 2 } + 3 (: error: the left operand returns two items :)
</syntaxhighlightpre>
Atomization also applies to function arguments. The following query returns 5, because the array will be atomized to a sequence of 5 integers:
let $f := function($x as xs:integer*) { count($x) }
return $f([1 to 5])
</syntaxhighlightpre>
However, the next query returns 1, because the array is already of the general type {{Code|item()}}, and no atomization will take place:
let $f := function($x as item()*) { count($x) }
return $f([1 to 5])
</syntaxhighlightpre>
Arrays can be compared with the {{Code|fn:deep-equal}} function. The [[Array Module]] describes the available set of array functions.
$array?(2 to 3) (: 3. returns the second and third values; same as: (1 to 2) ! $array(.) :)
)
</syntaxhighlightpre>
The lookup operator can also be used without left operand. In this case, the context item will be used as input. This query returns {{Code|Akureyri}}:
)
return $maps[?name = 'Hildur'] ?city
</syntaxhighlightpre>
=Arrow Operator=
(for $i in 'welcome' => string-to-codepoints()
return $i + 1) => codepoints-to-string()
</syntaxhighlightpre>
The syntax makes nested function calls more readable, as it is easy to see if parentheses are balanced.
(: Returns "This is a 'new' & 'flexible' syntax." :)
``["This is a 'new' & 'flexible' syntax."]``
</syntaxhighlightpre>
XQuery expressions can be embedded via backticks and a curly bracket. The evaluated results will be separated with spaces, and all strings will eventually be concatenated:
(: Returns »Count 1 2 3, and I will be there.« :)
let $c := 1 to 3
return ``[»Count `{ $c }`, and I will be there.«]``</syntaxhighlightpre>
=Serialization=
map { 'key': 'value' },
true#0
</syntaxhighlightpre>
Result:
}
fn:true#0
</syntaxhighlightpre>
==JSON Serialization==
declare option output:method 'json';
map { "key": "value" }
</syntaxhighlightpre>
<pre lang='xquery'>
<key>value</key>
</json>
</syntaxhighlightpre>
=Functions=
parse-json('{ "name": "john" }') (: yields { "name": "json" } :),
parse-json('[ 1, 2, 4, 8, 16]') (: yields [ 1, 2, 4, 8, 16 ] :)
</syntaxhighlightpre>
===fn:json-doc===
<pre lang='xquery'>
json-doc("http://ip.jsontest.com/")('ip') (: returns your IP address :)
</syntaxhighlightpre>
===fn:json-to-xml===
<string key="message">world</string>
</map> :)
</syntaxhighlightpre>
===fn:xml-to-json===
(: returns "JSON" :)
xml-to-json(<string xmlns="http://www.w3.org/2005/xpath-functions">JSON</string>)
</syntaxhighlightpre>
==fn:sort==
sort((1,2,3), (), function($x) { -$x }) (: yields 3, 2, 1 :),
sort((1,'a')) (: yields an error, as strings and integers cannot be compared :)
</syntaxhighlightpre>
==fn:contains-token==
contains-token(('a', 'b c', 'd'), 'c') (: yields true :)
<xml class='one two'/>/contains-token(@class, 'one') (: yields true :)
</syntaxhighlightpre>
==fn:parse-ietf-date==
fn:parse-ietf-date('28-Feb-1984 07:07:07')" (: yields 1984-02-28T07:07:07Z :),
fn:parse-ietf-date('Wed, 01 Jun 2001 23:45:54 +02:00')" (: yields 2001-06-01T23:45:54+02:00 :)
</syntaxhighlightpre>
==fn:apply==
fn:apply(function($a) { sum($a) }, [ 1 to 5 ]) (: 15 :)
fn:apply(count#1, [ 1,2 ]) (: error. the array has two members :)
</syntaxhighlightpre>
==fn:random-number-generator==
let $permutation := $rng('permute')(1 to 5) (: returns a random permutation of (1,2,3,4,5) :)
return ($number, $next-number, $permutation)
</syntaxhighlightpre>
==fn:format-number==
<pre lang='xquery'>
format-number(1984.42, '00.0e0') (: yields 19.8e2 :)
</syntaxhighlightpre>
==fn:tokenize==
<pre lang='xquery'>
fn:tokenize(" a b c d") (: yields "a", "b", "c", "d" :)
</syntaxhighlightpre>
==fn:trace==
fn:trace(<xml/>, "Node: ")/node() (: yields the debugging output "Node: <xml/>" :),
fn:trace(<xml/>)/node() (: returns the debugging output "<xml/>" :)
</syntaxhighlightpre>
==fn:string-join==
<pre lang='xquery'>
fn:string-join(1 to 3) (: yields the string "123" :)
</syntaxhighlightpre>
==fn:default-language==
xs:hexBinary('aa') < xs:hexBinary('bb'),
max((xs:hexBinary('aa'), xs:hexBinary('bb'))) = xs:hexBinary('bb')
</syntaxhighlightpre>
=Collations=
declare default collation 'http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive';
'HTML' = 'html'
</syntaxhighlightpre>
If the [http://site.icu-project.org/download ICU Library] is downloaded and added to the classpath, the full [https://www.w3.org/TR/xpath-functions-31/#uca-collations Unicode Collation Algorithm] features become available in BaseX:
(: returns 0 (both strings are compared as equal) :)
compare('a-b', 'ab', 'http://www.w3.org/2013/collation/UCA?alternate=shifted')
</syntaxhighlightpre>
=Enclosed Expressions=
element x { () },
text { () }
</syntaxhighlightpre>
With XQuery 3.1, the expression can be omitted. The following query is equivalent to the upper one:
element x { }
text { }
</syntaxhighlightpre>
=Changelog=