Difference between revisions of "Utility Module"
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | This [[Module Library|XQuery Module]] contains various | + | This [[Module Library|XQuery Module]] contains various utility and helper functions. |
+ | |||
+ | For all listed functions, equivalent expressions exist in standard XQuery, but code may be better readable with function calls: | ||
+ | |||
+ | <syntaxhighlight lang="xquery"> | ||
+ | (: standard XQuery :) | ||
+ | let $result := if(exists($sequence)) then $sequence else ('default', 'values') | ||
+ | return $result[last()] | ||
+ | |||
+ | (: XQuery with functions of this module :) | ||
+ | $sequence | ||
+ | => util:or(('default', 'values')) | ||
+ | => util:last() | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | In addition, various query optimizations create calls to the utility functions. | ||
=Conventions= | =Conventions= | ||
Line 33: | Line 48: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns {{Code|$items}} if it is a non-empty sequence. Otherwise, returns {{Code|$default}}. | + | |Returns {{Code|$items}} if it is a non-empty sequence. Otherwise, returns {{Code|$default}}. Equivalent to the following expressions: |
− | + | <syntaxhighlight lang="xquery"> | |
− | + | if(exists($items)) then $items else $default, | |
+ | (: Elvis operator :) | ||
+ | $items ?: $default | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Examples''' | | '''Examples''' | ||
Line 53: | Line 71: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Checks if the specified {{Code|$sequence}} has at least {{Code|$min}} and, optionally, at most {{Code|$max}} items. | + | |Checks if the specified {{Code|$sequence}} has at least {{Code|$min}} and, optionally, at most {{Code|$max}} items. Equivalent to: |
+ | <syntaxhighlight lang="xquery"> | ||
+ | let $count := count($sequence) | ||
+ | return $count >= $min and $count <= $max | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Examples''' | | '''Examples''' | ||
Line 71: | Line 93: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns the item from {{Code|$sequence}} at the specified {{Code|$position}}. Equivalent to < | + | |Returns the item from {{Code|$sequence}} at the specified {{Code|$position}}. Equivalent to: |
+ | <syntaxhighlight lang="xquery"> | ||
+ | $sequence[$position] | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Examples''' | | '''Examples''' | ||
Line 87: | Line 112: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns items from {{Code|$sequence}}, starting at position {{Code|$first}} and ending at {{Code|$last}}. Equivalent to < | + | |Returns items from {{Code|$sequence}}, starting at position {{Code|$first}} and ending at {{Code|$last}}. Equivalent to: |
+ | <syntaxhighlight lang="xquery"> | ||
+ | subsequence($sequence, $first, $last - $first + 1) | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Examples''' | | '''Examples''' | ||
Line 102: | Line 130: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns last item of a {{Code|$sequence}}. Equivalent to < | + | |Returns last item of a {{Code|$sequence}}. Equivalent to: |
+ | <syntaxhighlight lang="xquery"> | ||
+ | $sequence[last()] | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Examples''' | | '''Examples''' | ||
Line 117: | Line 148: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns all items of a {{Code|$sequence}} except for the last one. Equivalent to < | + | |Returns all items of a {{Code|$sequence}} except for the last one. Equivalent to: |
+ | <syntaxhighlight lang="xquery"> | ||
+ | $sequence[position() < last()] | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Examples''' | | '''Examples''' | ||
Line 134: | Line 168: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns nodes in ''distinct document order'': duplicate nodes will be removed, and the remaining nodes will be returned in [https://www.w3.org/TR/xquery-31/#dt-document-order document order]. | + | |Returns nodes in ''distinct document order'': duplicate nodes will be removed, and the remaining nodes will be returned in [https://www.w3.org/TR/xquery-31/#dt-document-order document order]. As results of path expressions are brought distinct document order before they are returned, the function is equivalent to: |
+ | <syntaxhighlight lang="xquery"> | ||
+ | $nodes/self::node() | ||
+ | </syntaxhighlight> | ||
|} | |} | ||
+ | |||
+ | ==util:root== | ||
{| width='100%' | {| width='100%' | ||
Line 143: | Line 182: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns the document nodes of the specified {{Code|$nodes}}. The | + | |Returns the document nodes of the specified {{Code|$nodes}}. The path expression <code>/abc</code> is internally represented as <code>util:root(.)/abc</code. Equivalent to: |
− | + | <syntaxhighlight lang="xquery"> | |
+ | $nodes ! / | ||
+ | </syntaxhighlight> | ||
+ | |} | ||
+ | |||
+ | =Array and Map Functions= | ||
+ | |||
+ | ==util:array-members== | ||
+ | |||
+ | {{Mark|Introduced with Version 9.5.}} | ||
+ | |||
+ | {| width='100%' | ||
+ | |- | ||
+ | | width='120' | '''Signatures''' | ||
+ | |{{Func|util:array-members|$array as array(*)|array(*)*}} | ||
+ | |- | ||
+ | | '''Summary''' | ||
+ | |Returns each member of an {{Code|$array}} as a new array. Equivalent to: | ||
+ | <syntaxhighlight lang="xquery"> | ||
+ | for $a in 1 to array:size($array) | ||
+ | return [ $array($a) ] | ||
+ | </syntaxhighlight> | ||
+ | |- | ||
+ | | '''Examples''' | ||
+ | | | ||
+ | * Returns three elements with the member values as concatenated text node. | ||
+ | <syntaxhighlight lang="xquery"> | ||
+ | let $array := [ (), 2, (3, 4) ] | ||
+ | for $member in array:members($array) | ||
+ | return element numbers { $member } | ||
+ | </syntaxhighlight> | ||
+ | |} | ||
+ | |||
+ | ==util:array-values== | ||
+ | |||
+ | {{Mark|Introduced with Version 9.5.}} | ||
+ | |||
+ | {| width='100%' | ||
+ | |- | ||
+ | | width='120' | '''Signatures''' | ||
+ | |{{Func|util:array-values|$array as array(*)|item()*}} | ||
+ | |- | ||
+ | | '''Summary''' | ||
+ | |Returns all members of an {{Code|$array}} as a sequence. Equivalent to: | ||
+ | <syntaxhighlight lang="xquery"> | ||
+ | $array ? * | ||
+ | </syntaxhighlight> | ||
+ | |- | ||
+ | | '''Examples''' | ||
+ | | | ||
+ | * Returns the array members as two items: | ||
+ | <syntaxhighlight lang="xquery"> | ||
+ | let $array := [ (), 2, [ 3, 4 ] ] | ||
+ | return array:values($array) | ||
+ | </syntaxhighlight> | ||
+ | |} | ||
+ | |||
+ | ==util:map-entries== | ||
+ | |||
+ | {{Mark|Introduced with Version 9.5.}} | ||
+ | |||
+ | {| width='100%' | ||
+ | |- | ||
+ | | width='120' | '''Signatures''' | ||
+ | |{{Func|util:map-entries|$map as map(*)|map(xs:string, item()*)*}} | ||
+ | |- | ||
+ | | '''Summary''' | ||
+ | |Returns each entry of a {{Code|$map}} as a new map, each with a {{Code|key}} and {{Code|value}} entry. Equivalent to: | ||
+ | <syntaxhighlight lang="xquery"> | ||
+ | map:for-each($map, function($key, $value) { | ||
+ | map { "key": $key, "value": $value } | ||
+ | }) | ||
+ | </syntaxhighlight> | ||
+ | |- | ||
+ | | '''Examples''' | ||
+ | | | ||
+ | * Returns three elements named by the key of the map, and with the entries as concatenated text node. | ||
+ | <syntaxhighlight lang="xquery"> | ||
+ | let $map := map { 'a': (), 'b': 2, 'c': [ 3, 4 ] } | ||
+ | for $entry in map:entries($map) | ||
+ | return element { $entry?key } { string-join($entry?value) } | ||
+ | </syntaxhighlight> | ||
+ | |} | ||
+ | |||
+ | ==util:map-values== | ||
+ | |||
+ | {{Mark|Introduced with Version 9.5.}} | ||
+ | {| width='100%' | ||
+ | |- | ||
+ | | width='120' | '''Signatures''' | ||
+ | |{{Func|util:map-values|$map as map(*)|item()*}} | ||
+ | |- | ||
+ | | '''Summary''' | ||
+ | |Returns all values of a {{Code|$map}} as a sequence. Equivalent to: | ||
+ | <syntaxhighlight lang="xquery"> | ||
+ | $map ? * | ||
+ | </syntaxhighlight> | ||
+ | |- | ||
+ | | '''Examples''' | ||
+ | | | ||
+ | * Returns the map values as two items: | ||
+ | <syntaxhighlight lang="xquery"> | ||
+ | let $map := map { 'a': (), 'b': 2, 'c': [ 3, 4 ] } | ||
+ | return map:values($map) | ||
+ | </syntaxhighlight> | ||
|} | |} | ||
Line 160: | Line 303: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Evaluates {{Code|$input}} and returns the result {{Code|$count}} times. Unless {{Code|$multiple}} is enabled, the input expression is only evaluated once. | + | |Evaluates {{Code|$input}} and returns the result {{Code|$count}} times. Unless {{Code|$multiple}} is enabled, the input expression is only evaluated once. Equivalent expressions: |
+ | <syntaxhighlight lang="xquery"> | ||
+ | util:replicate($input, $count, true()), | ||
+ | (1 to $count) ! $input | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Errors''' | | '''Errors''' | ||
Line 190: | Line 337: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Inserts the defined {{Code|$separator}} between the {{Code|$items}} of a sequence and returns the resulting sequence. | + | |Inserts the defined {{Code|$separator}} between the {{Code|$items}} of a sequence and returns the resulting sequence. Equivalent to: |
− | + | <syntaxhighlight lang="xquery"> | |
+ | head($items), for $item in tail($items) return ($separator, $item) | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Examples''' | | '''Examples''' | ||
Line 198: | Line 347: | ||
fn:intersperse((<_>1</_>, <_>2</_>, <_>3</_>), '; ') | fn:intersperse((<_>1</_>, <_>2</_>, <_>3</_>), '; ') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |} | ||
+ | |||
+ | ==util:duplicates== | ||
+ | |||
+ | {{Mark|Introduced with Version 9.5.}} | ||
+ | |||
+ | {| width='100%' | ||
+ | |- | ||
+ | | width='120' | '''Signatures''' | ||
+ | |{{Func|util:duplicates|$sequence as item()*|xs:anyAtomicType*}}<br/>{{Func|util:duplicates|$sequence as item()*, $collation as xs:string|xs:anyAtomicType*}} | ||
+ | |- | ||
+ | | '''Summary''' | ||
+ | |Returns duplicate values in a {{Code|$sequence}}. See [https://www.w3.org/TR/xpath-functions-31/#func-distinct-values fn:distinct-values] for the applied equality rules and the usage of the {{Code|$collation}} argument. | ||
+ | |- | ||
+ | | '''Examples''' | ||
+ | | | ||
+ | * <code>util:duplicates((1, 2, 1, 1))</code> returns <code>1</code>. | ||
|} | |} | ||
Line 208: | Line 374: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns all characters of a {{Code|$string}} as a sequence. Equivalent to < | + | |Returns all characters of a {{Code|$string}} as a sequence. Equivalent to: |
+ | <syntaxhighlight lang="xquery"> | ||
+ | for $cp in string-to-codepoints($string) | ||
+ | return codepoints-to-string($cp) | ||
+ | </syntaxhighlight> | ||
|- | |- | ||
| '''Examples''' | | '''Examples''' | ||
Line 228: | Line 398: | ||
;Version 9.5 | ;Version 9.5 | ||
− | * Added: [[#util:intersperse|util:intersperse]], [[#util:within|util:within]] | + | * Added: [[#util:intersperse|util:intersperse]], [[#util:within|util:within]], [[#util:duplicates|util:duplicates]], [[#util:array-members|util:array-members]], [[#util:array-values|util:array-values]], [[#util:map-entries|util:map-entries]], [[#util:map-values|util:map-values]] |
* Updated: [[#util:replicate|util:replicate]]: Third argument added. | * Updated: [[#util:replicate|util:replicate]]: Third argument added. | ||
Revision as of 12:17, 25 February 2021
This XQuery Module contains various utility and helper functions.
For all listed functions, equivalent expressions exist in standard XQuery, but code may be better readable with function calls:
<syntaxhighlight lang="xquery"> (: standard XQuery :) let $result := if(exists($sequence)) then $sequence else ('default', 'values') return $result[last()]
(: XQuery with functions of this module :) $sequence => util:or(('default', 'values')) => util:last() </syntaxhighlight>
In addition, various query optimizations create calls to the utility functions.
Contents
Conventions
All functions and errors in this module and errors are assigned to the http://basex.org/modules/util
namespace, which is statically bound to the util
prefix.
Conditions
util:if
Signatures | util:if($condition as item()*, $then as item()*) as item()* util:if($condition as item()*, $then as item()*, $else as item()*) as item()* |
Summary | Alternative writing for the if/then/else expression:
|
Examples |
|
util:or
Signatures | util:or($items as item()*, $default as item()*) as item()*
|
Summary | Returns $items if it is a non-empty sequence. Otherwise, returns $default . Equivalent to the following expressions:
<syntaxhighlight lang="xquery"> if(exists($items)) then $items else $default, (: Elvis operator :) $items ?: $default </syntaxhighlight> |
Examples |
|
util:within
Signatures | util:within($sequence as item()*, $min as xs:integer) as xs:boolean util:within($sequence as item()*, $min as xs:integer, $max as xs:integer) as xs:boolean
|
Summary | Checks if the specified $sequence has at least $min and, optionally, at most $max items. Equivalent to:
<syntaxhighlight lang="xquery"> let $count := count($sequence) return $count >= $min and $count <= $max </syntaxhighlight> |
Examples |
|
Positional Access
util:item
Signatures | util:item($sequence as item()*, $position as xs:double) as item()? |
Summary | Returns the item from $sequence at the specified $position . Equivalent to:
<syntaxhighlight lang="xquery"> $sequence[$position] </syntaxhighlight> |
Examples |
|
util:range
Signatures | util:range($sequence as item()*, $first as xs:double, $last as xs:double) as item()* |
Summary | Returns items from $sequence , starting at position $first and ending at $last . Equivalent to:
<syntaxhighlight lang="xquery"> subsequence($sequence, $first, $last - $first + 1) </syntaxhighlight> |
Examples |
|
util:last
Signatures | util:last($sequence as item()*) as item()? |
Summary | Returns last item of a $sequence . Equivalent to:
<syntaxhighlight lang="xquery"> $sequence[last()] </syntaxhighlight> |
Examples |
|
util:init
Signatures | util:init($sequence as item()*) as item()* |
Summary | Returns all items of a $sequence except for the last one. Equivalent to:
<syntaxhighlight lang="xquery"> $sequence[position() < last()] </syntaxhighlight> |
Examples |
|
Node Functions
util:ddo
Signatures | util:ddo($nodes as node()*) as node()* |
Summary | Returns nodes in distinct document order: duplicate nodes will be removed, and the remaining nodes will be returned in document order. As results of path expressions are brought distinct document order before they are returned, the function is equivalent to:
<syntaxhighlight lang="xquery"> $nodes/self::node() </syntaxhighlight> |
util:root
Signatures | util:root($nodes as node()*) as document-node()* |
Summary | Returns the document nodes of the specified $nodes . The path expression /abc is internally represented as util:root(.)/abc</code. Equivalent to:
|
Array and Map Functions
util:array-members
Signatures | util:array-members($array as array(*)) as array(*)*
|
Summary | Returns each member of an $array as a new array. Equivalent to:
<syntaxhighlight lang="xquery"> for $a in 1 to array:size($array) return [ $array($a) ] </syntaxhighlight> |
Examples |
<syntaxhighlight lang="xquery"> let $array := [ (), 2, (3, 4) ] for $member in array:members($array) return element numbers { $member } </syntaxhighlight> |
util:array-values
Signatures | util:array-values($array as array(*)) as item()*
|
Summary | Returns all members of an $array as a sequence. Equivalent to:
<syntaxhighlight lang="xquery"> $array ? * </syntaxhighlight> |
Examples |
<syntaxhighlight lang="xquery"> let $array := [ (), 2, [ 3, 4 ] ] return array:values($array) </syntaxhighlight> |
util:map-entries
Signatures | util:map-entries($map as map(*)) as map(xs:string, item()*)*
|
Summary | Returns each entry of a $map as a new map, each with a key and value entry. Equivalent to:
<syntaxhighlight lang="xquery"> map:for-each($map, function($key, $value) { map { "key": $key, "value": $value } }) </syntaxhighlight> |
Examples |
<syntaxhighlight lang="xquery"> let $map := map { 'a': (), 'b': 2, 'c': [ 3, 4 ] } for $entry in map:entries($map) return element { $entry?key } { string-join($entry?value) } </syntaxhighlight> |
util:map-values
Signatures | util:map-values($map as map(*)) as item()*
|
Summary | Returns all values of a $map as a sequence. Equivalent to:
<syntaxhighlight lang="xquery"> $map ? * </syntaxhighlight> |
Examples |
<syntaxhighlight lang="xquery"> let $map := map { 'a': (), 'b': 2, 'c': [ 3, 4 ] } return map:values($map) </syntaxhighlight> |
Helper Functions
util:replicate
Template:Mark Third argument added.
Signatures | util:replicate($input as item()*, $count as xs:integer) as item()* util:replicate($input as item()*, $count as xs:integer, $multiple as xs:boolean) as item()*
|
Summary | Evaluates $input and returns the result $count times. Unless $multiple is enabled, the input expression is only evaluated once. Equivalent expressions:
<syntaxhighlight lang="xquery"> util:replicate($input, $count, true()), (1 to $count) ! $input </syntaxhighlight> |
Errors | negative : The specified number is negative.
|
Examples |
<syntaxhighlight lang="xquery"> let $nodes := util:replicate(<node/>, 2) return $nodes[1] is $nodes[2] </syntaxhighlight>
<syntaxhighlight lang="xquery"> let $nodes := util:replicate(<node/>, 2, true()) return $nodes[1] is $nodes[2] </syntaxhighlight> |
util:intersperse
Signatures | util:intersperse($items as item()*, $separator as item()*) as item()*
|
Summary | Inserts the defined $separator between the $items of a sequence and returns the resulting sequence. Equivalent to:
<syntaxhighlight lang="xquery"> head($items), for $item in tail($items) return ($separator, $item) </syntaxhighlight> |
Examples | Inserts semicolon strings between the three input items:
<syntaxhighlight lang="xquery"> fn:intersperse((<_>1</_>, <_>2</_>, <_>3</_>), '; ') </syntaxhighlight> |
util:duplicates
Signatures | util:duplicates($sequence as item()*) as xs:anyAtomicType* util:duplicates($sequence as item()*, $collation as xs:string) as xs:anyAtomicType*
|
Summary | Returns duplicate values in a $sequence . See fn:distinct-values for the applied equality rules and the usage of the $collation argument.
|
Examples |
|
util:chars
Signatures | util:chars($string as xs:string) as xs:string* |
Summary | Returns all characters of a $string as a sequence. Equivalent to:
<syntaxhighlight lang="xquery"> for $cp in string-to-codepoints($string) return codepoints-to-string($cp) </syntaxhighlight> |
Examples |
|
Errors
Code | Description |
---|---|
negative
|
The specified number is negative. |
Changelog
- Version 9.5
- Added: util:intersperse, util:within, util:duplicates, util:array-members, util:array-values, util:map-entries, util:map-values
- Updated: util:replicate: Third argument added.
- Version 9.4
- Added: util:root
- Version 9.3
- Added: util:ddo
- Version 9.2
- Added: util:chars, util:init
- Updated: util:item, util:last, util:range renamed (before:
util:item-at
,util:item-range
,util:last-from
)
- Version 9.1
- Version 9.0
- Added: util:replicate
The Module was introduced with Version 8.5.