Difference between revisions of "Utility Module"
m (Text replacement - "<syntaxhighlight lang="xquery">" to "<pre lang='xquery'>") |
|||
Line 86: | Line 86: | ||
| '''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: | ||
− | < | + | <pre lang='xquery'> |
let $count := count($sequence) | let $count := count($sequence) | ||
return $count >= $min and $count <= $max | return $count >= $min and $count <= $max | ||
Line 110: | Line 110: | ||
| '''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) | subsequence($sequence, $first, $last - $first + 1) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 132: | Line 132: | ||
| '''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: | ||
− | < | + | <pre lang='xquery'> |
$nodes/self::node() | $nodes/self::node() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 148: | Line 148: | ||
| '''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: | ||
− | < | + | <pre lang='xquery'> |
$nodes ! / | $nodes ! / | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 169: | Line 169: | ||
| | | | ||
* Remove all namespaces from an element and its descendants: | * 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>) | util:strip-namespaces(<xml xmlns='uri' xmlns:prefix='uri2' prefix:name='value'><prefix:child/></xml>) | ||
Line 176: | Line 176: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* Remove all default namespaces: | * Remove all default namespaces: | ||
− | < | + | <pre lang='xquery'> |
<xml xmlns='uri1'><child xmlns='uri2'/></xml> | <xml xmlns='uri1'><child xmlns='uri2'/></xml> | ||
=> util:strip-namespaces('') | => util:strip-namespaces('') |
Revision as of 18:34, 1 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:pairs
|
util:map-values
|
map:values
|
util:or
|
$expr1 otherwise $expr2
|
util:replicate
|
fn:replicate
|
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 and Ranges
util:if
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
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
</syntaxhighlight>
|- valign="top"
| '''Examples'''
|
* <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}}.
|}
==util:range==
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>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)
</syntaxhighlight>
|- valign="top"
| '''Examples'''
|
* <code>util:range(//item, 11, 20)</code> returns all path results from (if available) position 11 to 20.
|}
=Node Functions=
==util:ddo==
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>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()
</syntaxhighlight>
|}
==util:root==
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>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 ! /
</syntaxhighlight>
|}
==util:strip-namespaces==
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>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>
</syntaxhighlight>
* Remove all default namespaces:
|
Errors
Code | Description |
---|---|
negative
|
The specified number is negative. |
Changelog
;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 from util: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.