Changes

Jump to navigation Jump to search
4,087 bytes removed ,  12:58, 26 October 2023
no edit summary
This [[Module Library|XQuery Module]] contains various some 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. With {{Announce|Version 11}}, various many functions of this module have been removed as they are now part of the official specification:
{|
=> util:strip-namespaces('')
</syntaxhighlight>
|}
 
=Array and Map Functions=
 
==util:map-entries==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>util:map-entries(
$map as map(*)
) as map(xs:string, item()*)*</pre>
|- valign="top"
| '''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>
|- valign="top"
| '''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 util:map-entries($map)
return element { $entry?key } { string-join($entry?value) }
</syntaxhighlight>
|}
 
==util:map-values==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>util:map-values(
$map as map(*)
) as item()*</pre>
|- valign="top"
| '''Summary'''
|Returns all values of a {{Code|$map}} as a sequence. Equivalent to:
<syntaxhighlight lang="xquery">
$map ? *
</syntaxhighlight>
|- valign="top"
| '''Examples'''
|
* Returns the map values as two items:
<syntaxhighlight lang="xquery">
let $map := map { 'a': (), 'b': 2, 'c': [ 3, 4 ] }
return util:map-values($map)
</syntaxhighlight>
|}
 
=Helper Functions=
 
==util:replicate==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>util:replicate(
$input as item()*,
$count as xs:integer,
$repeat as xs:boolean? := false()
) as item()*</pre>
|- valign="top"
| '''Summary'''
|Evaluates {{Code|$input}} and returns the result {{Code|$count}} times. Unless {{Code|$repeat}} is set to true, the input expression is evaluated multiple times. Equivalent expressions:
<syntaxhighlight lang="xquery">
util:replicate($input, $count, true()),
(1 to $count) ! $input
</syntaxhighlight>
|- valign="top"
| '''Errors'''
|{{Error|negative|#Errors}} The specified number is negative.
|- valign="top"
| '''Examples'''
|
* <code>util:replicate('A', 3)</code> returns <code>A A A</code>.
* In the following query, a single new element node is constructed, and {{Code|true}} is returned:
<syntaxhighlight lang="xquery">
let $nodes := util:replicate(<node/>, 2)
return $nodes[1] is $nodes[2]
</syntaxhighlight>
* In this query, two nodes are constructed, and the result is {{Code|false}}:
<syntaxhighlight lang="xquery">
let $nodes := util:replicate(<node/>, 2, true())
return $nodes[1] is $nodes[2]
</syntaxhighlight>
|}
 
==util:duplicates==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>util:duplicates(
$sequence as item()*,
$collation as xs:string := ()
) as xs:anyAtomicType*</pre>
|- valign="top"
| '''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.
|- valign="top"
| '''Examples'''
|
* <code>util:duplicates((1, 2, 1, 1))</code> returns <code>1</code>.
|}
 
==util:chars==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>util:chars(
$string as xs:string?
) as xs:string*</pre>
|- valign="top"
| '''Summary'''
|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>
|- valign="top"
| '''Examples'''
|
* <code>util:chars('AB')</code> returns the two strings <code>A</code> and <code>B</code>.
|}
Bureaucrats, editor, reviewer, Administrators
13,551

edits

Navigation menu