Changes

Jump to navigation Jump to search
3,586 bytes added ,  14:53, 12 December 2017
This article is part of the [[XQuery|XQuery Portal]].It summarizes provides a summary of the new most important features of the [http://www.w3.org/TR/xquery-31/ XQuery 3.1] Working Draftthat are already supported by BaseXRecommendation.
==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 {{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).
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. In BaseX, arrays (as well as sequences) are based on an efficient [http://en.wikipedia.org/wiki/Finger_tree Finger Tree] implementation.
Arrays can be constructed in two ways. With the square bracket notation, the comma serves as delimiter:
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.
===Atomization===
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:
Arrays can be compared with the {{Code|fn:deep-equal}} function. The [[Array Module]] describes the available set of array functions.
==Lookup Operator==
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:
# A wildcard {{Code|*}},
<pre class="brush:xquery">
for let $map in maps := (
map { 'name': 'Guðrún', 'city': 'Reykjavík' },
map { 'name': 'Hildur', 'city': 'Akureyri' }
)
return $mapmaps[?name = 'Hildur'] ?city
</pre>
==Arrow Operator==
The arrow operator applies <code>=></code> provides a function convenient alternative syntax for passing on functions to a value. The value is used expression that precedes the operator will be supplied as the first argument to of the function. It is introduced with the characters <code>=></code>, and it is followed by that follows the function to be calledarrow. 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:
<pre class="brush:xquery">
The syntax makes nested function calls more readable, as it is easy to see if parentheses are balanced.
=String Constructor= The string constructor has been inspired by [https://en.wikipedia.org/wiki/Here_document here document] literals of the Unix shell and script languages. It allows you to generate strings that contain various characters that would otherwise be interpreted as XQuery delimiters. The string constructors syntax uses two backticks and a square bracket for opening and closing a string: <pre class="brush:xquery">(: Returns "This is a 'new' & 'flexible' syntax." :)``["This is a 'new' & 'flexible' syntax."]``</pre> 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: <pre class="brush:xquery">(: Returns »Count 1 2 3, and I will be there.« :)let $c := 1 to 3return ``[»Count `{ $c }`, and I will be there.«]``</pre> =Serialization==
Two [[Serialization]] methods have been added to the [http://www.w3.org/TR/xslt-xquery-serialization-31 Serialization spec]:
===Adaptive Serialization=== The {{Code|adaptive}} serialization provides an intuitive textual representation for all XDM types, including maps and arrays, functions, attributes, and namespaces. All items will be separated by the value of the {{Code|item-separator}} parameter, which by default is a newline character. It is utilized by the functions <code>[[Profiling Module#prof:dump|prof:dump]]</code> and <code>[http://www.w3.org/TR/xpath-functions-31/#func-trace fn:trace]</code>.
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 specifiedExample:
<pre class="brush:xquery">
declare option output:method 'adaptive';
<element id='id0'/>/@id,
xs:token("abc"),
map { 'key': 'value' },
true#0
<pre class="brush:xml">
id="id0"xs:token("abc"),map {
"key": "value"
}
function fn:true#0
</pre>
===JSON Serialization===
The new {{Code|json}} serialization output method can be used to serialize XQuery maps, arrays, atomic values and empty sequences as JSON.
The {{Code|json}} output method has been introduced in BaseX quite a while agobefore it was added to the official specification. The implementation of this method now It complies with the standard serialization rules and, at the same time, preserves the existing semantics:
* If an XML node of type {{Code|element(json)}} is found, it will be serialized following the serialization rules of the [[JSON Module]].
</pre>
==Functions==
The following functions of have been added in 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 changeSpecification:
===Map Functions=== The following map functions are now available:
<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>
Please check out the [[Map Module]] for more details.
===Array Functions=== The following array functions are now available:
<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>
Please check out the [[Array Module]] for more details.==JSON Functions==
===With XQuery 3.1, native support for JSON Functions===objects was added. Strings and resources can be parsed to XQuery items and, as [[#JSON Serialization|shown above]], serialized back to their original form.
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. ====fn:parse-json====
; Signatures
</pre>
====fn:json-docs=doc===
; Signatures
</pre>
===fn:json-to-xml=== ; Signatures* <code>fn:json-to-xml($string as xs:string?) as node()?</code> 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]. <pre class="brush:xquery">json-to-xml('{ "message": "world" }') (: result:<map xmlns="http://www.w3.org/2005/xpath-functions"> <string key="message">world</string></map> :)</pre> ===fn:xml-to-json=== ; Signatures* <code>fn:xml-to-json($node as node()?) as xs:string?</code> 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]. <pre class="brush:xquery">(: returns "JSON" :)xml-to-json(<string xmlns="http://www.w3.org/2005/xpath-functions">JSON</string>)</pre> ==fn:sort===
; Signatures
* <code>fn:sort($input as item()*) as item()*</code>
* <code>fn:sort($input as item()*, $collation as xs:string?) as xs:anyAtomicType*)) as item()*</code>* <code>fn:sort($input as item()*, $collation as xs:string?, $key as function(item()*) as xs:anyAtomicType*)) as item()*</code>
Returns a new sequence with sorted {{Code|$input}} items, using an optional {{Code|$collation}}. If a sort {{Code|$key}} function is givensupplied, it will be applied on all items. The items of the resulting values will be sorted using the semantics of the {{Code|lt}} expression.
<pre class="brush:xquery">
sort(reverse(1 to 3)) (: yields 1, 2, 3 :),reverse(sort(1 to 3)) (: returns the sorted order in descending order :),sort((3, -2, 1), (), abs#1) (: yields 1, -2, 3 :),sort((1,2,3), (), function($x) { -$x }) (: yields 3, 2, 1 :),sort((1, 'a')) (: yields an error, as strings and integers cannot be compared :)
</pre>
===fn:contains-token===
; Signatures
</pre>
===fn:parse-ietf-date===
; Signature
</pre>
===fn:apply===
; Signatures
* <code>fn:apply($function as function(*), $array arguments as array(*)) as item()*</code>
A The supplied {{Code|$function }} is invoked with the specified {{Code|$arguments supplied by an array}}. The arity of the function must be the same as the size of the array.
Example:
fn:apply(concat#5, array { 1 to 5 }) (: 12345 :)
fn:apply(function($a) { sum($a) }, [ 1 to 5 ]) (: 15 :)
fn:apply(count#1, [ 1,2 ]) (: error (. the array has two members) :)
</pre>
===fn:random-number-generator===
; Signatures
</pre>
===fn:format-number===
The function has been extended to support scientific notation:
</pre>
===fn:tokenize===
If no separator is specified as second argument, a string will be tokenized at whitespace boundaries:
</pre>
===fn:trace===
The second argument can now be omitted:
</pre>
==Binary Datafn:string-join==
The type of the first argument is now <code>xs:anyAtomicType*</code>, and all items will be implicitly cast to strings: <pre class="brush:xquery">fn:string-join(1 to 3) (: yields the string "123" :)</pre> ==fn:default-language== Returns the default language used for formatting numbers and dates. BaseX always returns {{Code|en}}. ==Appendix== The three functions <code>fn:transform</code>, <code>fn:load-xquery-module</code> and <code>fn:collation-key</code> may be added in a future version of BaseX as their implementation might require the use of additional external libraries. =Binary Data= 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}}:
<pre class="brush:xquery">
</pre>
==Collations==
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>:
<pre class="brush:xquery">
</pre>
=Enclosed Expressions=Pending Features= ''Enclosed expression'' is the syntactical term for the expressions that are specified inside a function body, try/catch clauses, node constructors and some other expressions. In the following example expressions, its the empty sequence: <pre class="brush:xquery">declare function local:x() { () }; itry { () } catch * { () },element x { () },text { () }</pre>
The features of With XQuery 3.1 are still subject to change, but we are planning the expression can be omitted. The following query is equivalent to add the following enhancements in near futureupper one:
* New functions: <code>fnpre class="brush:collation-key</code>, <code>fn:load-xquery-module</code">, <code>fndeclare function local:transform</code>, <code>fn:json-to-xml</code>x() { };try { } catch * { }, <code>fn:xml-to-jsonelement x { }text { }</codepre>
=Changelog=
 
;Version 8.6
 
* Updated: Collation argument was inserted between first and second argument.
 
;Version 8.4
 
* Added: [[#String Constructors|String Constructors]], [[#fn:default-language|fn:default-language]], [[#Enclosed Expressions|Enclosed Expressions]]
* Updated: [[#Adaptive Serialization|Adaptive Serialization]], [[#fn:string-join|fn:string-join]]
 
;Version 8.2
 
* Added: [[#fn:json-to-xml|fn:json-to-xml]], [[#fn:xml-to-json|fn:xml-to-json]].
 
;Version 8.1
 
* Updated: arrays are now based on a [http://en.wikipedia.org/wiki/Finger_tree Finger Tree] implementation.
Introduced with Version 8.0.
 
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu