Changes

Jump to navigation Jump to search
34 bytes added ,  12:56, 2 July 2020
m
Text replacement - "[http://en.wikipedia.org/" to "[https://en.wikipedia.org/"
This article is part of the [[XQuery|XQuery Portal]]. It provides a summary of the most important features of the [httphttps://www.w3.org/TR/xquery-31/ XQuery 3.1] Recommendation.
=Maps=
<syntaxhighlight lang="xquery">
 
map { }, (: empty map :)
map { 'key': true(), 1984: (<a/>, <b/>) }, (: map with two entries :)
</syntaxhighlight>
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[[Higher-order functions 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:
<syntaxhighlight lang="xquery">
 
let $map := map { 'foo': 42, 'bar': 'baz', 123: 456 }
return fn:for-each(map:keys($map), $map)
=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 [httphttps://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:
=Serialization=
Two [[Serialization]] methods have been added to the [httphttps://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>[httphttps://www.w3.org/TR/xpath-functions-31/#func-trace fn:trace]</code>.
Example:
* If an XML node of type {{Code|element(json)}} is found, it will be serialized following the serialization rules of the [[JSON Module]].
* Any other node or atomic value, map, array, or empty sequence will be serialized according to the [httphttps://www.w3.org/TR/xslt-xquery-serialization-31/#json-output rules in the specification].
The following two queries will both return the JSON snippet <code>{ "key": "value" }</code>:
=Functions=
The following functions have been added in the [httphttps://www.w3.org/TR/xpath-functions-31/ XQuery 3.1 Functions and Operators] Specification:
==Map Functions==
* <code>fn:parse-json($input as xs:string, $options as map(*)) as item()?</code>
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 [httphttps://www.w3.org/TR/xpath-functions-31/#func-parse-json specification].
<syntaxhighlight lang="xquery">
* <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 [httphttps://www.w3.org/TR/xsltxpath-30functions-31/#func-json-to-xml xm specification].
<syntaxhighlight lang="xquery">
* <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 [httphttps://www.w3.org/TR/xsltxpath-30functions-31/#func-xml-to-json specification].
<syntaxhighlight lang="xquery">
</syntaxhighlight>
If the [http://site.icu-project.org/download ICU Library] is downloaded and added to the classpath, the full [httphttps://www.w3.org/TR/xpath-functions-31/#uca-collations Unicode Collation Algorithm] features become available in BaseX:
<syntaxhighlight lang="xquery">
;Version 8.1
* Updated: arrays are now based on a [httphttps://en.wikipedia.org/wiki/Finger_tree Finger Tree] implementation.
Introduced with Version 8.0.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu