Main Page » XQuery » Functions » Update Functions

Update Functions

This module provides additional functions for performing updates and returning results in updating expressions.

Conventions

All functions in this module are assigned to the http://basex.org/modules/update namespace, which is statically bound to the update prefix.

Except for update:cache, all functions are updating and thus comply to the XQuery Update constraints.

Updates

update:apply

Signature
update:apply(
  $function   as %updating fn(*),
  $arguments  as array(*)
) as empty-sequence()
SummaryThe updating variant of fn:apply applies the specified updating $function to the specified $arguments.
Examples
declare %updating function local:update(
  $database  as xs:string,
  $path      as xs:string,
  $function  as %updating fn(item(), xs:string) as empty-sequence()
) as empty-sequence() {
  update:apply($function, [ $database, $path ])
};
local:update('new-db', 'doc.xml', db:create#2),
local:update('existing-db', 'doc.xml', db:add#2)
Creates a new database with an initial document and adds a document to an existing database.

update:for-each

Signature
update:for-each(
  $input   as item()*,
  $action  as %updating fn(item()) as empty-sequence()
) as empty-sequence()
SummaryThe updating variant of fn:for-each applies the specified updating $action to every item of $input.
Examples
let $names := ('db1', 'db2')
return update:for-each($names, db:create#1)
Creates two databases.

update:for-each-pair

Signature
update:for-each-pair(
  $input1  as item()*,
  $input2  as item()*,
  $action  as %updating fn(item()) as empty-sequence()
) as empty-sequence()
SummaryThe updating variant of fn:for-each-pair applies the specified updating $action to the successive pairs of items of $input1 and $input2. Evaluation is stopped if one sequence yields no more items.
Examples
copy $xml := <xml><a/><b/></xml>
modify update:for-each-pair(
  ('a', 'b'),
  ('d', 'e'),
  fn($source, $target) {
    for $e in $xml/*[name() = $source]
    return rename node $e as $target
  }
)
return $xml
Renames nodes in an XML snippets.

update:map-for-each

Signature
update:map-for-each(
  $map     as map(*),
  $action  as %updating fn(xs:anyAtomicType, item()*) as empty-sequence()
) as item()*
SummaryThe updating variant of map:for-each applies the specified $action to every key/value pair of the supplied $map and returns the results as a sequence.
Examples
copy $doc := <xml/>
modify update:map-for-each(
  { 'id': 'id0', 'value': 456 },
  fn($key, $value) {
    insert node attribute { $key } { $value } into $doc
  }
)
return $doc
Inserts attributes into a document.

Output

update:output

Signature
update:output(
  $input  as item()*
) as empty-sequence()
SummaryThis function can be used if MIXUPDATES is not enabled, and if values need to be returned within an updating expression: The supplied $input will be cached and returned at the very end, i.e., after all updates on the pending update list have been processed. If one of the supplied items is affected by an update, a copy will be created and cached instead.
Examples
update:output("Prices have been deleted."),
delete node //price
Deletes all price elements in a database and returns an info message.

update:cache

Signature
update:cache(
  $reset  as xs:boolean?  := false()
) as item()*
SummaryReturns the items that have been cached by update:output. The output cache can optionally be $reset. The function can be used to check which items will eventually be returned as the result of an updating function. This function is nondeterministic: It will return different results before and after items have been cached. The function can be useful for writing unit tests.

Changelog

Version 9.3Version 9.1
  • Updated: update:output: Maps and arrays can be cached if they contain no persistent database nodes or function items.
Version 9.0

⚡Generated with XQuery