ComputedElementConstructor

Construct an element by computing an expression.


Syntax

ComputedElementConstructor

graphics/ComputedElementConstructor.png

QName Expr ExprSequence

Description

A ComputedElementConstructor constructs an element by computing the enclosed ExprSequence, called content expression. The name of the element is either a QName or it is computed from an expression (name expression).

The value resulting from evaluating the name expression undergoes atomization, and must be a single atomic value of the type xs:QName, xs:string, or xdt:untypedAtomic, otherwise a type error is raised. If it is of type xs:QName, its expanded QName is used as element name and the prefix part of the QName is retained. Otherwise, the value is converted to an expanded QName. If a namespace prefix is present, it is resolved using the statically known namespaces, otherwise it is local in the default namespace.

The content expression is evaluated in the same way as an enclosed expression in an ElementConstructor.

Examples

  • Computed constructors may be nested and several constructors are separated by commas. Note how the expression sequence is evaluated for the nested element constructors:

    for $a in input()/bib/book
    return
    element book {
      element title { $a/title },
      element notitle { "$a/title" }
    }

    This query is equivalent with the following one:

    for $a in input()/bib/book
    return
    <book>
      <title>{ $a/title }</title>
      <notitle>{ "$a/title" }</notitle>
    </book>
  • This query returns the alphabetical list of patient names grouped by the initial letter of the patients' surnames:

    let $patients := input()/patient/name
    let $letters := distinct-values(for    $patient in $patients
                                    return substring($patient/surname, 1, 1))
    return
      <patients> {
         for    $letter in $letters
         return element { $letter } {
           for      $patient in $patients[substring(surname, 1, 1) = $letter]
           order by $patient/surname
           return   $patient }
    }  
      </patients>

    Here, the name of the element is computed using the sequence of unique letters resulting from the first nested FLWOR expression. The content expression is another FLWOR expression returning the ordered list of all name elements for all letters found.

Related Syntax Constructs

The following construct(s) refer to this construct:

This construct is also related to the following construct(s):

ElementConstructor