Difference between revisions of "XQuery 3.1"

From BaseX Documentation
Jump to navigation Jump to search
Line 62: Line 62:
  
 
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.
 
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.
 +
 +
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:
 +
 +
<pre class="brush:xquery">
 +
fn:data([1 to 2])        (: returns the sequence 1, 2 :)
 +
[ 'a', 'b', 'c' ] = 'b'  (: returns true :)
 +
array { 1 to 2 } + 3    (: returns an error, because the left operand returns two items :)
 +
<a>{ [ 1, 2 ]}</a>      (: returns <a>1 2</a> :)
 +
 +
(: 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])
 +
</pre>
  
 
Arrays can be compared with the {{Code|fn:deep-equal}} function. The [[Array Module]] describes the available set of array functions.
 
Arrays can be compared with the {{Code|fn:deep-equal}} function. The [[Array Module]] describes the available set of array functions.

Revision as of 23:31, 9 August 2014

This article is part of the XQuery Portal. It summarizes the new features of the XQuery 3.1 Working Draft that are already supported by BaseX.

Maps

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 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).

Maps can be constructed as follows:

map { },                                    (: empty map :)
map { 'key': true(), 1984: (<a/>, <b/>) },  (: map with two entries :)
map:merge(                                  (: map with ten entries :)
  for $i in 1 to 10
  return map { $i: 'value' || $i }
)

The function corresponding to the map has the signature function($key as xs:anyAtomicType) as item()*. The expression $map($key) returns the associated value; the function call map:get($map, $key) is equivalent. For example, if $books-by-isbn is a map whose keys are ISBNs and whose associated values are book elements, then the expression $books-by-isbn("0470192747") returns the 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 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)

This returns some permutation of (42, 'baz', 456).

Because a map is a function item, functions that apply to functions also apply to maps. A map is an anonymous function, so fn:function-name returns the empty sequence; fn:function-arity always returns 1.

Like all other values, maps are immutable. For example, the map:remove 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.

Maps may be compared using the fn:deep-equal function. The Map Module describes the available set of map functions.

Arrays

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 1. The values of an array are called its members. In the type hierarchy, array has a distinct type, which is derived from function.

Arrays can be constructed in two ways. With the square bracket notation, the comma serves as delimiter:

[],             (: empty array :)
[ (1, 2) ],     (: array with single member :)
[ 1 to 2, 3 ]   (: array with two members; same as: [ (1, 2), 3 ] :)

With the array keyword and curly brackets, the inner expression is evaluated as usual, and the resulting values will be the members of the array:

array { },            (: empty array;              same as: array { () } :)  
array { (1, 2) },     (: array with three members; same as: array { 1, 2 } :)
array { 1 to 2, 3 }   (: array with three members; same as: array { 1, 2, 3 } :)

The function corresponding to the array has the signature function($index as xs:integer) as item()*. The expression $array($index) returns an addressed member of the array. The following query returns the five array members 48 49 50 51 52 as result:

let $array := array { 48 to 52 }
for $i in 1 to array:size($array)
return $array($i)

Like all other values, arrays are immutable. For example, the 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.

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:

fn:data([1 to 2])        (: returns the sequence 1, 2 :)
[ 'a', 'b', 'c' ] = 'b'  (: returns true :)
array { 1 to 2 } + 3     (: returns an error, because the left operand returns two items :)
<a>{ [ 1, 2 ]}</a>       (: returns <a>1 2</a> :)

(: 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])

Arrays can be compared with the fn:deep-equal function. The Array Module describes the available set of array functions.

Functions

The following functions of the XQuery 3.1 Functions and Operators Working Draft have already been implemented:

  • Map functions: map:merge, map:size, map:keys, map:contains, map:get, map:entry, map:put, map:remove, map:for-each-entry
  • Array functions: array:size, array:append, array:subarray, array:remove, array:insert-before, array:head, array:tail, array:reverse, array:join, array:for-each-member, array:filter, array:for-each-pair
  • fn:contains-token($input as xs:string*, $token as string, $collation as xs:string) as xs:boolean: Checks if the input strings contain a given token.

New signatures have been added for the following functions:

  • fn:tokenize($string as xs:string) as xs:string*: Splits a string at whitespace boundaries.

Binary Data

Items of type xs:hexBinary and xs:base64Binary can now be compared against each other. The following queries all yieldl true:

xs:hexBinary('') < xs:hexBinary('bb'),
xs:hexBinary('aa') < xs:hexBinary('bb'),
max((xs:hexBinary('aa'), xs:hexBinary('bb'))) = xs:hexBinary('bb')

Pending Features

XQuery 3.1 is still subject to change, but we are planning to add the following features and functions soon:

  • Arrow operator (=>): applies a function to an item, using the item as the first argument to the function. The expression $i=>$f() is equivalent to $f($i), and $i=>$f($j) is equivalent to $f($i, $j).
  • Lookup operator (?): returns the value of a map for a specific key, or the member of an array at a specified position. The expression $map('name') is equivalent to $map?name.
  • Support for JSON: fn:json-doc, fn:parse-json
  • New functions: array:fold-left, array:fold-right, fn:collation-key, fn:load-module, fn:parse-ietf-date, fn:transform, map:for-each-entry
  • Support for scientific notation: format-number

Changelog

Introduced with Version 8.0.