• +49-(0)721-402485-12
Ihre Experten für XML, XQuery und XML-Datenbanken

The computed element constructor

When using the direct element constructor, element content and attribute values can be computed by expressions; however, the element name is constant. In case also the name of an element shall be calculated, a computed element constructor is to be used. In doing so, the key word element is followed by the name of the element, namely as constant QName or as expression in an evaluation context whose result is again subject to the atomisation and then must have one of the types xs:QName , xs:string or xs:untypedAtomic. Then comes the element content in an evaluation context, again with the rule that attribute nodes have to stand at the beginning of the resulting sequence. The same applies for namespace nodes which indeed have to be placed before the attribute nodes. All atomic values in the element content become text nodes, whereby adjoining text nodes are combined. So, for example

element LaboratoryTest {
attribute ID {"LaboratoryTest_040782"},
element Number {1},
element Date {"2002-05-10T10:30:00-05:00"},
<TestObject>Left thigh</TestObject>,
element Laboratory {
namespace xlink {"http://www.w3.org/1999/xlink"},
xlink:href {'Hochwaldklinik.xml#xpointer(id("Radiology"))'}

becomes the following element:

<LaboratoryTest ID="LaboratoryTest_040782">
<TestObject>Left thigh</TestObject>
<Laboratory xmlns:xlink="http://www.w3.org/1999/xlink"

As you can see, within the element constructor again both types of element constructors can be used. In the Laboratory element a namespace node for the xlink prefix must be generated if it is not already defined in the environment. You can also see that it is not important which type is used within an element constructor as long as it is convertible to xs:string (in the Number element an integer literal is used, in Date a character string, although the element is of xs:date type). The syntax of the computed element constructor is:

CompElemConstructor::=element ( QName | { Expr } ) { Expr? }

Common rules for both constructor types

If names with a namespace prefix are used in an element constructor, the prefix must be defined, namely in the XQuery prologue or in the element itself.

By taking into account the validation context, the resulting element is validated against the known Schema definitions and, where applicable, a type is assigned to it. This also implies that attributes with their default values can be added to the element. This validation process takes a possibly indicated xsi:type attribute into account. In this way, the content of the following element is validated against xs:integer, even if no definition is known for the element:

<Unknown xsi:type="xs:integer">42</Unknown>


Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)

<< backnext >>