7 Extensions to Existing Expressions

XQuery Update Facility 3.0 provides extensions to the semantics of several existing kinds of XQuery expressions, as specified in this section.

7.1 FLWOR Expression

[42]    FLWORExpr    ::=    InitialClause IntermediateClause* ReturnClause
[43]    InitialClause    ::=    ForClause | LetClause | WindowClause
[44]    IntermediateClause    ::=    InitialClause | WhereClause | GroupByClause | OrderByClause | CountClause
[45]    ForClause    ::=    "for" ForBinding ("," ForBinding)*
[46]    ForBinding    ::=    "$" VarName TypeDeclaration? AllowingEmpty? PositionalVar? "in" ExprSingle
[47]    AllowingEmpty    ::=    "allowing" "empty"
[48]    PositionalVar    ::=    "at" "$" VarName
[49]    LetClause    ::=    "let" LetBinding ("," LetBinding)*
[50]    LetBinding    ::=    "$" VarName TypeDeclaration? ":=" ExprSingle
[51]    WindowClause    ::=    "for" (TumblingWindowClause | SlidingWindowClause)
[52]    TumblingWindowClause    ::=    "tumbling" "window" "$" VarName TypeDeclaration? "in" ExprSingle WindowStartCondition WindowEndCondition?
[53]    SlidingWindowClause    ::=    "sliding" "window" "$" VarName TypeDeclaration? "in" ExprSingle WindowStartCondition WindowEndCondition
[54]    WindowStartCondition    ::=    "start" WindowVars "when" ExprSingle
[55]    WindowEndCondition    ::=    "only"? "end" WindowVars "when" ExprSingle
[56]    WindowVars    ::=    ("$" CurrentItem)? PositionalVar? ("previous" "$" PreviousItem)? ("next" "$" NextItem)?
[57]    CurrentItem    ::=    EQName
[58]    PreviousItem    ::=    EQName
[59]    NextItem    ::=    EQName
[60]    CountClause    ::=    "count" "$" VarName
[61]    WhereClause    ::=    "where" ExprSingle
[62]    GroupByClause    ::=    "group" "by" GroupingSpecList
[63]    GroupingSpecList    ::=    GroupingSpec ("," GroupingSpec)*
[64]    GroupingSpec    ::=    GroupingVariable (TypeDeclaration? ":=" ExprSingle)? ("collation" URILiteral)?
[66]    OrderByClause    ::=    (("order" "by") | ("stable" "order" "by")) OrderSpecList
[67]    OrderSpecList    ::=    OrderSpec ("," OrderSpec)*
[68]    OrderSpec    ::=    ExprSingle OrderModifier
[69]    OrderModifier    ::=    ("ascending" | "descending")? ("empty" ("greatest" | "least"))? ("collation" URILiteral)?
[70]    ReturnClause    ::=    "return" ExprSingle

The syntax of the FLWOR expression is not changed.

Category Rules:
Additional Semantics:
  1. The semantics of all clauses other than the ReturnClause are as specified in Section 3.10 FLWOR Expressions XQ30. These clauses generate a stream of tuples of bound variables.

  2. The result of the FLWOR expression is the concatenation of the XDM instances and pending update lists returned by evaluating the ReturnClause on each tuple generated by the previous step. Pending update lists are concatenated by the upd:mergeUpdates operation.

    Note:

    In the event of incompatible updates, the upd:mergeUpdates operation may raise an error, as described in [8.2.2 upd:mergeUpdates].

Example:
  • Update an inventory of parts according to a set of changes provided in the bound variable $changes. Both /inventory and $changes contain a set of part elements, each with a partno and a quantity.

    for $p in /inventory/part
    let $deltap := $changes/part[partno eq $p/partno]
    return 
        replace value of node $p/quantity
        with $p/quantity + $deltap/quantity

7.2 Typeswitch Expression

[75]    TypeswitchExpr    ::=    "typeswitch" "(" Expr ")" CaseClause+ "default" ("$" VarName)? "return" ExprSingle
[76]    CaseClause    ::=    "case" ("$" VarName "as")? SequenceTypeUnion "return" ExprSingle
[77]    SequenceTypeUnion    ::=    SequenceType ("|" SequenceType)*

The syntax of the typeswitch expression is not changed. The return expressions in the CaseClauses and default clause are called branches, and the expression in parentheses after the keyword typeswitch is called the test.

Category Rules:
Additional Semantics:

Selection of the effective case and binding of variables are performed as specified in Section 3.16.2 Typeswitch XQ30. The expression in the return clause of the effective case (or default) is then evaluated, resulting in an XDM instance and pending update list, which serves as the result of the typeswitch expression.

7.3 Switch Expression

[72]    SwitchExpr    ::=    "switch" "(" Expr ")" SwitchCaseClause+ "default" "return" ExprSingle
[73]    SwitchCaseClause    ::=    ("case" SwitchCaseOperand)+ "return" ExprSingle
[74]    SwitchCaseOperand    ::=    ExprSingle

The syntax of the switch expression is not changed. The return expressions in the CaseClauses and default clause are called branches, and the expression in parentheses after the keyword switch is called the test.

Category Rules:
Additional Semantics:

Selection of the effective case is performed as specified in Section 3.13 Switch Expression XQ30. The expression in the return clause of the effective case (or default) is then evaluated, resulting in an XDM instance and pending update list, which serves as the result of the switch expression.

7.4 Conditional Expression

[78]    IfExpr    ::=    "if" "(" Expr ")" "then" ExprSingle "else" ExprSingle

The syntax of conditional expression is not changed. The operands of the then and else clauses are called branches, and the expression in parentheses after the keyword if is called the test.

Category Rules:
Additional Semantics:

Selection of the effective branch performed as specified in Section 3.12 Conditional Expressions XQ30. The result of the conditional expression is the XDM instance and pending update list returned by the selected branch.

Example:
  • If the element bound to variable $e has a last-updated attribute, update its value to the current date; otherwise insert such an attribute.

    if ($e/@last-updated)
    then replace value of node 
            $e/last-updated with fn:currentDate()
    else insert node 
            attribute last-updated {fn:currentDate()} into $e

7.5 Try/Catch Expression

[79]    TryCatchExpr    ::=    TryClause CatchClause+
[80]    TryClause    ::=    "try" "{" TryTargetExpr "}"
[81]    TryTargetExpr    ::=    Expr
[82]    CatchClause    ::=    "catch" CatchErrorList "{" Expr "}"
[83]    CatchErrorList    ::=    NameTest ("|" NameTest)*
Category Rules:
Additional Semantics:

The TryTargetExpr is evaluated, resulting in a pending update list or raising an error. If a pending update list is returned, it is checked for compatibility using upd:compatibilityCheck, which may also raise an error. If an error has not been raised, the result of the try/catch expression is the XDM instance and pending update list returned by it. However if it raises an error the result of the try/catch expression is the XDM instance and pending update list returned by executing the CatchClause matching the error, as defined in Section 3.15 Try/Catch Expressions XQ30.

Note:

Since the pending update list is considered part of the result of an expression, an error raised while executing the TryTargetExpr results in no update operations being returned from the execution of that expression.

7.6 Comma Expression

[40]    Expr    ::=    ExprSingle ("," ExprSingle)*

The comma expressions is composed of one or more expressions concatenated by the comma operator, as described in Section 3.4.1 Constructing Sequences XQ30.

Category Rules:
Additional Semantics:

The operand expressions of the comma expression are evaluated, and the XDM instances and pending update lists returned are concatenated in operand order. Pending update lists are concatenated by the upd:mergeUpdates operation.

Examples:
  • This example makes the value of an element empty and gives the element an xsi:nil="true" attribute. Both of these operations may be necessary in order to preserve the validity of the element.

    let $q := /inventory/item[serialno = "123456"]/quantity
    return
       ( replace value of node $q with ( ),
         insert node attribute xsi:nil {"true"} into $q )

7.7 Parenthesized Expression

[131]    ParenthesizedExpr    ::=    "(" Expr? ")"
Category Rules:

The category of a parenthesized expression is the same as the category of the expression enclosed in parentheses, which may have any category.

Additional Semantics:

The result of a parenthesized expression is the same as the result of the expression enclosed in parentheses.

7.8 Extension Expression

[105]    ExtensionExpr    ::=    Pragma+ "{" Expr? "}"
Category Rules:

The category of an extension (pragma) expression is the same as the category of the enclosed expression, which may have any category.

Additional Semantics:

The result of an extension (pragma) expression is the same as the result of the enclosed expression.

7.9 Inline Functions

[167]    InlineFunctionExpr    ::=    Annotation* "function" "(" ParamList? ")" ("as" SequenceType)? FunctionBody
[36]    FunctionBody    ::=    EnclosedExpr

The %simple and %updating annotations can be specified at the start of the inline function expression, to force the creation of a simple or updating function repectively.

Category Rules:
  1. If neither %simple nor %updating is specified:

    1. The FunctionBody can be an expression of any category.

    2. If the FunctionBody is an updating expression, the inline function expression is a simple expression, returning an updating function.

    3. If the FunctionBody is a simple expression, the inline function expression is a simple expression, returning a simple function.

  2. If %simple is specified:

    1. The FunctionBody must not be an updating expression [err:XUST0001]

    2. The inline function expression is a simple expression, returning a simple function.

  3. If %updating is specified:

    1. The FunctionBody can be an expression of any category.

    2. The inline function expression is a simple expression, returning an updating function.

  4. It is a static error to use more than one %updating or %simple annotation in a given annotation set [err:XUST0033].

7.10 Function Calls

[135]    FunctionCall    ::=    EQName ArgumentList
[123]    ArgumentList    ::=    "(" (Argument ("," Argument)*)? ")"
[136]    Argument    ::=    ExprSingle | ArgumentPlaceholder
[137]    ArgumentPlaceholder    ::=    "?"

Function calls can either be a partial function applicationXQ30 or not.

Category Rules:
Additional Semantics:

The function call is evaluated as specified in Section 3.1.5 Static Function Calls XQ30. All pending update lists returned by the evaluation of argument expressionsXQ30 are concatenated with the pending update list returned by evaluating the function to form the pending update list returned from the function call expression.

7.11 Dynamic Function Invocation

[122]    PostfixExpr    ::=    PrimaryExpr (Predicate | ArgumentList)*
[123]    ArgumentList    ::=    "(" (Argument ("," Argument)*)? ")"
[136]    Argument    ::=    ExprSingle | ArgumentPlaceholder
[137]    ArgumentPlaceholder    ::=    "?"

Dynamic function invocation can either be a partial function applicationXQ30 or not.

Category Rules:
Additional Semantics:

The dynamic function invocation is evaluated as specified in Section 3.2.2 Dynamic Function Call XQ30.

If the function returned by the PrimaryExpr is an updating function, and the dynamic function invocation is not a partial function applicationXQ30, a dynamic error is raised [err:XUDY0038]. To perform dynamic function invocation of an updating function, [5.5 Dynamic Updating Function Invocation] should be used.

All pending update lists returned by the evaluation of the PrimaryExpr or argument expressionsXQ30 are concatenated to form the pending update list returned from the dynamic function invocation expression.

7.12 Direct and Computed Node Constructors

[140]    DirElemConstructor    ::=    "<" QName DirAttributeList ("/>" | (">" DirElemContent* "</" QName S? ">"))
[141]    DirAttributeList    ::=    (S (QName S? "=" S? DirAttributeValue)?)*
[151]    CDataSection    ::=    "<![CDATA[" CDataSectionContents "]]>"
[149]    DirPIConstructor    ::=    "<?" PITarget (S DirPIContents)? "?>"
[147]    DirCommentConstructor    ::=    "<!--" DirCommentContents "-->"
[155]    CompElemConstructor    ::=    "element" (EQName | ("{" Expr "}")) "{" ContentExpr? "}"
[157]    CompAttrConstructor    ::=    "attribute" (EQName | ("{" Expr "}")) "{" Expr? "}"
[154]    CompDocConstructor    ::=    "document" "{" Expr "}"
[162]    CompTextConstructor    ::=    "text" "{" Expr "}"
[164]    CompPIConstructor    ::=    "processing-instruction" (NCName | ("{" Expr "}")) "{" Expr? "}"
[163]    CompCommentConstructor    ::=    "comment" "{" Expr "}"
Category Rules:
Additional Semantics:

The pending update lists returned by each of the operand expressions are merged by calling upd:mergeUpdates, and returned as the pending update list part of the result of the node constructor.

7.13 Other Expressions

All other XQuery expressions not listed in the sections preceeding this are extended as follows:

Category Rules: