Difference between revisions of "XQuery 3.1"

From BaseX Documentation
Jump to navigation Jump to search
Line 5: Line 5:
 
==Maps==
 
==Maps==
  
A map is an additional kind of item. It comprises a collation and a set of entries. Each entry comprises a key which is an arbitrary atomic value, and an arbitrary sequence called the associated value. Within a map, no two entries have the same key, when compared using the {{Code|eq}} operator under the map's collation. It is not necessary that all the keys should be mutually comparable (for example, they can include a mixture of integers and strings). Key values will never be of type {{Code|xs:untypedAtomic}}, and they will never be the {{Code|xs:float}} or {{Code|xs:double}} value {{Code|NaN}}.
+
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).
  
The function call <code>[[Map Module#map:get|map:get($map, $key)]]</code> can be used to retrieve the value associated with a given key.
+
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:
 
 
A ''map'' can be viewed as a function from keys to associated values. To achieve this, a map is also a function item. The function corresponding to the map has the signature {{Code|function($key as xs:anyAtomicType) as item()*}}. Calling the function has the same effect as calling the {{Code|get}} function: the expression {{Code|$map($key)}} returns the same result as {{Code|map:get($map, $key)}}. 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'':
 
  
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
Line 24: Line 22:
 
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}}.
 
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}}.
  
Maps may be compared using the {{Code|fn:deep-equal}} function. The semantics for this function are extended so that when two items are compared, at any level of recursion, the items compare equal if they are both maps, if both use the same collation, if both contain the same set of keys (compared using the {{Code|eq}} operator), without regard to ordering, and if for each key that is present in both maps, the associated values are deep-equal. When comparing maps, the maps' collation is used rather than the collation supplied as an argument to the {{Code|fn:deep-equal}} function.
+
Maps may be compared using the {{Code|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 {{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.
 +
 
 +
 
 +
The function corresponding to the array has the signature {{Code|function($index as xs:integer) as item()*}}. The expression {{Code|$array($index)}} returns the associated member. The fact that an array 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.
 +
 
 +
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.
  
The [[Map Module]] describes all map functions.
+
Arrays may be compared using the {{Code|fn:deep-equal}} function. The [[Array Module]] describes the available set of array functions.
  
 
==Functions==
 
==Functions==
Line 54: Line 63:
 
XQuery 3.1 is still subject to change, but the following features and functions will be added soon:
 
XQuery 3.1 is still subject to change, but the following features and functions will be added soon:
  
* Support for arrays: data structure, functions
 
 
* Arrow operator (<code>=></code>): applies a function to an item, using the item as the first argument to the function. The expression <code>$i=>$f()</code> is equivalent to <code>$f($i)</code>, and <code>$i=>$f($j)</code> is equivalent to <code>$f($i, $j)</code>.
 
* Arrow operator (<code>=></code>): applies a function to an item, using the item as the first argument to the function. The expression <code>$i=>$f()</code> is equivalent to <code>$f($i)</code>, and <code>$i=>$f($j)</code> is equivalent to <code>$f($i, $j)</code>.
 
* Map lookup operator (<code>?</code>): returns the value of a map for a specific key. The expression <code>$map('name')</code> is equivalent to <code>$map?name</code>.
 
* Map lookup operator (<code>?</code>): returns the value of a map for a specific key. The expression <code>$map('name')</code> is equivalent to <code>$map?name</code>.

Revision as of 20:42, 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).

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:map($map, map:keys($map))

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

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.

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.

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.


The function corresponding to the array has the signature function($index as xs:integer) as item()*. The expression $array($index) returns the associated member. The fact that an array 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.

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.

Arrays may be compared using 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

fn:contains-token,

  • 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

New signatures have been added for the following functions:

fn:tokenize

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 the following features and functions will be added 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).
  • Map lookup operator (?): returns the value of a map for a specific key. 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.