Changes

Jump to navigation Jump to search
240 bytes removed ,  18:31, 1 December 2023
m
Text replacement - "</syntaxhighlight>" to "</pre>"
<syntaxhighlight 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'''.
<syntaxhighlight 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.
<syntaxhighlight 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 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.
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.
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.
)
return $c
</syntaxhighlightpre>
;Result:
<author>Joey</author>
</entry>
</syntaxhighlightpre>
Instead of the main-memory {{Code|<entry>}} element, a database node can be supplied:
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.
)
return $doc
</syntaxhighlightpre>
===update===
delete node ./text()
}
</syntaxhighlightpre>
If multiple nodes are supplied as input, the updates will subsequently be performed on each node:
delete node text()
}
</syntaxhighlightpre>
It is easy to chain subsequent update expressions:
insert node "text" into child
}
</syntaxhighlightpre>
===transform with===
replace value of node . with 'new-text'
}
</syntaxhighlightpre>
==Functions==
local:add(., <sub/>)
}
</syntaxhighlightpre>
If update operations are defined in an anonymous function, it may be necessary to call the function with an additional {{Code|updating}} keyword:
updating $add(., <sub/>)
}
</syntaxhighlightpre>
=Concepts=
insert node <b/> into /doc,
/doc/* ! (rename node . as 'renamed')
</syntaxhighlightpre>
…applied on the document…
<syntaxhighlight lang="xml">
<doc> <a/> </doc>
</syntaxhighlightpre>
…results in the following document:
<syntaxhighlight lang="xml">
<doc> <renamed/><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.
<syntaxhighlight 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.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu