Computed element constructors
A computed element constructor creates an element node for which the content of the node is computed based on an enclosed expression.
The result of a computed element constructor is a new element node that has its own node identity. All of the attribute and descendant nodes of the new element node are also new nodes that have their own identities, even if they are copies of existing nodes.
Syntax
- element
- A keyword that indicates that an element node will be constructed.
- ElementName
- The QName of the element to construct. If ElementName includes a namespace prefix, the prefix is resolved to a namespace URI by using the statically known namespaces. If ElementName has no namespace prefix, the name is implicitly qualified by the default element/type namespace. The expanded QName that results from evaluating ElementName becomes the name of the constructed element node.
- ContentExpression
- An expression that generates the content of the constructed element node. The value of ContentExpression can be any sequence of nodes and atomic values. ContentExpression can be used to compute both the content and the attributes of the constructed node. For each node that is returned by ContentExpression, a new copy is made of the node and all of its descendants, which retain their original type annotations. Any attribute nodes that are returned by ContentExpression must be at the beginning of the node sequence (before any other nodes); these attribute nodes become attributes of the constructed element. Any element, content, or processing instruction nodes that are returned by ContentExpression become children of the newly constructed node. Any atomic values that are returned by ContentExpression are converted to strings and stored in text nodes, which become children of the constructed node. Adjacent text nodes are merged into a single text node.
Example
In the following expression,
a computed element constructor makes a modified copy of an existing element.
Suppose that the variable
$e is bound to an element that
has numeric content. This constructor creates a new element named length that
has the same attributes as $e and has numeric content equal
to twice the content of $e:element length {$e/@*, 2 * fn:data($e)} In
this example, if the variable $e is bound to the expression let
$e := <length units="inches">{5}</length>,
then the result of the example expression is the element <length
units="inches">10</length>.