This [[Module Library|XQuery Module]] contains various small some utility and helper functions. Please note that some With {{Announce|Version 11}}, many functions have been removed in favor of new features of the XQuery 4: {||- valign="top"| '''BaseX 10'''| '''XQuery 4'''|- valign="top"| {{Code|util:array-members}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-members <code>array:members</code>]|- valign="top"| {{Code|util:array-values}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-values <code>array:values</code>]|- valign="top"| {{Code|util:chars}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-chars <code>fn:characters</code>]|- valign="top"| {{Code|util:duplicates}}| [https://qt4cg.org/specifications/xpath-functions are used for internal query rewritings-40/Overview. They may be renamed html#func-duplicate-values <code>fn:duplicate-values</code>]|- valign="top"| {{Code|util:init}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-trunk <code>fn:trunk</code>]|- valign="top"| {{Code|util:intersperse}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-intersperse <code>fn:intersperse</code>]|- valign="top"| {{Code|util:item}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-items-at <code>fn:items-at</code>]|- valign="top"| {{Code|util:last}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-foot <code>fn:foot</code>]|- valign="top"| {{Code|util:map-entries}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-entries <code>map:entries</code>]|- valign="top"| {{Code|util:map-values}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-values <code>map:values</code>]|- valign="top"| {{Code|util:or moved to other modules in future versions of BaseX}}| <code>$expr1 otherwise $expr2|- valign="top"| {{Code|util:replicate}}| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-replicate <code>fn:replicate</code>]|}
=Conventions=
All functions and errors in this module and errors are assigned to the <code><nowiki>http://basex.org/modules/util</nowiki></code> namespace, which is statically bound to the {{Code|util}} prefix.<br/>
=Conditionsand Ranges=
==util:if==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|util:if|$condition as item()*, $then as item()*|item()*}}<br/pre>{{Func|util:if|( $condition as item()*, $then as item()*, $else as item()*| := ()) as item()*}}<br/pre>|-valign="top"
| '''Summary'''
|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.
* Otherwise, {{Code|$else}} will be evaluated. If no the third argument is suppliedomitted, an empty sequence will be returned.|-valign="top"
| '''Examples'''
|
|}
==util:orcount-within==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|util:or|$items as item()*, $default as item()*|item()*}}|-| '''Summary'''|Returns {{Code|$items}} if it is a non-empty sequence. Otherwise, returns {{Code|$default}}. The function is equivalent to one of the following expressions:* <code>if(exists($items)) then $items else $default</code>* <code>$items ?: $default</code> (see [[XQuery Extensions#Elvis Operator|Elvis Operator]] for more details)|-| '''Examples'''|* <codepre>util:or(123, 456)</code> returns {{Code|123}}.* <code>util:or(1[. = 0], -1)</code> returns {{Code|count-1}}.|} ==util:within== {{Mark|Introduced with Version 9.5:}} {| width='100%'|-| width='120' | '''Signatures'''(|{{Func|util:within|$sequence as item()*, $min as xs:integer|xs:boolean}}<br/>{{Func|util:within| $sequence as item()*, $min as xs:integer, $max as xs:integer| := ()) as xs:boolean}}</pre>|-valign="top"
| '''Summary'''
|Checks if the specified {{Code|$sequence}} has at least {{Code|$min}} and, optionally, at most {{Code|$max}} items. Equivalent to:
<syntaxhighlight pre lang="'xquery"'>
let $count := count($sequence)
return $count >= $min and $count <= $max
</syntaxhighlightpre>|-| '''Examples'''|* <code>util:within(('a', 'b', 'c'), 2)</code> returns {{Code|true}}.* <code>util:within((1 to 1000000000)[. < 10], 3, 6)</code> returns {{Code|true}}.|} =Positional Access= ==util:item== {| widthvalign='100%'|-| width='120' | '''Signatures'''|{{Func|util:item|$sequence as item()*, $position as xs:double|item()?}}<br/>|-| '''Summary'''|Returns the item from {{Code|$sequence}} at the specified {{Code|$position}}. Equivalent to <code>$sequence[$position]</code>.|-"top"
| '''Examples'''
|
* <code>util:itemcount-within(reverse(1 to 5'a', 'b', 'c'), 12)</code> returns <code>5</code>{{Code|true}}.* <code>util:itemcount-within(('a'1 to 1000000000)[. < 10],'b')3, 06)</code> returns an empty sequence{{Code|true}}.
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>util:range|( $sequence as item()*, $first as xs:double, $last as xs:double|) as item()*}}<br/pre>|-valign="top"
| '''Summary'''
|Returns items from {{Code|$sequence}}, starting at position {{Code|$first}} and ending at {{Code|$last}}. Equivalent to :<codepre lang='xquery'>subsequence($sequence, $first, $last - $first + 1)</codepre>.|-valign="top"
| '''Examples'''
|
* <code>util:range(//item, 11, 20)</code> returns all path results from (if available) position 11 to 20.
|}
==util:last==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:last|$sequence as item()*|item()?}}<br/>
|-
| '''Summary'''
|Returns last item of a {{Code|$sequence}}. Equivalent to <code>$sequence[last()]</code>.
|-
| '''Examples'''
|
* <code>util:last(reverse(1 to 100))</code> returns <code>1</code>.
|}
==util:init==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|util:init|$sequence as item()*|item()*}}<br/>
|-
| '''Summary'''
|Returns all items of a {{Code|$sequence}} except for the last one. Equivalent to <code>$sequence[position() < last()]</code>.
|-
| '''Examples'''
|
* <code>util:init(1 to 4)</code> returns <code>1 2 3</code>.
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>util:ddo|( $nodes as node()*|) as node()*}}<br/pre>|-valign="top"
| '''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]. All As results of path expression expressions are in brought into distinct document orderbefore they are returned, so the function is equivalent to the expression :<codepre lang='xquery'>$nodes/self::node()</codepre>.
|}
==util:root==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>util:root|( $nodes as node()*|) as document-node()*}}<br/pre>|-valign="top"
| '''Summary'''
|Returns the document nodes of the specified {{Code|$nodes}}. The function is equivalent to the expression <code>$nodes ! /</code>. The path expression <code>/abc</code>is internally represented as <code>util:root(.)/abc</code>.Equivalent to:<pre lang='xquery'>util:ddo($x ! /)</pre>
|}
=Helper Functions= ==util:replicatestrip-namespaces== {{Mark|Updated with Version 9.5:}} Third argument added.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>util:replicate|strip-namespaces( $input node as itemnode()*, $count prefixes as xs:integer|item()string*}}<br/>{{Func|util :replicate|$input as item= ()*, $count as xs:integer, $multiple ) as xs:boolean|itemnode()*}}</pre>|-valign="top"
| '''Summary'''
|Evaluates {{Code|$input}} and returns Removes namespaces with the result {{Code|$count}} times. Unless {{Code|$multiple}} is enabled, the input expression is only evaluated once. The function call specified <code>util:replicate($input, $count, true())prefixes</code> is equivalent to from the supplied <code>(1 to $count) ! $inputnode</code>.|-| '''Errors'''|{{Error|negative|#Errors}} The An empty string can be supplied to remove the default namespace. If no prefixes are specified number is negative, all namespaces will be removed.|-valign="top"
| '''Examples'''
|
* <code>util:replicate('A', 3)</code> returns <code>A A A</code>.* In the following query, a single new Remove all namespaces from an element node is constructed, and {{Code|true}} is returnedits descendants:<syntaxhighlight pre lang="'xquery"'>let $nodes := util:replicatestrip-namespaces(<node/>, 2)return $nodes[1] is $nodes[2]</syntaxhighlight>* In this query, two nodes are constructed, and the result is {{Code|false}}xml xmlns='uri' xmlns:<syntaxhighlight langprefix="xquery">let $nodes 'uri2' prefix:name= util'value'><prefix:replicate(<nodechild/>, 2, true())return $nodes[1] is $nodes[2]</syntaxhighlightxml>|} ==util:intersperse== {{Mark|Introduced with Version 9.5.}})
{| width='100%'|-| width='120' | '''Signatures'''|{{Func|util(: yields :intersperse|$items as item()*, $separator as item()*|item()*}}|-| <xml name='value''Summary'''|Inserts the defined {{Code|$separator}} between the {{Code|$items}} of a sequence and returns the resulting sequence. The function is equivalent to><brchild/><code/xml>head($items), for $item in tail($items) return ($separator, $item)</codepre>|-| '''Examples'''| Inserts semicolon strings between the three input items* Remove all default namespaces:<syntaxhighlight pre lang="'xquery"'>fn:intersperse((<_>1</_>, <_>2</_>, <_>3</_>), xml xmlns='; uri1')></syntaxhighlight>|} ==util:chars== {| width='100%'|-| widthchild xmlns='120' | '''Signatures''uri2'|{{Func|util:chars|$string as xs:string|xs:string*}}<br/>|-| '''Summary'''|Returns all characters of a {{Code|$string}} as a sequence. Equivalent to <code>string-to-codepoints($string) ! codepoints-to-string(.)</codexml>.|-| '''Examples'''|* <code=>util:charsstrip-namespaces('AB')</code> returns the two strings <code>A</code> and <code>B</codepre>.
|}
! width="110"|Code
|Description
|-valign="top"
|{{Code|negative}}
|The specified number is negative.
=Changelog=
;Version 11.0
* Removed: {{Code|util:array-members}}, {{Code|util:array-values}}, {{Code|util:chars}}, {{Code|util:duplicates}}, {{Code|util:init}}, {{Code|util:intersperse}}, {{Code|util:item}}, {{Code|util:last}}, {{Code|util:map-entries}}, {{Code|util:map-values}}, {{Code|util:replicate}}
;Version 9.7
* Added: {{Function||util:strip-namespaces}}
* Updated: {{Function||util:count-within}}: Renamed from {{Code|util:within}}.
;Version 9.5
* Added: [[#{{Code|util:intersperse}}, {{Code|util:within}}, {{Code|util:duplicates}}, {{Code|util:intersperse]]array-members}}, {{Code|util:array-values}}, [[#{{Code|util:withinmap-entries}}, {{Code|util:within]]map-values}}* Updated: [[#util:replicate{{Code|util:replicate]]}}: Third argument added.
;Version 9.4
* Added: [[#util:root{{Function||util:root]]}}
;Version 9.3
* Added: [[#util:ddo{{Function||util:ddo]]}}
;Version 9.2
* Added: [[#util:chars{{Code|util:chars]]}}, [[#util:init{{Code|util:init]]}}* Updated: [[#util:item{{Code|util:item]]}}, [[#util:last{{Code|util:last]]}}, [[#util:range{{Function||util:range]] }} renamed (before: {{Code|util:item-at}}, {{Code|util:item-range}}, {{Code|util:last-from}})
;Version 9.1
* Added: [[#util:if{{Function||util:if]]}}, [[#util:or{{Code|util:or]]}}
;Version 9.0
* Added: [[#util:replicate{{Code|util:replicate]]}}
The Module was introduced with Version 8.5.