https://docs.basex.org/api.php?action=feedcontributions&user=Alex&feedformat=atomBaseX Documentation - User contributions [en]2024-03-28T10:28:10ZUser contributionsMediaWiki 1.34.0https://docs.basex.org/index.php?title=XQuery_3.1&diff=11939XQuery 3.12015-08-23T20:14:01Z<p>Alex: Fixed links to specification for fn:json-to-xml and fn:xml-to-json.</p>
<hr />
<div>This article is part of the [[XQuery|XQuery Portal]].<br />
It summarizes the new features of the [http://www.w3.org/TR/xquery-31/ XQuery 3.1] Working Draft<br />
that are already supported by BaseX.<br />
<br />
=Maps=<br />
<br />
A ''map'' is a function that associates a set of keys with values, resulting in a collection of key/value pairs. Each key/value pair in a map is called an entry. A key is an arbitrary atomic value, and the associated value is an arbitrary sequence. Within a map, no two entries have the same key, when compared using the {{Code|eq}} operator. It is not necessary that all the keys should be mutually comparable (for example, they can include a mixture of integers and strings).<br />
<br />
Maps can be constructed as follows:<br />
<br />
<pre class="brush:xquery"><br />
map { }, (: empty map :)<br />
map { 'key': true(), 1984: (<a/>, <b/>) }, (: map with two entries :)<br />
map:merge( (: map with ten entries :)<br />
for $i in 1 to 10<br />
return map { $i: 'value' || $i }<br />
)<br />
</pre><br />
<br />
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:<br />
<br />
<pre class="brush:xquery"><br />
let $map := map { 'foo': 42, 'bar': 'baz', 123: 456 }<br />
return fn:for-each(map:keys($map), $map)<br />
</pre><br />
<br />
This returns some permutation of {{Code|(42, 'baz', 456)}}.<br />
<br />
Because a map is a function item, functions that apply to functions also apply to maps. A map is an anonymous function, so {{Code|fn:function-name}} returns the empty sequence; {{Code|fn:function-arity}} always returns {{Code|1}}.<br />
<br />
Like all other values, maps are immutable. For example, the <code>[[Map Module#map:remove|map:remove]]</code> function creates a new map by removing an entry from an existing map, but the existing map is not changed by the operation. Like sequences, maps have no identity. It is meaningful to compare the contents of two maps, but there is no way of asking whether they are "the same map": two maps with the same content are indistinguishable.<br />
<br />
Maps may be compared using the {{Code|fn:deep-equal}} function. The [[Map Module]] describes the available set of map functions.<br />
<br />
=Arrays=<br />
<br />
An ''array'' is a function that associates a set of positions, represented as positive integer keys, with values. The first position in an array is associated with the integer {{Code|1}}. The values of an array are called its members. In the type hierarchy, array has a distinct type, which is derived from function. In BaseX, arrays (as well as sequences) are based on an efficient [http://en.wikipedia.org/wiki/Finger_tree Finger Tree] implementation.<br />
<br />
Arrays can be constructed in two ways. With the square bracket notation, the comma serves as delimiter:<br />
<br />
<pre class="brush:xquery"><br />
[], (: empty array :)<br />
[ (1, 2) ], (: array with single member :)<br />
[ 1 to 2, 3 ] (: array with two members; same as: [ (1, 2), 3 ] :)<br />
</pre><br />
<br />
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:<br />
<br />
<pre class="brush:xquery"><br />
array { }, (: empty array; same as: array { () } :) <br />
array { (1, 2) }, (: array with two members; same as: array { 1, 2 } :)<br />
array { 1 to 2, 3 } (: array with three members; same as: array { 1, 2, 3 } :)<br />
</pre><br />
<br />
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:<br />
<br />
<pre class="brush:xquery"><br />
let $array := array { 48 to 52 }<br />
for $i in 1 to array:size($array)<br />
return $array($i)<br />
</pre><br />
<br />
Like all other values, arrays are immutable. For example, the <code>[[Array Module#array:reverse|array:reverse]]</code> 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.<br />
<br />
==Atomization==<br />
<br />
If an array is ''atomized'', all of its members will be atomized. As a result, an atomized item may now result in more than one item. Some examples:<br />
<br />
<pre class="brush:xquery"><br />
fn:data([1 to 2]) (: returns the sequence 1, 2 :)<br />
[ 'a', 'b', 'c' ] = 'b' (: returns true :)<br />
<a>{ [ 1, 2 ] }</a> (: returns <a>1 2</a> :)<br />
array { 1 to 2 } + 3 (: error: the left operand returns two items :)<br />
</pre><br />
<br />
Atomization also applies to function arguments. The following query returns 5, because the array will be atomized to a sequence of 5 integers:<br />
<br />
<pre class="brush:xquery"><br />
let $f := function($x as xs:integer*) { count($x) }<br />
return $f([1 to 5])<br />
</pre><br />
<br />
However, the next query returns 1, because the array is already of the general type {{Code|item()}}, and no atomization will take place:<br />
<br />
<pre class="brush:xquery"><br />
let $f := function($x as item()*) { count($x) }<br />
return $f([1 to 5])<br />
</pre><br />
<br />
Arrays can be compared with the {{Code|fn:deep-equal}} function. The [[Array Module]] describes the available set of array functions.<br />
<br />
=Lookup Operator=<br />
<br />
The lookup operator provides some syntactic sugar to access values of maps or array members at a specified position. It is introduced by the question mark ({{Code|?}}) and followed by a specifier. The specifier can be:<br />
<br />
# A wildcard {{Code|*}},<br />
# The name of the key,<br />
# The integer offset, or<br />
# Any other parenthesized expression.<br />
<br />
The following example demonstrates the four alternatives:<br />
<br />
<pre class="brush:xquery"><br />
let $map := map { 'R': 'red', 'G': 'green', 'B': 'blue' }<br />
return (<br />
$map?* (: 1. returns all values; same as: map:keys($map) ! $map(.) :),<br />
$map?R (: 2. returns the value associated with the key 'R'; same as: $map('R') :),<br />
$map?('G','B') (: 3. returns the values associated with the key 'G' and 'B' :)<br />
),<br />
<br />
let $array := [ 'one', 'two', 'three' ]<br />
return (<br />
$array?* (: 1. returns all values; same as: (1 to array:size($array)) ! $array(.) :),<br />
$array?1 (: 2. returns the first value; same as: $array(1) :),<br />
$array?(2 to 3) (: 3. returns the second and third values; same as: (1 to 2) ! $array(.) :)<br />
)<br />
</pre><br />
<br />
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}}:<br />
<br />
<pre class="brush:xquery"><br />
for $map in (<br />
map { 'name': 'Guðrún', 'city': 'Reykjavík' },<br />
map { 'name': 'Hildur', 'city': 'Akureyri' }<br />
)<br />
return $map[?name = 'Hildur'] ?city<br />
</pre><br />
<br />
=Arrow Operator=<br />
<br />
The arrow operator applies a function to a value. The value is used as the first argument to the function. It is introduced with the characters <code>=></code>, and it is followed by the function to be called. If <code>$v</code> is a value and <code>f()</code> is a function, then <code>$v=>f()</code> is equivalent to <code>f($v)</code>, and <code>$v=>f($j)</code> is equivalent to <code>f($v, $j)</code>. This is further illustrated by an example:<br />
<br />
<pre class="brush:xquery"><br />
(: Returns 3 :)<br />
count(('A', 'B', 'C')),<br />
('A', 'B', 'C') => count(),<br />
('A', 'B', 'C') => (function( $sequence) { count( $sequence)})(),<br />
<br />
(: Returns W-E-L-C-O-M-E :)<br />
string-join(tokenize(upper-case('w e l c o m e')), '-'),<br />
'w e l c o m e' => upper-case() => tokenize() => string-join('-'),<br />
<br />
(: Returns xfmdpnf :)<br />
codepoints-to-string(<br />
for $i in string-to-codepoints('welcome')<br />
return $i + 1<br />
),<br />
(for $i in 'welcome' => string-to-codepoints()<br />
return $i + 1) => codepoints-to-string()<br />
</pre><br />
<br />
The syntax makes nested function calls more readable, as it is easy to see if parentheses are balanced.<br />
<br />
=Serialization=<br />
<br />
Two [[Serialization]] methods have been added to the [http://www.w3.org/TR/xslt-xquery-serialization-31 Serialization spec]:<br />
<br />
==Adaptive Serialization==<br />
<br />
In BaseX, {{Code|adaptive}} is used as the new default serialization method. It provides a textual representation for all XDM types, including maps and arrays, functions, attributes, and namespaces. All items will be separated using the value of the {{Code|item-separator}} parameter, or a newline if no value is specified:<br />
<br />
<pre class="brush:xquery"><br />
<element id='id0'/>/@id,<br />
map { 'key': 'value' },<br />
true#0<br />
</pre><br />
<br />
Result:<br />
<br />
<pre class="brush:xml"><br />
id="id0"<br />
{<br />
"key": "value"<br />
}<br />
function true#0<br />
</pre><br />
<br />
==JSON Serialization==<br />
<br />
The new {{Code|json}} serialization output method can be used to serialize XQuery maps, arrays, atomic values and empty sequences as JSON.<br />
<br />
The {{Code|json}} output method has been introduced in BaseX quite a while ago. The implementation of this method now complies with the standard serialization rules and, at the same time, preserves the existing semantics:<br />
<br />
* If an XML node of type {{Code|element(json)}} is found, it will be serialized following the serialization rules of the [[JSON Module]].<br />
* Any other node or atomic value, map, array, or empty sequence will be serialized according to the [http://www.w3.org/TR/xslt-xquery-serialization-31/#json-output rules in the specification].<br />
<br />
The following two queries will both return the JSON snippet <code>{ "key": "value" }</code>:<br />
<br />
<pre class="brush:xquery"><br />
declare option output:method 'json';<br />
map { "key": "value" }<br />
</pre><br />
<br />
<pre class="brush:xquery"><br />
declare option output:method 'json';<br />
<json type='object'><br />
<key>value</key><br />
</json><br />
</pre><br />
<br />
=Functions=<br />
<br />
The following functions of the [http://www.w3.org/TR/xpath-functions-31/ XQuery 3.1 Functions and Operators] Working Draft have been added. Please be aware that the functions are still subject to change:<br />
<br />
==Map Functions==<br />
<br />
The following map functions are now available:<br />
<br />
<code>map:merge</code>, <code>map:size</code>, <code>map:keys</code>, <code>map:contains</code>, <code>map:get</code>, <code>map:entry</code>, <code>map:put</code>, <code>map:remove</code>, <code>map:for-each</code><br />
<br />
Please check out the [[Map Module]] for more details.<br />
<br />
==Array Functions==<br />
<br />
The following array functions are now available:<br />
<br />
<code>array:size</code>, <code>array:append</code>, <code>array:subarray</code>, <code>array:remove</code>, <code>array:insert-before</code>, <code>array:head</code>, <code>array:tail</code>, <code>array:reverse</code>, <code>array:join</code>, <code>array:flatten</code>, <code>array:for-each</code>, <code>array:filter</code>, <code>array:fold-left</code>, <code>array:fold-right</code>, <code>array:for-each-pair</code><br />
<br />
Please check out the [[Array Module]] for more details.<br />
<br />
==JSON Functions==<br />
<br />
XQuery now provides native support for JSON objects. Strings and resources can be parsed to XQuery items and, as [[#JSON Serialization|shown above]], serialized back to their original form.<br />
<br />
===fn:parse-json===<br />
<br />
; Signatures<br />
* <code>fn:parse-json($input as xs:string) as item()?</code><br />
* <code>fn:parse-json($input as xs:string, $options as map(*)) as item()?</code><br />
<br />
Parses the supplied string as JSON text and returns its item representation. The result may be a map, an array, a string, a double, a boolean, or an empty sequence. The allowed options can be looked up in the [http://www.w3.org/TR/xpath-functions-31/#func-parse-json specification].<br />
<br />
<pre class="brush:xquery"><br />
parse-json('{ "name": "john" }') (: yields { "name": "json" } :),<br />
parse-json('[ 1, 2, 4, 8, 16]') (: yields [ 1, 2, 4, 8, 16 ] :)<br />
</pre><br />
<br />
===fn:json-docs===<br />
<br />
; Signatures<br />
* <code>fn:json-doc($uri as xs:string) as item()?</code><br />
* <code>fn:json-doc($uri as xs:string, $options as map(*)) as item()?</code><br />
<br />
Retrieves the text from the specified URI, parses the supplied string as JSON text and returns its item representation (see [[#fn:parse-json|fn:parse-json]] for more details).<br />
<br />
<pre class="brush:xquery"><br />
json-doc("http://ip.jsontest.com/")('ip') (: returns your IP address :)<br />
</pre><br />
<br />
===fn:json-to-xml===<br />
<br />
; Signatures<br />
* <code>fn:json-to-xml($string as xs:string?) as node()?</code><br />
<br />
Converts a JSON string to an XML node representation. The allowed options can be looked up in the [http://www.w3.org/TR/xslt-30/#func-json-to-xml specification].<br />
<br />
<pre class="brush:xquery"><br />
json-to-xml('{ "message": "world" }')<br />
<br />
(: result:<br />
<map xmlns="http://www.w3.org/2005/xpath-functions/json"><br />
<string key="message">world</string><br />
</map> :)<br />
</pre><br />
<br />
===fn:xml-to-json===<br />
<br />
; Signatures<br />
* <code>fn:xml-to-json($node as node()?) as xs:string?</code><br />
<br />
Converts an XML node, whose format conforms to the results created by [[#fn:json-to-xml|fn:json-to-xml]], to a JSON string representation. The allowed options can be looked up in the [http://www.w3.org/TR/xslt-30/#func-xml-to-json specification].<br />
<br />
<pre class="brush:xquery"><br />
(: returns "JSON" :)<br />
xml-to-json(<string xmlns="http://www.w3.org/2005/xpath-functions/json">JSON</string>)<br />
</pre><br />
<br />
==fn:sort==<br />
<br />
; Signatures<br />
* <code>fn:sort($input as item()*) as item()*</code><br />
* <code>fn:sort($input as item()*, $key as function(item()*) as xs:anyAtomicType*)) as item()*</code><br />
<br />
Returns a new sequence with sorted {{Code|$input}} items. If a sort {{Code|$key}} function is given, it will be applied on all items. The items of the resulting values will be sorted using the semantics of the {{Code|lt}} expression.<br />
<br />
<pre class="brush:xquery"><br />
sort(reverse(1 to 3)) (: yields 1, 2, 3 :),<br />
sort((3, -2, 1), abs#1) (: yields 1, -2, 3 :),<br />
sort((1,2,3), function($x) { -$x }) (: yields 3, 2, 1 :),<br />
sort((1, 'a')) (: yields an error, as strings and integers cannot be compared :)<br />
</pre><br />
<br />
==fn:contains-token==<br />
<br />
; Signatures<br />
* <code>fn:contains-token($input as xs:string*, $token as string) as xs:boolean</code><br />
* <code>fn:contains-token($input as xs:string*, $token as string, $collation as xs:string) as xs:boolean</code><br />
<br />
The supplied strings will be tokenized at whitespace boundaries. The function returns {{Code|true}} if one of the strings equals the supplied token, possibly under the rules of a supplied collation:<br />
<br />
<pre class="brush:xquery"><br />
contains-token(('a', 'b c', 'd'), 'c') (: yields true :)<br />
<xml class='one two'/>/contains-token(@class, 'one') (: yields true :)<br />
</pre><br />
<br />
==fn:parse-ietf-date==<br />
<br />
; Signature<br />
* <code>fn:parse-ietf-date($input as xs:string?) as xs:string?</code><br />
<br />
Parses a string in the IETF format (which is widely used on the Internet) and returns a {{Code|xs:dateTime}} item:<br />
<br />
<pre class="brush:xquery"><br />
fn:parse-ietf-date('28-Feb-1984 07:07:07')" (: yields 1984-02-28T07:07:07Z :),<br />
fn:parse-ietf-date('Wed, 01 Jun 2001 23:45:54 +02:00')" (: yields 2001-06-01T23:45:54+02:00 :)<br />
</pre><br />
<br />
==fn:apply==<br />
<br />
; Signatures<br />
* <code>fn:apply($function as function(*), $array as array(*)) as item()*</code><br />
<br />
A supplied function is invoked with the arguments supplied by an array. The arity of the function must be the same as the size of the array.<br />
<br />
Example:<br />
<br />
<pre class="brush:xquery"><br />
fn:apply(concat#5, array { 1 to 5 }) (: 12345 :)<br />
fn:apply(function($a) { sum($a) }, [ 1 to 5 ]) (: 15 :)<br />
fn:apply(count#1, [ 1,2 ]) (: error (the array has two members) :)<br />
</pre><br />
<br />
==fn:random-number-generator==<br />
<br />
; Signatures<br />
* <code>fn:random-number-generator() as map(xs:string, item())</code><br />
* <code>fn:random-number-generator($seed as xs:anyAtomicType) as map(xs:string, item())</code><br />
<br />
Creates a random number generator, using an optional seed. The returned map contains three entries:<br />
<br />
* {{Code|number}} is a random double between 0 and 1<br />
* {{Code|next}} is a function that returns another random number generator<br />
* {{Code|permute}} is a function that returns a random permutation of its argument<br />
<br />
The returned random generator is ''deterministic'': If the function is called twice with the same arguments and in the same execution scope, it will always return the same result.<br />
<br />
Example:<br />
<br />
<pre class="brush:xquery"><br />
let $rng := fn:random-number-generator()<br />
let $number := $rng('number') (: returns a random number :)<br />
let $next-rng := $rng('next')() (: returns a new generator :)<br />
let $next-number := $next-rng('number') (: returns another random number :)<br />
let $permutation := $rng('permute')(1 to 5) (: returns a random permutation of (1,2,3,4,5) :)<br />
return ($number, $next-number, $permutation)<br />
</pre><br />
<br />
==fn:format-number==<br />
<br />
The function has been extended to support scientific notation:<br />
<br />
<pre class="brush:xquery"><br />
format-number(1984.42, '00.0e0') (: yields 19.8e2 :)<br />
</pre><br />
<br />
==fn:tokenize==<br />
<br />
If no separator is specified as second argument, a string will be tokenized at whitespace boundaries:<br />
<br />
<pre class="brush:xquery"><br />
fn:tokenize(" a b c d") (: yields "a", "b", "c", "d" :)<br />
</pre><br />
<br />
==fn:trace==<br />
<br />
The second argument can now be omitted:<br />
<br />
<pre class="brush:xquery"><br />
fn:trace(<xml/>, "Node: ")/node() (: yields the debugging output "Node: <xml/>" :),<br />
fn:trace(<xml/>)/node() (: returns the debugging output "<xml/>" :)<br />
</pre><br />
<br />
=Binary Data=<br />
<br />
Items of type <code>xs:hexBinary</code> and <code>xs:base64Binary</code> can now be compared against each other. The following queries all yield {{Code|true}}:<br />
<br />
<pre class="brush:xquery"><br />
xs:hexBinary('') < xs:hexBinary('bb'),<br />
xs:hexBinary('aa') < xs:hexBinary('bb'),<br />
max((xs:hexBinary('aa'), xs:hexBinary('bb'))) = xs:hexBinary('bb')<br />
</pre><br />
<br />
=Collations=<br />
<br />
XQuery 3.1 provides a new default collation, which allows for a case-insensitive comparison of ASCII characters (<code>A-Z</code> = <code>a-z</code>). This query returns <code>true</code>:<br />
<br />
<pre class="brush:xquery"><br />
declare default collation 'http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive';<br />
'HTML' = 'html'<br />
</pre><br />
<br />
If the [http://site.icu-project.org/download ICU Library] is downloaded and added to the classpath, the full [http://www.w3.org/TR/xpath-functions-31/#uca-collations Unicode Collation Algorithm] features get available in BaseX:<br />
<br />
<pre class="brush:xquery"><br />
(: returns 0 (both strings are compared as equal) :)<br />
compare('a-b', 'ab', 'http://www.w3.org/2013/collation/UCA?alternate=shifted')<br />
</pre><br />
<br />
=Pending Features=<br />
<br />
The following functions have not been implemented yet:<br />
<br />
* <code>fn:collation-key</code>, <code>fn:load-xquery-module</code>, <code>fn:transform</code><br />
<br />
=Changelog=<br />
<br />
;Version 8.2<br />
<br />
* Added: [[#fn:json-to-xml|fn:json-to-xml]], [[#fn:xml-to-json|fn:xml-to-json]].<br />
<br />
;Version 8.1<br />
<br />
* Updated: arrays are now based on an efficient [http://en.wikipedia.org/wiki/Finger_tree Finger Tree] implementation.<br />
<br />
Introduced with Version 8.0.<br />
<br />
[[Category:XQuery]]</div>Alex