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.

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.

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.

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.

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.