XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / element
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
The instruction xsl:element creates element nodes. The name and namespace of the new element can be determined dynamically through an attribute value template.
Classification | Instruction |
Function group | Output to the result document |
Introduction: | XSLT 1.0 |
Position in the stylesheet and permitted content:
xsl:element is an instruction used inside sequence constructors or template blocks.
xsl:element may be empty to create an empty element. Usually it contains a sequence constructor that produces the element’s content, using any XSLT instructions and literal result elements.
Attributes:
Standard attributes apply to xsl:element. It also has required attribute name and optional attributes namespace, use-attribute-sets, plus, from XSLT 2.0, optional attributes inherit-namespaces, type and validation.
inherit-namespaces
Value | "yes" (default) | "no" |
Usage | Optional |
Introduction | XSLT 2.0 |
Attribute inherit-namespaces uses values yes or no to control whether namespace nodes are inherited by children created inside the sequence constructor.
If absent, the default is yes, so constructed or copied namespace nodes are normally inherited. If set to no, no namespaces are inherited by child elements. Serialization can express this through explicit undeclarations such as xmlns="" or similar forms.
Note: This attribute is unavailable in XSLT 1.0.
name
Value | {QName} (also as AVT) |
Usage | Required |
Introduction | XSLT 1.0 |
The name attribute supplies the generated element’s QName, which can include a prefix. Its namespace URI can be specified with attribute namespace. An AVT can determine the element name dynamically.
namespace
Value | {URI-reference} (also as AVT) |
Usage | Optional |
Introduction | XSLT 1.0 |
The namespace attribute supplies the new element’s namespace URI. The name attribute may specify a prefix; otherwise one is generated. An AVT can also generate the namespace URI dynamically.
type
Value | QName |
Usage | Optional |
Introduction | XSLT 2.0 |
The type attribute gives the QName of a schema type to associate with copied element and attribute nodes. It may be a predefined (built-in) type or one from an imported schema. A missing type declaration is a static error. If validation succeeds, meaning property validity on the generated PSVI has value valid, the type is associated with the element; otherwise transformation stops.
Note: The type attribute on xsl:element must not coexist with attribute validation. It is unavailable in XSLT 1.0.
use-attribute-sets
Value | QNames |
Usage | Optional |
Introduction | XSLT 1.0 |
The use-attribute-set attribute imports attribute sets using their whitespace-separated QNames. If imported sets contain conflicting same-named attributes, the first imported takes precedence according to QName-list order.
validation
Value | "strict" | "lax" | "preserve" | "strip" |
Usage | Optional |
Introduction | XSLT 2.0 |
The validation attribute controls the validation level. Its default is strip unless the xsl:stylesheet element specifies a default-validation attribute with another value.
Note: The validation attribute on xsl:element must not coexist with attribute type. It is unavailable in XSLT 1.0.
The four explicit values are strict, lax, preserve and strip. Their effects on xsl:element are:
validation = "strict"
Valuestrictrequires strict schema validation of the generated element. A declaration must be available using standard XML Schema methods. The namespace may identify it implicitly. The resultingvalidityproperty in its PSVI must equalvalid. If it equalsinvalidornotKnown, the transformation fails with a type error (type error).
validation = "lax"
Valuelaxalso validates against an available schema, but missing schemas or element declarations are not errors. Thevalidityproperty may therefore equal eithervalidornotKnown. In the latter case, the unvalidated element receives typexs:untyped. If propertyvalidityequalsinvalid, transformation fails with a type error (type error).
validation = "preserve"
Valuepreservenormally preserves an explicitly assigned type. For elements created withxsl:elementthis is meaningless, sopreservehas the same effect asstrip, assigning typexs:anyTypeto the element.
validation = "strip"
Valuestripassigns typexs:untypedto the generated element. Normally,stripremoves existing type annotations, but an element created withxsl:elementcannot have had a previous type association.
Purpose:
Instruction xsl:element adds an element node to the result. If its name or namespace cannot be predetermined, xsl:element is the only way to create it: both may be determined by AVTs at runtime. If the name is fixed, xsl:copy or literal result elements may be simpler.
The template body constructs the element’s children and text. Any XSLT instructions and literal result elements are allowed.
Attributes come from previously defined sets through attribute use-attribute-sets or from instruction xsl:attribute inside the constructor, before any child nodes are added. For duplicate names, the last-added attribute wins. Attributes created with xsl:attribute thus override same-named attributes from sets.
The element’s QName may include a prefix. Without a separately supplied URI, that prefix must have an in-scope stylesheet declaration. The created element automatically receives the corresponding namespace node.
The prefix need not be declared in the stylesheet if attribute namespace also supplies a namespace URI. The element uses that URI, normally bound to the prefix in attribute name, though the parser may choose another prefix if necessary.
Examples:
Example 1 – creating an element:
<xsl:variable name="der_name">mein_element</xsl:variable>
<xsl:element name="{$der_name}"/>
produces:
<mein_element/>An AVT reads the element name from a variable.
Example 2 – nested xsl:element:
<xsl:element name="buch">
<xsl:element name="titel">Finnegan's Wake</xsl:element>
<xsl:element name="autor">James Joyce</xsl:element>
</xsl:element>
produces:
<buch>
<titel>Finnegan's Wake</titel>
<autor>James Joyce</autor>
</buch>
Example 3 – supplied namespace:
<xsl:element name="galileo:buch" namespace="http://www.galileocomputing.de">
<xsl:element name="titel">Einstieg in XML</xsl:element>
<xsl:element name="autor">Helmut Vonhoegen</xsl:element>
</xsl:element>
produces:
<galileo:buch xmlns:galileo="http://www.galileocomputing.de">
<titel>Einstieg in XML</titel>
<autor>Helmut Vonhoegen</autor>
</galileo:buch>
The parser adds the corresponding namespace declaration.
Example 4 – nesting with supplied namespace:
The parent’s supplied namespace is not in stylesheet scope and does not automatically apply inside it at runtime. Without explicit namespace URIs for the inner elements, the parser rejects their creation.
<xsl:element name="galileo:buch" namespace="http://www.galileocomputing.de">
<xsl:element name="galileo:titel" namespace="http://www.galileocomputing.de">Einstieg in XML</xsl:element>
<xsl:element name="galileo:autor" namespace="http://www.galileocomputing.de">Helmut Vonhoegen</xsl:element>
</xsl:element>
produces:
<galileo:buch xmlns:galileo="http://www.galileocomputing.de">
<galileo:titel>Einstieg in XML</galileo:titel>
<galileo:autor>Helmut Vonhoegen</galileo:autor>
</galileo:buch>
In the result, child elements automatically inherit the outer element’s namespace scope. The parser therefore emits attribute xmlns only once.
Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:element
name = { qname }
namespace = { uri-reference }
use-attribute-sets = qnames>
<!-- Content: template -->
</xsl:element>
XSLT 2.0:
<!-- Category: instruction -->
<xsl:element
name = { qname }
namespace? = { uri-reference }
inherit-namespaces? = "yes" | "no"
use-attribute-sets? = qnames
type? = qname
validation? = "strict" | "lax" | "preserve" | "strip">
<!-- Content: sequence-constructor -->
</xsl:element>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn