===insert===
<syntaxhighlight pre lang="'xquery"'>
insert node (attribute { 'a' } { 5 }, 'text', <e/>) into /n
</syntaxhighlightpre>
Insert enables you to insert a sequence of nodes into a single target node. Several modifiers are available to specify the exact insert location: insert into '''as first'''/'''as last''', insert '''before'''/'''after''' and insert '''into'''.
===delete===
<syntaxhighlight pre lang="'xquery"'>
delete node //n
</syntaxhighlightpre>
The example query deletes all <code><n></code> elements in your database. In contrast to other updating expressions, multiple nodes can be supplied as a target.
===replace===
<syntaxhighlight pre lang="'xquery"'>
replace node /n with <a/>
</syntaxhighlightpre>
The target element is replaced by the DOM node <code><a/></code>. You can also replace the value of a node and its descendants by using the modifier '''value of''':
<syntaxhighlight pre lang="'xquery"'>
replace value of node /n with 'newValue'
</syntaxhighlightpre>
All descendants of /n are deleted, and the supplied text is inserted as the only child. The result of the insert sequence is either a single text node or an empty sequence. If the insert sequence is empty, all descendants of the target are deleted. Consequently, replacing the value of a node leaves the target with either a single text node or no descendants at all.
===rename===
<syntaxhighlight pre lang="'xquery"'>
for $n in //originalNode
return rename node $n as 'renamedNode'
</syntaxhighlightpre>
All <code>originalNode</code> elements are renamed. A loop can be used to modify multiple nodes within a single statement. Nodes on the {{Code|descendant}} or {{Code|attribute}} axis of the target are not affected.
===copy/modify/return===
<syntaxhighlight pre lang="'xquery"'>
copy $c := doc('example.xml')//originalNode
modify rename node $c as 'copyOfNode'
return $c
</syntaxhighlightpre>
A copy of the {{Code|originalNode}} element is created, renamed and returned; the original document will not be updated.
;Query:
<syntaxhighlight pre lang="'xquery"'>
copy $c :=
<entry>
)
return $c
</syntaxhighlightpre>
;Result:
<syntaxhighlight pre lang="xml">
<entry>
<title>Copy of: Transform expression example</title>
<author>Joey</author>
</entry>
</syntaxhighlightpre>
Instead of the main-memory {{Code|<entry>}} element, a database node can be supplied:
<syntaxhighlight pre lang="'xquery"'>
copy $c := (db:get('example')//entry)[1]
...
</syntaxhighlightpre>
In this case, the database node remains untouched, as all updates are performed on the node copy.
Entire documents can be copied and modified:
<syntaxhighlight pre lang="'xquery"'>
copy $doc := doc("zaokeng.kml")
modify (
)
return $doc
</syntaxhighlightpre>
===update===
{{Announce|Updated with Version 10:}} Curly braces are now mandatory.
The {{Code|update}} expression is a BaseX-specific convenience operator for the bulky {{Code|copy/modify/return}} construct. Similar to the [[XQuery 3.0#Simple Map Operator|XQuery 3.0 map operator]], the nodes resulting from the first expression are bound as context items, and the bracketed expressions performs updates on the item. The updated nodes is returned as result:
<syntaxhighlight pre lang="'xquery"'>
for $item in db:get('data')//item
return $item update {
delete node ./text()
}
</syntaxhighlightpre>
If multiple nodes are supplied as input, the updates will subsequently be performed on each node:
<syntaxhighlight pre lang="'xquery"'>
db:get('data')//item update {
delete node text()
}
</syntaxhighlightpre>
It is easy to chain subsequent update expressions:
<syntaxhighlight pre lang="'xquery"'>
<root/> update {
insert node <child/> into .
insert node "text" into child
}
</syntaxhighlightpre>
===transform with===
The {{Code|transform with}} expression was added to the current [https://www.w3.org/TR/xquery-update-30/#id-transform-with XQuery Update 3.0] working draft. It is a simplified version of the [[#update|update]] expression (it is limited to single input nodes and cannot be chained):
<syntaxhighlight pre lang="'xquery"'>
<xml>text</xml> transform with {
replace value of node . with 'new-text'
}
</syntaxhighlightpre>
==Functions==
===User-Defined Functions===
If an updating function item is called, the function call must Functions that performs updates need to be prefixed marked with the an {{Code|%updating}} keyword. This ensures that the query compiler can statically detect whether an invoked annotation: <pre lang='xquery'>declare %updating function item will perform updates or notlocal:add($target, $node) { insert node $node into $target};
<syntaxhighlight lang="xquery">let $node := <node>TO-BE-DELETED</node>update {let $delete-text local:= %updating functionadd($node) { delete node $node., <sub//text(>)
}
return </pre> If update operations are defined in an anonymous function, it may be necessary to call the function with an additional {{Code|updating}} keyword: <pre lang='xquery'>let $add := %updating function($target, $node update ) { updating insert node $node into $delete-text(.)target
}
return <node/syntaxhighlight>update { updating $add(., <sub/>)As shown in the example, user-defined and anonymous functions can additionally be annotated as {{Code|%updating}}.</pre>
=Concepts=
==Pending Update List==
{{Announce|Updated with Version 10}}: {{Function|Database|db:put-binary}} is executed before standard XQuery Update expressions.
Updating statements are not executed immediately, but are first collected as update primitives within a set-like structure, the so-called Pending Update List (PUL). After the evaluation of the query, and after some consistency checks and optimizations, the update primitives will be applied in the following order:
The query…
<syntaxhighlight pre lang="'xquery"'>
insert node <b/> into /doc,
for $n in /doc/child::node* ! ()return rename node $n . as 'justRenamedrenamed')</syntaxhighlightpre>
…applied on the document…
<syntaxhighlight pre lang="xml">
<doc> <a/> </doc>
</syntaxhighlightpre>
…results in the following document:
<syntaxhighlight pre lang="xml"><doc> <justRenamedrenamed/><b/> </doc></syntaxhighlightpre>
Despite explicitly renaming all child nodes of {{Code|<doc/>}}, the former {{Code|<a/>}} element is the only one to be renamed. The {{Code|<<b/>}} element is inserted within the same snapshot and is therefore not yet visible to the user.
==Returning Results==
* The BaseX-specific {{Function|Update|update:output}} function bridges this gap: it caches the results of its arguments at runtime and returns them after all updates have been processed. The following example performs an update and returns a success message:
<syntaxhighlight pre lang="'xquery"'>
update:output("Update successful."), insert node <c/> into doc('factbook')/mondial
</syntaxhighlightpre>
* With {{Option|MIXUPDATES}}, all updating constraints will be turned off. Returned nodes will be copied before they are modified by updating expressions. An error is raised if items are returned within a transform expression.