Construct an attribute by computing an expression.
The following construct(s) refer to this construct:
A ComputedAttributeConstructor
constructs an attribute by
computing the enclosed ExprSequence
, called content
expression. The name of the attribute 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 and, after atomization, converted into a sequence of atomic values each of which is cast into a string and concatenated with a space character between each pair of values. If the result of the expression after atomization is an empty sequence, the string is of length zero.
Tip:
A computed attribute constructor is needed in the context of update
expressions, since you cannot construct a stand-alone attribute with the
constructors using regular XML syntax.
This query constructs an attribute node named
size
and the computed value
"6":
attribute size { 4 + 2 }
Computed element constructors may be nested and several constructors are separated by commas:
element section { attribute type { "overview" }, attribute lang { "en" }, element para { "This section deals with attribute constructors." } }
See the section Performing
Update Operations in the XQuery 4 User Guide
for information about updating attribute nodes with the help of a
ComputedAttributeConstructor
.