xsl:element

Used to create an output element whose name might be calculated at run-time.

Category: instruction
Content: sequence-constructor
Permitted parent elements: any XSLT element whose content model is sequence constructor; any literal result element

Attributes

name

{ qname }

The name of the generated element. The name attribute is an attribute value template, so it may contain string expressions inside curly braces.

namespace?

{ uri }

inherit-namespaces?

boolean

use-attribute-sets?

eqnames

Attributes of the generated element can be defined by reference to named attribute sets, provided as a white-space-separated list. They are applied in the order given: if the same attribute is generated more than once, the later value always takes precedence.

type?

eqname

Added in XSLT 2.0. Indicates the data type of the value of the element. The value may be a built-in type defined in XML Schema, for example xs:integer or xs:date, or a user-defined type defined in a schema imported using xsl:import-schema. Type annotations are only accessible if the attribute is added to a temporary tree that specifies type-information="preserve". The attribute causes the content of the element to be validated against the schema definition of the type, and will cause a fatal dynamic error if validation fails.

validation?

"strict" | "lax" | "preserve" | "strip"

on-empty?

expression

Added in XSLT 3.0, motivated by streaming. Expression which defines an alternative result in the case where the element would otherwise be empty. The most likely value is on-empty="()", which means that empty elements are not written (see example below).

Details

The attributes of the generated element are defined by subsequent xsl:attribute elements, or by reference to named attribute sets using the optional use-attribute-sets attribute. The content of the generated element is whatever is generated between the <xsl:element> and </xsl:element> tags.

Examples

Example 1

The following code creates a <FONT> element with several attributes:

<xsl:element name="FONT"> <xsl:attribute name="SIZE">4</xsl:attribute> <xsl:attribute name="FACE">Courier New</xsl:attribute> Some output text </xsl:element>

Example 2

The following is an example using the on-empty="()" attribute new in XSLT 3.0, motivated by streaming:

<xsl:element name="authors" on-empty="()"> <xsl:apply-templates/> </xsl:element>

This has the effect that if there are no authors, no <authors> element will be written. Achieving this within the constraints of streaming can be difficult. But of course the feature is also a convenient short-cut quite independently of streaming.

Links to W3C specifications

XSLT 2.0 Specification

XSLT 3.0 Specification

See also

xsl:attribute

xsl:copy