XQuery Update Facility: Insert Delete Replace Rename
The XQuery Update Facility extends XQuery to enable persistent
modifications to XML documents without requiring full-document rewrites.
Instead of altering nodes instantly, update expressions generate
updating primitives that are collected into a Pending Update List (PUL)
and applied together in a specific, safe execution order at the end of
the query snapshot. The core of this mechanism relies on four primary
expressions: insert, delete,
replace, and rename.
The insert Expression
The insert expression adds new nodes into an existing
XML tree. It supports directional and positional modifiers to define
exactly where the source nodes should be placed relative to the target
node.
- Syntax forms:
insert node $source into $target: Inserts the node as a child of the target element or document node, without a guaranteed position relative to existing children unless specified.insert node $source as first into $target: Inserts the node as the first child of the target element.insert node $source as last into $target: Inserts the node as the final child of the target element.insert node $source before $target: Inserts the node as a sibling immediately preceding the target node.insert node $source after $target: Inserts the node as a sibling immediately following the target node.
If attributes are inserted into an element, the positional keywords
(before, after, first,
last) are ignored because attributes are unordered.
The delete Expression
The delete expression removes one or more nodes from an
XML document.
- Syntax:
delete node $targetdelete nodes $targets
The target expression evaluates to a sequence of zero or more nodes. When executed, the targeted nodes (which can be elements, attributes, text nodes, comments, or processing instructions) are detached from their respective parent nodes. If a deleted element contains child nodes, the entire subtree is removed automatically.
The replace Expression
The replace expression updates XML content through two
distinct operational modes: replacing an entire node or replacing only a
node’s value.
- Replacing a Node (
replace node):replace node $target with $replacement- This form removes the target node and inserts one or more replacement nodes in its exact position. If the target is an attribute, the replacement must also be an attribute; if the target is an element, comment, text node, or processing instruction, the replacement must not be an attribute.
- Replacing Node Value
(
replace value of node):replace value of node $target with $new-value- This form preserves the identity and attributes of the target node
while altering its internal content. For element nodes, all existing
children are removed and replaced with a single text node containing the
string value of
$new-value. For attribute, text, comment, or processing instruction nodes, the string value of the node is updated directly.
The rename Expression
The rename expression modifies the QName (qualified
name) of an element, attribute, or processing instruction without
altering its node identity, attributes, or child content.
- Syntax:
rename node $target as $new-name
The target expression must resolve to a single element, attribute, or
processing instruction node. The $new-name expression
evaluates to an xs:QName (or an xs:string
representing the name). The operation preserves the node’s position,
descendants, and attributes while updating only its name and associated
namespace bindings.