Difference between revisions of "Utility Module"

From BaseX Documentation
Jump to navigation Jump to search
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
This [[Module Library|XQuery Module]] contains various utility and helper functions.
+
This [[Module Library|XQuery Module]] contains some utility and helper functions.
  
For all listed functions, equivalent expressions exist in standard XQuery, but code may be better readable with function calls:
+
With {{Announce|Version 11}}, many functions have been removed in favor of new features of XQuery 4:
  
<syntaxhighlight lang="xquery">
+
{|
(: standard XQuery :)
+
|- valign="top"
let $result := if(exists($sequence)) then $sequence else ('default', 'values')
+
| '''BaseX 10'''
return $result[last()]
+
| '''XQuery 4'''
 
+
|- valign="top"
(: XQuery with functions of this module :)
+
| {{Code|util:array-members}}
$sequence
+
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-members <code>array:members</code>]
=> util:or(('default', 'values'))
+
|- valign="top"
=> util:last()
+
| {{Code|util:array-values}}
</syntaxhighlight>
+
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-values <code>array:values</code>]
 
+
|- valign="top"
In addition, various query optimizations create calls to the utility functions.
+
| {{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-40/Overview.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}}
 +
| <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=
 
=Conventions=
Line 20: Line 49:
 
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/>
 
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/>
  
=Conditions=
+
=Conditions and Ranges=
  
 
==util:if==
 
==util:if==
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
|{{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/>
+
|<pre>util:if(
|-
+
  $condition as item()*,
 +
  $then       as item()*,
 +
  $else       as item()* := ()
 +
) as item()*</pre>
 +
|- 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 the third argument is omitted, an empty sequence will be returned.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 40: Line 73:
 
|}
 
|}
  
==util:or==
+
==util:count-within==
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{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}}. Equivalent to the following expressions:
 
<syntaxhighlight lang="xquery">
 
if(exists($items)) then $items else $default,
 
(: Elvis operator :)
 
$items ?: $default
 
</syntaxhighlight>
 
|-
 
| '''Examples'''
 
|
 
* <code>util:or(123, 456)</code> returns {{Code|123}}.
 
* <code>util:or(1[. = 0], -1)</code> returns {{Code|-1}}.
 
|}
 
 
 
==util:within==
 
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
|{{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|xs:boolean}}
+
|<pre>util:count-within(
|-
+
  $sequence as item()*,
 +
  $min       as xs:integer,
 +
  $max       as xs:integer := ()
 +
) as xs:boolean</pre>
 +
|- 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:
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
let $count := count($sequence)
 
let $count := count($sequence)
 
return $count >= $min and $count <= $max
 
return $count >= $min and $count <= $max
</syntaxhighlight>
+
</pre>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* <code>util:within(('a', 'b', 'c'), 2)</code> returns {{Code|true}}.
+
* <code>util:count-within(('a', 'b', 'c'), 2)</code> returns {{Code|true}}.
* <code>util:within((1 to 1000000000)[. < 10], 3, 6)</code> returns {{Code|true}}.
+
* <code>util:count-within((1 to 1000000000)[. < 10], 3, 6)</code> returns {{Code|true}}.
|}
 
 
 
=Positional Access=
 
 
 
==util:item==
 
 
 
{| width='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:
 
<syntaxhighlight lang="xquery">
 
$sequence[$position]
 
</syntaxhighlight>
 
|-
 
| '''Examples'''
 
|
 
* <code>util:item(reverse(1 to 5), 1)</code> returns <code>5</code>.
 
* <code>util:item(('a','b'), 0)</code> returns an empty sequence.
 
 
|}
 
|}
  
Line 105: Line 100:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
|{{Func|util:range|$sequence as item()*, $first as xs:double, $last as xs:double|item()*}}<br/>
+
|<pre>util:range(
|-
+
  $sequence as item()*,
 +
  $first     as xs:double,
 +
  $last     as xs:double
 +
) as item()*</pre>
 +
|- 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:
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
subsequence($sequence, $first, $last - $first + 1)
 
subsequence($sequence, $first, $last - $first + 1)
</syntaxhighlight>
+
</pre>
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
 
* <code>util:range(//item, 11, 20)</code> returns all path results from (if available) position 11 to 20.
 
* <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:
 
<syntaxhighlight lang="xquery">
 
$sequence[last()]
 
</syntaxhighlight>
 
|-
 
| '''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:
 
<syntaxhighlight lang="xquery">
 
$sequence[position() < last()]
 
</syntaxhighlight>
 
|-
 
| '''Examples'''
 
|
 
* <code>util:init(1 to 4)</code> returns <code>1 2 3</code>.
 
 
|}
 
|}
  
Line 161: Line 124:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
|{{Func|util:ddo|$nodes as node()*|node()*}}<br/>
+
|<pre>util:ddo(
|-
+
  $nodes as node()*
 +
) as node()*</pre>
 +
|- 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:
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
$nodes/self::node()
 
$nodes/self::node()
</syntaxhighlight>
+
</pre>
 
|}
 
|}
  
Line 175: Line 140:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
|{{Func|util:root|$nodes as node()*|document-node()*}}<br/>
+
|<pre>util:root(
|-
+
  $nodes as node()*
 +
) as document-node()*</pre>
 +
|- 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:
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
$nodes ! /
 
$nodes ! /
</syntaxhighlight>
+
</pre>
 
|}
 
|}
  
 
==util:strip-namespaces==
 
==util:strip-namespaces==
 
{{Mark|Introduced with Version 10.0:}}
 
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
|{{Func|util:strip-namespaces|$node as node()|node()}}<br/>{{Func|util:strip-namespaces|$node as node(), $prefixes as xs:string*|node()}}<br/>
+
|<pre>util:strip-namespaces(
|-
+
  $node     as node(),
 +
  $prefixes as xs:string* := ()
 +
) as node()</pre>
 +
|- 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'''
 
|
 
|
 
* Remove all namespaces from an element and its descendants:
 
* Remove all namespaces from an element and its descendants:
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>)
 
util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>)
  
 
(: yields :)
 
(: yields :)
 
<xml name='value'><child/></xml>
 
<xml name='value'><child/></xml>
</syntaxhighlight>
+
</pre>
 
* Remove all default namespaces:
 
* Remove all default namespaces:
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
<xml xmlns='uri1'><child xmlns='uri2'/></xml>
 
<xml xmlns='uri1'><child xmlns='uri2'/></xml>
 
=> util:strip-namespaces('')
 
=> util:strip-namespaces('')
</syntaxhighlight>
+
</pre>
|}
 
 
 
=Array and Map Functions=
 
 
 
==util:array-members==
 
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|util:array-members|$array as array(*)|array(*)*}}
 
|-
 
| '''Summary'''
 
|Returns each member of an {{Code|$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==
 
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|util:array-values|$array as array(*)|item()*}}
 
|-
 
| '''Summary'''
 
|Returns all members of an {{Code|$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==
 
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|util:map-entries|$map as map(*)|map(xs:string, item()*)*}}
 
|-
 
| '''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>
 
|-
 
| '''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%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|util:map-values|$map as map(*)|item()*}}
 
|-
 
| '''Summary'''
 
|Returns all values of a {{Code|$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==
 
 
 
{| width='100%'
 
|-
 
| 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()*}}
 
|-
 
| '''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:
 
<syntaxhighlight lang="xquery">
 
util:replicate($input, $count, true()),
 
(1 to $count) ! $input
 
</syntaxhighlight>
 
|-
 
| '''Errors'''
 
|{{Error|negative|#Errors}} The specified number is negative.
 
|-
 
| '''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:intersperse==
 
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|util:intersperse|$items as item()*, $separator as item()*|item()*}}
 
|-
 
| '''Summary'''
 
|Inserts the defined {{Code|$separator}} between the {{Code|$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==
 
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|util:duplicates|$sequence as item()*|xs:anyAtomicType*}}<br/>{{Func|util:duplicates|$sequence as item()*, $collation as xs:string|xs:anyAtomicType*}}
 
|-
 
| '''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.
 
|-
 
| '''Examples'''
 
|
 
* <code>util:duplicates((1, 2, 1, 1))</code> returns <code>1</code>.
 
|}
 
 
 
==util:chars==
 
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|util:chars|$string as xs:string|xs:string*}}<br/>
 
|-
 
| '''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>
 
|-
 
| '''Examples'''
 
|
 
* <code>util:chars('AB')</code> returns the two strings <code>A</code> and <code>B</code>.
 
 
|}
 
|}
  
Line 402: Line 187:
 
! width="110"|Code
 
! width="110"|Code
 
|Description
 
|Description
|-
+
|- valign="top"
 
|{{Code|negative}}
 
|{{Code|negative}}
 
|The specified number is negative.
 
|The specified number is negative.
Line 409: Line 194:
 
=Changelog=
 
=Changelog=
  
;Version 10.0
+
;Version 11.0
* Added: [[#util:strip-namespaces|util:strip-namespaces]]
+
* 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
 
;Version 9.5
* Added: [[#util:intersperse|util:intersperse]], [[#util:within|util:within]], [[#util:duplicates|util:duplicates]], [[#util:array-members|util:array-members]], [[#util:array-values|util:array-values]], [[#util:map-entries|util:map-entries]], [[#util:map-values|util:map-values]]
+
* 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: [[#util:replicate|util:replicate]]: Third argument added.
+
* Updated: {{Code|util:replicate}}: Third argument added.
  
 
;Version 9.4
 
;Version 9.4
* Added: [[#util:root|util:root]]
+
* Added: {{Function||util:root}}
  
 
;Version 9.3
 
;Version 9.3
* Added: [[#util:ddo|util:ddo]]
+
* Added: {{Function||util:ddo}}
  
 
;Version 9.2
 
;Version 9.2
* Added: [[#util:chars|util:chars]], [[#util:init|util:init]]
+
* Added: {{Code|util:chars}}, {{Code|util:init}}
* Updated: [[#util:item|util:item]], [[#util:last|util:last]], [[#util:range|util:range]] renamed (before: {{Code|util:item-at}}, {{Code|util:item-range}}, {{Code|util:last-from}})
+
* 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
 
;Version 9.1
* Added: [[#util:if|util:if]], [[#util:or|util:or]]
+
* Added: {{Function||util:if}}, {{Code|util:or}}
  
 
;Version 9.0
 
;Version 9.0
* Added: [[#util:replicate|util:replicate]]
+
* Added: {{Code|util:replicate}}
  
 
The Module was introduced with Version 8.5.
 
The Module was introduced with Version 8.5.

Latest revision as of 14:36, 5 December 2023

This XQuery Module contains some utility and helper functions.

With Version 11, many functions have been removed in favor of new features of XQuery 4:

BaseX 10 XQuery 4
util:array-members array:members
util:array-values array:values
util:chars fn:characters
util:duplicates fn:duplicate-values
util:init fn:trunk
util:intersperse fn:intersperse
util:item fn:items-at
util:last fn:foot
util:map-entries map:entries
util:map-values map:values
util:or $expr1 otherwise $expr2
util:replicate fn:replicate

Conventions[edit]

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 and Ranges[edit]

util:if[edit]

Signature
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 the third argument is omitted, an empty sequence will be returned.
Examples
  • util:if(true(), 123, 456) returns 123.
  • util:if(0, 'wrong!') returns an empty sequence.

util:count-within[edit]

Signature
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:
let $count := count($sequence)
return $count >= $min and $count <= $max
Examples
  • util:count-within(('a', 'b', 'c'), 2) returns true.
  • util:count-within((1 to 1000000000)[. < 10], 3, 6) returns true.

util:range[edit]

Signature
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:
subsequence($sequence, $first, $last - $first + 1)
Examples
  • util:range(//item, 11, 20) returns all path results from (if available) position 11 to 20.

Node Functions[edit]

util:ddo[edit]

Signature
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:
$nodes/self::node()

util:root[edit]

Signature
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. Equivalent to:
$nodes ! /

util:strip-namespaces[edit]

Signature
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:
util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>)

(: yields :)
<xml name='value'><child/></xml>
  • Remove all default namespaces:
<xml xmlns='uri1'><child xmlns='uri2'/></xml>
=> util:strip-namespaces('')

Errors[edit]

Code Description
negative The specified number is negative.

Changelog[edit]

Version 11.0
  • Removed: util:array-members, util:array-values, util:chars, util:duplicates, util:init, util:intersperse, util:item, util:last, util:map-entries, util:map-values, util:replicate
Version 9.7
Version 9.5
  • Added: util:intersperse, util:within, util:duplicates, util:array-members, util:array-values, util:map-entries, util:map-values
  • Updated: util:replicate: Third argument added.
Version 9.4
Version 9.3
Version 9.2
  • Added: util:chars, util:init
  • Updated: util:item, util:last, util:range renamed (before: util:item-at, util:item-range, util:last-from)
Version 9.1
Version 9.0
  • Added: util:replicate

The Module was introduced with Version 8.5.