This [[Module Library|XQuery Module]] contains some utility and helper functions.
With {{Announce|Version 11}}, many functions have been removed as they are now part in favor of the official specificationnew features of XQuery 4:
{|
| {{Code|util:duplicates}}
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-duplicate-values <code>fn:duplicate-values</code>]
|- valign="top"
| {{Code|util:if}}
| <code>if($test) { $then } else { $else }</code> (with the new syntax, the else branch is optional)
|- valign="top"
| {{Code|util:init}}
|- valign="top"
| {{Code|util:map-entries}}
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-pairs entries <code>map:pairsentries</code>]
|- valign="top"
| {{Code|util:map-values}}
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/>
=Range Conditionsand Ranges= ==util:if== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>util:if( $condition as item()*, $then as item()*, $else as item()* := ()) as item()*</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 the third argument is omitted, an empty sequence will be returned.|- valign="top"| '''Examples'''|* <code>util:if(true(), 123, 456)</code> returns {{Code|123}}.* <code>util:if(0, 'wrong!')</code> returns an empty sequence.|}
==util:count-within==
| '''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>
|- valign="top"
| '''Examples'''
| '''Summary'''
|Returns items from {{Code|$sequence}}, starting at position {{Code|$first}} and ending at {{Code|$last}}. Equivalent to:
<syntaxhighlight pre lang="'xquery"'>
subsequence($sequence, $first, $last - $first + 1)
</syntaxhighlightpre>
|- valign="top"
| '''Examples'''
|- 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]. As results of path expressions are brought into distinct document order before they are returned, the function is equivalent to:<syntaxhighlight pre lang="'xquery"'>
$nodes/self::node()
</syntaxhighlightpre>
|}
| '''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:
<syntaxhighlight pre lang="'xquery"'>util:ddo($nodes x ! /)</syntaxhighlightpre>
|}
|
* Remove all namespaces from an element and its descendants:
<syntaxhighlight pre lang="'xquery"'>
util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>)
(: yields :)
<xml name='value'><child/></xml>
</syntaxhighlightpre>
* Remove all default namespaces:
<syntaxhighlight pre lang="'xquery"'>
<xml xmlns='uri1'><child xmlns='uri2'/></xml>
=> util:strip-namespaces('')
</syntaxhighlightpre>
|}
;Version 9.5
* Added: {{Function|Code|util:intersperse}}, {{Function|Code|util:within}}, {{Function|Code|util:duplicates}}, {{Function|Code|util:array-members}}, {{Function|Code|util:array-values}}, {{Function|Code|util:map-entries}}, {{Function|Code|util:map-values}}* Updated: {{Function|Code|util:replicate}}: Third argument added.
;Version 9.4
;Version 9.2
* Added: {{Function|Code|util:chars}}, {{Function|Code|util:init}}* Updated: {{Function|Code|util:item}}, {{Function|Code|util:last}}, {{Function||util:range}} renamed (before: {{Code|util:item-at}}, {{Code|util:item-range}}, {{Code|util:last-from}})
;Version 9.1
* Added: {{Function||util:if}}, {{Function|Code|util:or}}
;Version 9.0
* Added: {{Function|Code|util:replicate}}
The Module was introduced with Version 8.5.