Difference between revisions of "Utility Module"

From BaseX Documentation
Jump to navigation Jump to search
Line 15: Line 15:
  
 
In addition, various query optimizations create calls to the utility functions.
 
In addition, various query optimizations create calls to the utility functions.
 +
 +
With {{Announce|Version 11}}, various functions of this module have been removed as they are now part of the official specification:
 +
 +
{|
 +
|- 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-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-pairs <code>map:pairs</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:replicate}}
 +
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-replicate <code>fn:replicate</code>]
 +
|}
  
 
=Conventions=
 
=Conventions=
Line 47: Line 88:
  
 
=Positional Access=
 
=Positional Access=
 
==util:item==
 
 
{| width='100%'
 
|- valign="top"
 
| width='120' | '''Signature'''
 
|<pre>util:item(
 
  $sequence  as item()*,
 
  $position  as xs:double
 
) as item()?</pre>
 
|- valign="top"
 
| '''Summary'''
 
|Returns the item from {{Code|$sequence}} at the specified {{Code|$position}}. Equivalent to:
 
<syntaxhighlight lang="xquery">
 
$sequence[$position]
 
</syntaxhighlight>
 
|- valign="top"
 
| '''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.
 
|}
 
  
 
==util:range==
 
==util:range==
Line 90: Line 109:
 
|
 
|
 
* <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%'
 
|- valign="top"
 
| width='120' | '''Signature'''
 
|<pre>util:last(
 
  $sequence  as item()*
 
) as item()?</pre>
 
|- valign="top"
 
| '''Summary'''
 
|Returns last item of a {{Code|$sequence}}. Equivalent to:
 
<syntaxhighlight lang="xquery">
 
$sequence[last()]
 
</syntaxhighlight>
 
|- valign="top"
 
| '''Examples'''
 
|
 
* <code>util:last(reverse(1 to 100))</code> returns <code>1</code>.
 
|}
 
 
==util:init==
 
 
{| width='100%'
 
|- valign="top"
 
| width='120' | '''Signature'''
 
|<pre>util:init(
 
  $sequence  as item()*
 
) as item()*</pre>
 
|- valign="top"
 
| '''Summary'''
 
|Returns all items of a {{Code|$sequence}} except for the last one. Equivalent to:
 
<syntaxhighlight lang="xquery">
 
$sequence[position() < last()]
 
</syntaxhighlight>
 
|- valign="top"
 
| '''Examples'''
 
|
 
* <code>util:init(1 to 4)</code> returns <code>1 2 3</code>.
 
 
|}
 
|}
  
Line 196: Line 175:
  
 
=Array and Map Functions=
 
=Array and Map Functions=
 
==util:array-members==
 
 
{| width='100%'
 
|- valign="top"
 
| width='120' | '''Signature'''
 
|<pre>util:array-members(
 
  $array  as array(*)
 
) as array(*)*</pre>
 
|- valign="top"
 
| '''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>
 
|- valign="top"
 
| '''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%'
 
|- valign="top"
 
| width='120' | '''Signature'''
 
|<pre>util:array-values(
 
  $array  as array(*)
 
) as item()*</pre>
 
|- valign="top"
 
| '''Summary'''
 
|Returns all members of an {{Code|$array}} as a sequence. Equivalent to:
 
<syntaxhighlight lang="xquery">
 
$array ? *
 
</syntaxhighlight>
 
|- valign="top"
 
| '''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==
 
==util:map-entries==
Line 333: Line 262:
 
let $nodes := util:replicate(<node/>, 2, true())
 
let $nodes := util:replicate(<node/>, 2, true())
 
return $nodes[1] is $nodes[2]
 
return $nodes[1] is $nodes[2]
</syntaxhighlight>
 
|}
 
 
==util:intersperse==
 
 
{| width='100%'
 
|- valign="top"
 
| width='120' | '''Signature'''
 
|<pre>util:intersperse(
 
  $items      as item()*,
 
  $separator  as item()*
 
) as item()*</pre>
 
|- valign="top"
 
| '''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>
 
|- valign="top"
 
| '''Examples'''
 
| Inserts semicolon strings between the three input items:
 
<syntaxhighlight lang="xquery">
 
fn:intersperse((<_>1</_>, <_>2</_>, <_>3</_>), '; ')
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
|}
 
|}
Line 411: Line 317:
  
 
;Version 11.0
 
;Version 11.0
* Removed: {{Function||util:if}}, {{Function||util:or}}
+
* Removed: {{Function||util:if}}, {{Function||util:intersperse}}, {{Function||util:item}}, {{Function||util:or}}
  
 
;Version 9.7
 
;Version 9.7

Revision as of 12:46, 26 October 2023

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.

With Version 11, various functions of this module have been removed as they are now part of the official specification:

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:pairs
util:map-values map:values
util:replicate fn:replicate

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:count-within

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:

<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:range

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:

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

Node Functions

util:ddo

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:

<syntaxhighlight lang="xquery"> $nodes/self::node() </syntaxhighlight>

util:root

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:

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

util:strip-namespaces

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:

<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:map-entries

Signature
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

Signature
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

Signature
util:replicate(
  $input   as item()*,
  $count   as xs:integer,
  $repeat  as xs:boolean?  := false()
) as item()*
Summary Evaluates $input and returns the result $count times. Unless $repeat is set to true, the input expression is evaluated multiple times. 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:duplicates

Signature
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

Signature
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 11.0
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.