xsl:element

The xsl:element instruction is used to create an output element whose name might be calculated at run-time.

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

The attributes of the generated element are defined by subsequent xsl:attribute elements. The content of the generated element is whatever is generated between the <xsl:element> and </xsl:element> tags.

Additionally, attributes of the generated element can be defined by reference to a named attribute set. The optional use-attribute-sets attribute contains a white-space-separated list of attribute set names. They are applied in the order given: if the same attribute is generated more than once, the later value always takes precedence.

For example, 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>

A new attribute type was added in XSLT 2.0. This 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.

XSLT 3.0 introduces an attribute on-empty="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. This is motivated by streaming; for example one might write:

<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.