Difference between revisions of "Utility Module"
(12 intermediate revisions by 2 users not shown) | |||
Line 8: | Line 8: | ||
==util:if== | ==util:if== | ||
− | |||
− | |||
{| width='100%' | {| width='100%' | ||
Line 28: | Line 26: | ||
==util:or== | ==util:or== | ||
− | |||
− | |||
{| width='100%' | {| width='100%' | ||
Line 37: | Line 33: | ||
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns {{Code|$items}} if it is a non-empty sequence. Otherwise, returns {{Code|$default}}. The function is equivalent to the | + | |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''' | | '''Examples''' | ||
Line 48: | Line 46: | ||
==util:item== | ==util:item== | ||
− | |||
− | |||
{| width='100%' | {| width='100%' | ||
Line 66: | Line 62: | ||
==util:range== | ==util:range== | ||
− | |||
− | |||
{| width='100%' | {| width='100%' | ||
Line 83: | Line 77: | ||
==util:last== | ==util:last== | ||
− | |||
− | |||
{| width='100%' | {| width='100%' | ||
|- | |- | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | |{{Func|util:last | + | |{{Func|util:last|$sequence as item()*|item()?}}<br/> |
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
Line 100: | Line 92: | ||
==util:init== | ==util:init== | ||
− | |||
− | |||
{| width='100%' | {| width='100%' | ||
Line 114: | Line 104: | ||
| | | | ||
* <code>util:init(1 to 4)</code> returns <code>1 2 3</code>. | * <code>util:init(1 to 4)</code> returns <code>1 2 3</code>. | ||
+ | |} | ||
+ | |||
+ | =Node Functions= | ||
+ | |||
+ | ==util:ddo== | ||
+ | |||
+ | {| width='100%' | ||
+ | |- | ||
+ | | width='120' | '''Signatures''' | ||
+ | |{{Func|util:ddo|$nodes as node()*|node()*}}<br/> | ||
+ | |- | ||
+ | | '''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 results of path expression are in distinct document order, so the function is equivalent to the expression <code>$nodes/self::node()</code>. | ||
+ | |} | ||
+ | |||
+ | {| width='100%' | ||
+ | |- | ||
+ | | width='120' | '''Signatures''' | ||
+ | |{{Func|util:root|$nodes as node()*|document-node()*}}<br/> | ||
+ | |- | ||
+ | | '''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. | ||
+ | |||
|} | |} | ||
Line 119: | Line 133: | ||
==util:replicate== | ==util:replicate== | ||
+ | |||
+ | {{Mark|Updated with Version 9.5:}} Third argument added. | ||
{| width='100%' | {| width='100%' | ||
|- | |- | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | |{{Func|util:replicate|$ | + | |{{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''' | | '''Summary''' | ||
− | | | + | |Evaluates {{Code|$input}} and returns the result {{Code|$count}} times. Unless {{Code|$multiple}} is enabled, the input expression is only evaluated once. The function call <code>util:replicate($input, $count, true())</code> is equivalent to <code>(1 to $count) ! $input</code>. |
|- | |- | ||
| '''Errors''' | | '''Errors''' | ||
Line 134: | Line 150: | ||
| | | | ||
* <code>util:replicate('A', 3)</code> returns <code>A A A</code>. | * <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: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 <code>string-to-codepoints($string) ! codepoints-to-string(.)</code>. | ||
+ | |- | ||
+ | | '''Examples''' | ||
+ | | | ||
+ | * <code>util:chars('AB')</code> returns the two strings <code>A</code> and <code>B</code>. | ||
|} | |} | ||
Line 147: | Line 188: | ||
=Changelog= | =Changelog= | ||
+ | |||
+ | ;Version 9.5 | ||
+ | * Updated: [[#util:replicate|util:replicate]]: Third argument added. | ||
+ | |||
+ | ;Version 9.4 | ||
+ | * Added: [[#util:root|util:root]] | ||
+ | |||
+ | ;Version 9.3 | ||
+ | * Added: [[#util:ddo|util:ddo]] | ||
;Version 9.2 | ;Version 9.2 | ||
− | * Added: [[#util:init|util:init]] | + | * Added: [[#util:chars|util:chars]], [[#util:init|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}}) |
;Version 9.1 | ;Version 9.1 |
Revision as of 18:50, 18 November 2020
This XQuery Module contains various small utility and helper functions. Please note that some of the functions are used for internal query rewritings. They may be renamed or moved to other modules in future versions of BaseX.
Contents
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:if
Signatures | util:if($condition as item()*, $then as item()*) as item()* util:if($condition as item()*, $then as item()*, $else as item()*) as item()* |
Summary | Alternative writing for the if/then/else expression:
|
Examples |
|
util:or
Signatures | util:or($items as item()*, $default as item()*) as item()*
|
Summary | Returns $items if it is a non-empty sequence. Otherwise, returns $default . The function is equivalent to one of the following expressions:
|
Examples |
|
Positional Access
util:item
Signatures | util:item($sequence as item()*, $position as xs:double) as item()? |
Summary | Returns the item from $sequence at the specified $position . Equivalent to $sequence[$position] .
|
Examples |
|
util:range
Signatures | 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:last
Signatures | util:last($sequence as item()*) as item()? |
Summary | Returns last item of a $sequence . Equivalent to $sequence[last()] .
|
Examples |
|
util:init
Signatures | util:init($sequence as item()*) as item()* |
Summary | Returns all items of a $sequence except for the last one. Equivalent to $sequence[position() < last()] .
|
Examples |
|
Node Functions
util:ddo
Signatures | 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. All results of path expression are in distinct document order, so the function is equivalent to the expression $nodes/self::node() .
|
Signatures | util:root($nodes as node()*) as document-node()* |
Summary | Returns the document nodes of the specified $nodes . The function is equivalent to the expression $nodes ! / . The path expression /abc
is internally represented as
|
Helper Functions
util:replicate
Template:Mark Third argument added.
Signatures | util:replicate($input as item()*, $count as xs:integer) as item()* util:replicate($input as item()*, $count as xs:integer, $multiple as xs:boolean) as item()*
|
Summary | Evaluates $input and returns the result $count times. Unless $multiple is enabled, the input expression is only evaluated once. The function call util:replicate($input, $count, true()) is equivalent to (1 to $count) ! $input .
|
Errors | negative : The specified number is negative.
|
Examples |
<syntaxhighlight lang="xquery"> let $nodes := util:replicate(<node/>, 2) return $nodes[1] is $nodes[2] </syntaxhighlight>
<syntaxhighlight lang="xquery"> let $nodes := util:replicate(<node/>, 2, true()) return $nodes[1] is $nodes[2] </syntaxhighlight> |
util:chars
Signatures | util:chars($string as xs:string) as xs:string* |
Summary | Returns all characters of a $string as a sequence. Equivalent to string-to-codepoints($string) ! codepoints-to-string(.) .
|
Examples |
|
Errors
Code | Description |
---|---|
negative
|
The specified number is negative. |
Changelog
- Version 9.5
- Updated: util:replicate: Third argument added.
- Version 9.4
- Added: util:root
- Version 9.3
- Added: util:ddo
- 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.