Changes

Jump to navigation Jump to search
204 bytes removed ,  18:33, 1 December 2023
m
Text replacement - "<syntaxhighlight lang="xquery">" to "<pre lang='xquery'>"
===insert===
<syntaxhighlight pre lang="'xquery"'>
insert node (attribute { 'a' } { 5 }, 'text', <e/>) into /n
</pre>
===delete===
<syntaxhighlight pre lang="'xquery"'>
delete node //n
</pre>
===replace===
<syntaxhighlight pre lang="'xquery"'>
replace node /n with <a/>
</pre>
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'
</pre>
===rename===
<syntaxhighlight pre lang="'xquery"'>
for $n in //originalNode
return rename node $n as 'renamedNode'
===copy/modify/return===
<syntaxhighlight pre lang="'xquery"'>
copy $c := doc('example.xml')//originalNode
modify rename node $c as 'copyOfNode'
;Query:
<syntaxhighlight pre lang="'xquery"'>
copy $c :=
<entry>
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]
...
Entire documents can be copied and modified:
<syntaxhighlight pre lang="'xquery"'>
copy $doc := doc("zaokeng.kml")
modify (
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 {
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()
It is easy to chain subsequent update expressions:
<syntaxhighlight pre lang="'xquery"'>
<root/> update {
insert node <child/> into .
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'
Functions that performs updates need to be marked with an {{Code|%updating}} annotation:
<syntaxhighlight pre lang="'xquery"'>
declare %updating function local:add($target, $node) {
insert node $node into $target
If update operations are defined in an anonymous function, it may be necessary to call the function with an additional {{Code|updating}} keyword:
<syntaxhighlight pre lang="'xquery"'>
let $add := %updating function($target, $node) {
insert node $node into $target
The query…
<syntaxhighlight pre lang="'xquery"'>
insert node <b/> into /doc,
/doc/* ! (rename node . as 'renamed')
* 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
</pre>
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu