Declare an updating function.
The following construct(s) refer to this construct:
An UpdatingFunctionDecl
declares an updating function. The
declaration differs from the declaration of a non-updating function by the
additional "updating" keyword, the absence of a
result type specification, and an UpdateSequence
instead of a
result expression.
The UpdatingFunctionDecl
occurs in the prolog of an update
statement or a library module declaration. Although it is also syntactically
valid in the prolog of a query, it is useless in queries because it can only be
invoked from updating expressions.
This function inserts an attribute with the specified name into an element if it does not exist, or modifies the attribute value if it does exist.
declare updating function local:upsert($e as element(), $an as xs:QName, $av as xs:string) { let $ea := $e/attribute()[node-name(.) = $an] do if ($ea) then replace $ea with attribute {$an} {$av} else insert attribute {$an} {$av} into $e }