2 Extensions to the Processing Model

Expressions in XQuery Update Facility 3.0 can return a pending update list in addition to an XDM instance, which can result in modifications being made to the state of existing nodes. This specification defines the semantics of updates to an XDM instance. Propagation of these updates to an underlying persistent store (if any) is beyond the scope of this specification.

2.1 Pending Update Lists

[Definition: A pending update list is an unordered collection of update primitives, which represent node state changes that have not yet been applied. ] The term "result" used in Section 2.3.4 Errors and Optimization XQ30 includes both the XDM instance and the pending update list returned by an expression.

XQuery Update Facility 3.0 relaxes many of the constraints placed on the position of updating expressions in [XQuery Update Facility 1.0] with the goal of allowing expressions to return both a non-empty pending update list and a non-empty XDM instance. Existing expressions are extended with new rules to determine the category and resulting pending update list when they are executed.

2.2 Updating Expressions

XQuery Update Facility 3.0 introduces a new category of expression, called an updating expression. Unlike expressions defined in [XQuery 3.0: An XML Query Language], updating expressions can potentially result in the modification of the state of an existing node. [Definition: An updating expression is an expression that can return a non-empty pending update list. This includes basic updating expressions, static function calls to an updating function, or any expression that directly contains an updating expression (other than the modify expression of a copy modify or transform with expression). ] [Definition: A simple expression is any XQuery expression that is not an updating expression. ]

The classification of each expression into one of the above categories is performed by static analysis, according to rules specified in this document for each type of expression.

[Definition: Five new kinds of expressions named basic updating expression are introduced, including insert, delete, replace, rename, and dynamic updating function invocation expressions. Basic updating expression are always classified as updating expressions. ] In addition two new kinds of expressions named transform with, and copy modify expressions can apply pending update lists to copies of nodes. These expressions are usually classified as simple expressions rather than updating expressions.

2.3 Updating Functions

[Definition: An updating function is a functionDM30 which can return a non-empty pending update list when called. ] [Definition: If a function is not an updating function, then it is a simple function. ]

2.3.1 Declaring an Updating Function

[26]    AnnotatedDecl    ::=    "declare" (CompatibilityAnnotation | Annotation)* (VarDecl | FunctionDecl)
[27]    CompatibilityAnnotation    ::=    "updating"
[28]    Annotation    ::=    "%" EQName ("(" Literal ("," Literal)* ")")?
[167]    InlineFunctionExpr    ::=    Annotation* "function" "(" ParamList? ")" ("as" SequenceType)? FunctionBody

XQuery Update Facility 3.0 defines the %updating annotation which can be used to declare an updating function. The bare keyword updating is also allowed on a function declared in the prolog for backwards compatibility with XQuery Update 1.0, and behaves exactly as if the %updating annotation was specified instead.

For completeness, the %simple annotation is also defined. This can be used to explicitly label a simple function. A FunctionDecl without an %updating or %simple annotation is also considerd a simple function.

If an %updating or %simple annotation is used on a VarDecl a static error is raised [err:XUST0032]. It is a static error to use more than one %updating or %simple annotation in a given annotation set [err:XUST0033].

2.3.2 Testing for an Updating Function

[193]    FunctionTest    ::=    Annotation* (AnyFunctionTest
| TypedFunctionTest)
[194]    AnyFunctionTest    ::=    "function" "(" "*" ")"
[195]    TypedFunctionTest    ::=    "function" "(" (SequenceType ("," SequenceType)*)? ")" "as" SequenceType

[Definition: An updating function assertion is a function assertionXQ30 that can be used to control how a FunctionTest matches updating functions. ] Updating function assertions can be added to either allow, prohibit, or require an updating function that matches the FunctionTest. The updating function assertions defined are as follows:

Function Assertions for Matching Updating Functions
Function Assertion Matching Behaviour Default?
%simple, or %updating("prohibited") Matches only simple functions true
%updating, or %updating("allowed") Matches both simple functions and updating functions false
%updating("required") Matches only updating functions false

A FunctionTest without an updating function assertion only matches simple functions as it does in unextended XQuery 3.0.

It is a static error to use more than one updating function assertion in the function assertionXQ30 set of a FunctionTest [err:XUST0034].

2.4 Update Operations

XQuery Update Facility 3.0 also defines a set of update operations. [Definition: Update operations are used in defining the semantics of XQuery updates, but are not directly available to users. Update operations are defined in [8 Update Operations]. ] Update operations fall into the following categories:

  1. [Definition: Update primitives are the components of pending update lists. Each update primitive represents a node state change that has not yet been applied. ] [Definition: The first argument of an update primitive, called its target node, is the principal node to be affected by the update primitive. ] Update primitives are held on pending update lists until they are made effective by a upd:applyUpdates operation.

  2. [Definition: Update routines are sequences of actions that are used in the definition of XQuery semantics but do not appear on pending update lists. ] upd:applyUpdates is an example of an update routine.

2.5 Applying Pending Updates

If the outermost expression in a query returns a non-empty pending update list, upd:applyUpdates is implicitly invoked, supplying as arguments (a) the pending update list, and (b) the value of the revalidation mode in the static context of the main module. This invocation of upd:applyUpdates may raise an error (see [8.2.3 upd:applyUpdates] for possible error codes.)

[Definition: A snapshot is a scope within which expressions are evaluated with respect to a fixed XDM instance and updates are held pending. ] A snapshot is terminated by invocation of the upd:applyUpdates operation. XQuery Update Facility 3.0 defines an entire query as one snapshot.

2.6 Revalidation

In general, updating expressions cause a loss of type information from nodes that are affected by the update. Type information for these nodes may be recovered by a revalidation process at the end of the snapshot. Revalidation is defined by the upd:revalidate update routine. More information about type loss during update operations can be found in [8.1 Update Primitives], as well as [8.2.5 upd:removeType].