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/>
=FunctionsConditions and Ranges=
==util:item-atif==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>util:if( $condition as item-at|()*, $sequence then as item()*, $position else as xsitem()* :double|= ()) as item()?}}*<br/pre>|-valign="top"
| '''Summary'''
|Returns Alternative writing for the item from if/then/else expression:* If the ''effective boolean value'' of {{Code|$sequencecondition}} at is true, the specified {{Code|$positionthen}}branch will be evaluated. Equivalent to <code>* Otherwise, {{Code|$else}} will be evaluated. If the third argument is omitted, an empty sequence[$position]</code>will be returned.|-valign="top"
| '''Examples'''
|
* <code>util:item-atif(reversetrue(1 to 5), 1123, 456)</code> returns <code>5</code>{{Code|123}}.* <code>util:item-atif(('a'0,'bwrong!'), 0)</code> returns an empty sequence.
|}
==util:itemcount-rangewithin==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>util:itemcount-range|within( $sequence as item()*, $first min as xs:doubleinteger, $last max as xs:double|iteminteger := ()*}}) as xs:boolean<br/pre>|-valign="top"
| '''Summary'''
|Returns items from Checks if the specified {{Code|$sequence}}, starting has at position least {{Code|$firstmin}} and ending , optionally, at most {{Code|$lastmax}}items. Equivalent to :<codepre lang='xquery'>subsequencelet $count := count($sequence, )return $count >= $first, min and $last - count <= $first + 1)max</codepre>.|-valign="top"
| '''Examples'''
|
* <code>util:itemcount-rangewithin(//item('a', 'b', 11'c'), 202)</code> returns all path results from {{Code|true}}.* <code>util:count-within(if available(1 to 1000000000) position 11 to 20[. < 10], 3, 6)</code> returns {{Code|true}}.
|}
==util:last-fromrange==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>util:last-from|range( $sequence as item()*|, $first as xs:double, $last as xs:double) as item()?}}*<br/pre>|-valign="top"
| '''Summary'''
|Returns last item of a 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:lastrange(//item, 11, 20)</code> returns all path results from (if available) position 11 to 20.|} =Node Functions= ==util:ddo== {| width='100%'|-fromvalign="top"| width='120' | '''Signature'''|<pre>util:ddo( $nodes as node(reverse)*) as node(1 )*</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]. As results of path expressions are brought into distinct document order before they are returned, the function is equivalent to :<pre lang='xquery'>$nodes/self::node()</pre>|} ==util:root== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>util:root( $nodes as node()*)as document-node()*</pre>|- valign="top"| '''Summary'''|Returns the document nodes of the specified {{Code|$nodes}}. The path expression <code>/abc</code> returns is internally represented as <code>1util:root(.)/abc</code>.Equivalent to:<pre lang='xquery'>util:ddo($x ! /)</pre>
|}
==util:replicatestrip-namespaces==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>util:replicate|strip-namespaces( $sequence node as itemnode()*, $count prefixes as xs:integer|itemstring* := ()) as node()*}}<br/pre>|-valign="top"
| '''Summary'''
|Returns {{Code|$count}} instances of Removes namespaces with the specified {{Code|<code>$sequence}}. A similar result can be generated with prefixes</code> from the supplied <code>(1 to $count) ! $sequencenode</code>, but in . An empty string can be supplied to remove the latter casedefault namespace. If no prefixes are specified, the right-hand expression all namespaces will be evaluated multiple times.|-| '''Errors'''|{{Error|negative|#Errors}} The specified number is negativeremoved.|-valign="top"
| '''Examples'''
|
* Remove all namespaces from an element and its descendants:<codepre lang='xquery'>util:replicatestrip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='Avalue', 3><prefix:child/></xml>) (: yields :)<xml name='value'><child/></codexml></pre>* Remove all default namespaces:<pre lang='xquery'> returns <codexml xmlns='uri1'>A A A<child xmlns='uri2'/></xml>=> util:strip-namespaces('')</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:array-members}}, {{Code|util:array-values}}, {{Code|util:map-entries}}, {{Code|util:map-values}}
* Updated: {{Code|util:replicate}}: Third argument added.
;Version 9.4
* Added: {{Function||util:root}}
;Version 9.3
* Added: {{Function||util:ddo}}
;Version 9.2
* Added: {{Code|util:chars}}, {{Code|util:init}}
* Updated: {{Code|util:item}}, {{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}}, {{Code|util:or}}
;Version 9.0
* Added: [[#util:replicate{{Code|util:replicate]]}}
The Module was introduced with Version 8.5.