Difference between revisions of "Utility Module"

From BaseX Documentation
Jump to navigation Jump to search
m (Text replacement - "\[\[#([^]:]+:[^|]+)\|([^]:]+:[^|]+)\]\]" to "{{Function||$1}}")
Line 25: Line 25:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:if|$condition as item()*, $then as item()*|item()*}}<br/>{{Func|util:if|$condition as item()*, $then as item()*, $else as item()*|item()*}}<br/>
 
|{{Func|util:if|$condition as item()*, $then as item()*|item()*}}<br/>{{Func|util:if|$condition as item()*, $then as item()*, $else as item()*|item()*}}<br/>
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Alternative writing for the if/then/else expression:
 
|Alternative writing for the if/then/else expression:
 
* If the ''effective boolean value'' of {{Code|$condition}} is true, the {{Code|$then}} branch will be evaluated.
 
* If the ''effective boolean value'' of {{Code|$condition}} is true, the {{Code|$then}} branch will be evaluated.
 
* Otherwise, {{Code|$else}} will be evaluated. If no third argument is supplied, an empty sequence will be returned.
 
* Otherwise, {{Code|$else}} will be evaluated. If no third argument is supplied, an empty sequence will be returned.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 43: Line 43:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:or|$items as item()*, $default as item()*|item()*}}
 
|{{Func|util:or|$items as item()*, $default as item()*|item()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns {{Code|$items}} if it is a non-empty sequence. Otherwise, returns {{Code|$default}}. Equivalent to the following expressions:
 
|Returns {{Code|$items}} if it is a non-empty sequence. Otherwise, returns {{Code|$default}}. Equivalent to the following expressions:
Line 54: Line 54:
 
$items ?: $default
 
$items ?: $default
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 66: Line 66:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:count-within|$sequence as item()*, $min as xs:integer|xs:boolean}}<br/>{{Func|util:count-within|$sequence as item()*, $min as xs:integer, $max as xs:integer|xs:boolean}}
 
|{{Func|util:count-within|$sequence as item()*, $min as xs:integer|xs:boolean}}<br/>{{Func|util:count-within|$sequence as item()*, $min as xs:integer, $max as xs:integer|xs:boolean}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Checks if the specified {{Code|$sequence}} has at least {{Code|$min}} and, optionally, at most {{Code|$max}} items. Equivalent to:
 
|Checks if the specified {{Code|$sequence}} has at least {{Code|$min}} and, optionally, at most {{Code|$max}} items. Equivalent to:
Line 76: Line 76:
 
return $count >= $min and $count <= $max
 
return $count >= $min and $count <= $max
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 88: Line 88:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:item|$sequence as item()*, $position as xs:double|item()?}}<br/>
 
|{{Func|util:item|$sequence as item()*, $position as xs:double|item()?}}<br/>
|-
+
|- valign="top"
 
| '''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:
Line 97: Line 97:
 
$sequence[$position]
 
$sequence[$position]
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 107: Line 107:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:range|$sequence as item()*, $first as xs:double, $last as xs:double|item()*}}<br/>
 
|{{Func|util:range|$sequence as item()*, $first as xs:double, $last as xs:double|item()*}}<br/>
|-
+
|- valign="top"
 
| '''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:
Line 116: Line 116:
 
subsequence($sequence, $first, $last - $first + 1)
 
subsequence($sequence, $first, $last - $first + 1)
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 125: Line 125:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:last|$sequence as item()*|item()?}}<br/>
 
|{{Func|util:last|$sequence as item()*|item()?}}<br/>
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns last item of a {{Code|$sequence}}. Equivalent to:
 
|Returns last item of a {{Code|$sequence}}. Equivalent to:
Line 134: Line 134:
 
$sequence[last()]
 
$sequence[last()]
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 143: Line 143:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:init|$sequence as item()*|item()*}}<br/>
 
|{{Func|util:init|$sequence as item()*|item()*}}<br/>
|-
+
|- valign="top"
 
| '''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:
Line 152: Line 152:
 
$sequence[position() < last()]
 
$sequence[position() < last()]
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 163: Line 163:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:ddo|$nodes as node()*|node()*}}<br/>
 
|{{Func|util:ddo|$nodes as node()*|node()*}}<br/>
|-
+
|- valign="top"
 
| '''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]. As results of path expressions are brought distinct document order before they are returned, the function is equivalent to:
 
|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:
Line 177: Line 177:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:root|$nodes as node()*|document-node()*}}<br/>
 
|{{Func|util:root|$nodes as node()*|document-node()*}}<br/>
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|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:
 
|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:
Line 191: Line 191:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:strip-namespaces|$node as node()|node()}}<br/>{{Func|util:strip-namespaces|$node as node(), $prefixes as xs:string*|node()}}<br/>
 
|{{Func|util:strip-namespaces|$node as node()|node()}}<br/>{{Func|util:strip-namespaces|$node as node(), $prefixes as xs:string*|node()}}<br/>
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Removes namespaces with the specified <code>$prefixes</code> from the supplied <code>$node</code>. An empty string can be supplied to remove the default namespace. If no prefixes are specified, all namespaces will be removed.
 
|Removes namespaces with the specified <code>$prefixes</code> from the supplied <code>$node</code>. An empty string can be supplied to remove the default namespace. If no prefixes are specified, all namespaces will be removed.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 219: Line 219:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:array-members|$array as array(*)|array(*)*}}
 
|{{Func|util:array-members|$array as array(*)|array(*)*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns each member of an {{Code|$array}} as a new array. Equivalent to:
 
|Returns each member of an {{Code|$array}} as a new array. Equivalent to:
Line 229: Line 229:
 
return [ $array($a) ]
 
return [ $array($a) ]
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 243: Line 243:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:array-values|$array as array(*)|item()*}}
 
|{{Func|util:array-values|$array as array(*)|item()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns all members of an {{Code|$array}} as a sequence. Equivalent to:
 
|Returns all members of an {{Code|$array}} as a sequence. Equivalent to:
Line 252: Line 252:
 
$array ? *
 
$array ? *
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 265: Line 265:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:map-entries|$map as map(*)|map(xs:string, item()*)*}}
 
|{{Func|util:map-entries|$map as map(*)|map(xs:string, item()*)*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns each entry of a {{Code|$map}} as a new map, each with a {{Code|key}} and {{Code|value}} entry. Equivalent to:
 
|Returns each entry of a {{Code|$map}} as a new map, each with a {{Code|key}} and {{Code|value}} entry. Equivalent to:
Line 276: Line 276:
 
})
 
})
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 290: Line 290:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:map-values|$map as map(*)|item()*}}
 
|{{Func|util:map-values|$map as map(*)|item()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns all values of a {{Code|$map}} as a sequence. Equivalent to:
 
|Returns all values of a {{Code|$map}} as a sequence. Equivalent to:
Line 299: Line 299:
 
$map ? *
 
$map ? *
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 314: Line 314:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:replicate|$input as item()*, $count as xs:integer|item()*}}<br/>{{Func|util:replicate|$input as item()*, $count as xs:integer, $multiple as xs:boolean|item()*}}
 
|{{Func|util:replicate|$input as item()*, $count as xs:integer|item()*}}<br/>{{Func|util:replicate|$input as item()*, $count as xs:integer, $multiple as xs:boolean|item()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Evaluates {{Code|$input}} and returns the result {{Code|$count}} times. Unless {{Code|$multiple}} is enabled, the input expression is only evaluated once.  Equivalent expressions:
 
|Evaluates {{Code|$input}} and returns the result {{Code|$count}} times. Unless {{Code|$multiple}} is enabled, the input expression is only evaluated once.  Equivalent expressions:
Line 324: Line 324:
 
(1 to $count) ! $input
 
(1 to $count) ! $input
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|negative|#Errors}} The specified number is negative.
 
|{{Error|negative|#Errors}} The specified number is negative.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 346: Line 346:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:intersperse|$items as item()*, $separator as item()*|item()*}}
 
|{{Func|util:intersperse|$items as item()*, $separator as item()*|item()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Inserts the defined {{Code|$separator}} between the {{Code|$items}} of a sequence and returns the resulting sequence. Equivalent to:
 
|Inserts the defined {{Code|$separator}} between the {{Code|$items}} of a sequence and returns the resulting sequence. Equivalent to:
Line 355: Line 355:
 
head($items), for $item in tail($items) return ($separator, $item)
 
head($items), for $item in tail($items) return ($separator, $item)
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
| Inserts semicolon strings between the three input items:
 
| Inserts semicolon strings between the three input items:
Line 366: Line 366:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:duplicates|$sequence as item()*|xs:anyAtomicType*}}<br/>{{Func|util:duplicates|$sequence as item()*, $collation as xs:string|xs:anyAtomicType*}}
 
|{{Func|util:duplicates|$sequence as item()*|xs:anyAtomicType*}}<br/>{{Func|util:duplicates|$sequence as item()*, $collation as xs:string|xs:anyAtomicType*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''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.
 
|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'''
 
| '''Examples'''
 
|
 
|
Line 381: Line 381:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|util:chars|$string as xs:string|xs:string*}}<br/>
 
|{{Func|util:chars|$string as xs:string|xs:string*}}<br/>
|-
+
|- valign="top"
 
| '''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:
Line 391: Line 391:
 
return codepoints-to-string($cp)
 
return codepoints-to-string($cp)
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 402: Line 402:
 
! width="110"|Code
 
! width="110"|Code
 
|Description
 
|Description
|-
+
|- valign="top"
 
|{{Code|negative}}
 
|{{Code|negative}}
 
|The specified number is negative.
 
|The specified number is negative.

Revision as of 14:20, 20 July 2022

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.

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:
  • If the effective boolean value of $condition is true, the $then branch will be evaluated.
  • Otherwise, $else will be evaluated. If no third argument is supplied, an empty sequence will be returned.
Examples
  • util:if(true(), 123, 456) returns 123.
  • util:if(0, 'wrong!') returns an empty sequence.

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:or(123, 456) returns 123.
  • util:or(1[. = 0], -1) returns -1.

util:count-within

Updated with BaseX 10: Renamed from util:within

Signatures util:count-within($sequence as item()*, $min as xs:integer) as xs:boolean
util:count-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
  • util:count-within(('a', 'b', 'c'), 2) returns true.
  • util:count-within((1 to 1000000000)[. < 10], 3, 6) returns true.

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:item(reverse(1 to 5), 1) returns 5.
  • util:item(('a','b'), 0) returns an empty sequence.

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:range(//item, 11, 20) returns all path results from (if available) position 11 to 20.

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:last(reverse(1 to 100)) returns 1.

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
  • util:init(1 to 4) returns 1 2 3.

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:

<syntaxhighlight lang="xquery"> $nodes ! / </syntaxhighlight>

util:strip-namespaces

Signatures util:strip-namespaces($node as node()) as node()
util:strip-namespaces($node as node(), $prefixes as xs:string*) as node()
Summary Removes namespaces with the specified $prefixes from the supplied $node. An empty string can be supplied to remove the default namespace. If no prefixes are specified, all namespaces will be removed.
Examples
  • Remove all namespaces from an element and its descendants:

<syntaxhighlight lang="xquery"> util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>)

(: yields :) <xml name='value'><child/></xml> </syntaxhighlight>

  • Remove all default namespaces:

<syntaxhighlight lang="xquery"> <xml xmlns='uri1'><child xmlns='uri2'/></xml> => util:strip-namespaces() </syntaxhighlight>

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
  • Returns three elements with the member values as concatenated text node.

<syntaxhighlight lang="xquery"> let $array := [ (), 2, (3, 4) ] for $member in util: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
  • Returns the array members as two items:

<syntaxhighlight lang="xquery"> let $array := [ (), 2, [ 3, 4 ] ] return util: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
  • 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

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
  • 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

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
  • util:replicate('A', 3) returns A A A.
  • In the following query, a single new element node is constructed, and 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 false:

<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:duplicates((1, 2, 1, 1)) returns 1.

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
  • util:chars('AB') returns the two strings A and B.

Errors

Code Description
negative The specified number is negative.

Changelog

Version 9.7
Version 9.5
Version 9.4
Version 9.3
Version 9.2
Version 9.1
Version 9.0

The Module was introduced with Version 8.5.