Difference between revisions of "Utility Module"
Jump to navigation
Jump to search
(60 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | This [[Module Library|XQuery Module]] contains | + | This [[Module Library|XQuery Module]] contains some utility and helper functions. |
+ | |||
+ | With {{Announce|Version 11}}, many functions have been removed in favor of new features of 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-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 5: | 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' | ''' | + | | width='120' | '''Signature''' |
− | | | + | |<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 | + | * Otherwise, {{Code|$else}} will be evaluated. If the third argument is omitted, an empty sequence will be returned. |
− | |- | + | |- valign="top" |
| '''Examples''' | | '''Examples''' | ||
| | | | ||
Line 25: | Line 73: | ||
|} | |} | ||
− | ==util: | + | ==util:count-within== |
{| width='100%' | {| width='100%' | ||
− | |- | + | |- valign="top" |
− | | width='120' | ''' | + | | width='120' | '''Signature''' |
− | | | + | |<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: |
− | + | <pre lang='xquery'> | |
− | + | let $count := count($sequence) | |
− | |- | + | return $count >= $min and $count <= $max |
+ | </pre> | ||
+ | |- valign="top" | ||
| '''Examples''' | | '''Examples''' | ||
| | | | ||
− | * <code>util: | + | * <code>util:count-within(('a', 'b', 'c'), 2)</code> returns {{Code|true}}. |
− | + | * <code>util:count-within((1 to 1000000000)[. < 10], 3, 6)</code> returns {{Code|true}}. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | * <code>util: | ||
− | |||
|} | |} | ||
Line 64: | Line 100: | ||
{| width='100%' | {| width='100%' | ||
− | |- | + | |- valign="top" |
− | | width='120' | ''' | + | | width='120' | '''Signature''' |
− | | | + | |<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: |
− | |- | + | <pre lang='xquery'> |
+ | subsequence($sequence, $first, $last - $first + 1) | ||
+ | </pre> | ||
+ | |- valign="top" | ||
| '''Examples''' | | '''Examples''' | ||
| | | | ||
Line 76: | Line 119: | ||
|} | |} | ||
− | = | + | =Node Functions= |
− | + | ==util:ddo== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{| width='100%' | {| width='100%' | ||
− | |- | + | |- valign="top" |
− | | width='120' | ''' | + | | width='120' | '''Signature''' |
− | | | + | |<pre>util:ddo( |
− | |- | + | $nodes as node()* |
+ | ) as node()*</pre> | ||
+ | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns | + | |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: |
− | + | <pre lang='xquery'> | |
− | + | $nodes/self::node() | |
− | + | </pre> | |
− | |||
|} | |} | ||
− | + | ==util:root== | |
− | |||
− | ==util: | ||
{| width='100%' | {| width='100%' | ||
− | |- | + | |- valign="top" |
− | | width='120' | ''' | + | | width='120' | '''Signature''' |
− | | | + | |<pre>util:root( |
− | |- | + | $nodes as node()* |
+ | ) as document-node()*</pre> | ||
+ | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
− | |Returns | + | |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: |
− | + | <pre lang='xquery'> | |
− | + | $nodes ! / | |
− | + | </pre> | |
− | |||
− | |||
− | |||
− | |||
|} | |} | ||
− | ==util: | + | ==util:strip-namespaces== |
{| width='100%' | {| width='100%' | ||
− | |- | + | |- valign="top" |
− | | width='120' | ''' | + | | width='120' | '''Signature''' |
− | | | + | |<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. |
− | |- | + | |- valign="top" |
| '''Examples''' | | '''Examples''' | ||
| | | | ||
− | * < | + | * Remove all namespaces from an element and its descendants: |
− | + | <pre lang='xquery'> | |
− | + | util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>) | |
− | |||
− | |||
− | |||
− | + | (: yields :) | |
− | + | <xml name='value'><child/></xml> | |
− | + | </pre> | |
− | + | * Remove all default namespaces: | |
− | + | <pre lang='xquery'> | |
− | + | <xml xmlns='uri1'><child xmlns='uri2'/></xml> | |
− | + | => util:strip-namespaces('') | |
+ | </pre> | ||
|} | |} | ||
Line 159: | 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 165: | Line 193: | ||
=Changelog= | =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 | ;Version 9.3 | ||
− | * Added: | + | * Added: {{Function||util:ddo}} |
;Version 9.2 | ;Version 9.2 | ||
− | * Added: | + | * 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 | ;Version 9.1 | ||
− | * Added: | + | * Added: {{Function||util:if}}, {{Code|util:or}} |
;Version 9.0 | ;Version 9.0 | ||
− | * Added: | + | * 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
|
Contents
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:
|
Examples |
|
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: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 |
|
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 |
util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>)
(: yields :)
<xml name='value'><child/></xml>
<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
- Added:
util:strip-namespaces
- Updated:
util:count-within
: Renamed fromutil:within
.
- 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
- 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
- Added:
util:if
,util:or
- Version 9.0
- Added:
util:replicate
The Module was introduced with Version 8.5.