<xsl:element> Element
Creates an output element with the specified name.
<xsl:element name = "element-name" namespace = "uri-reference" use-attribute-sets = QNames> </xsl:element>
Attributes
- name
- Required. The name of the element to create. If this value is a qualified name (QName), the element node is created in the namespace currently bound to the prefix, unless overridden by a
namespaceattribute. The value of thenameattribute is interpreted as an attribute value template—that is, expressions in curly braces are evaluated and converted to strings, as in <xsl:value-of>. This allows the name of the element to be calculated or obtained from the source XML. - namespace
- The namespace URI of the created element. If the
nameattribute contains a QName, the prefix specified there will be bound to the namespace specified in thenamespaceattribute. This might require additional namespace declarations when serializing. Thenamespacevalue is interpreted as an attribute value template. - use-attribute-sets
- A white space separated list of attribute sets, specified as a qualified name. Specifying this attribute declares each attribute in each listed attribute set.
Element Information
| Number of occurrences | Unlimited |
| Parent elements | xsl:copy, xsl:element, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:otherwise, xsl:param, xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements |
| Child elements | xsl:apply-templates, xsl:attribute, xsl:call-template, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:for-each, xsl:if, xsl:processing-instruction, xsl:text, xsl:value-of, xsl:variable, output elements |
Remarks
The <xsl:element> element allows an element to be created with a computed name. The name of the element to be created is specified by a required name attribute and an optional namespace attribute. The content of the <xsl:element> element is a template for the attributes and children of the created element.
This element provides an escaping mechanism for creating elements with namespace clashes, such as XSLT itself.
Example
The <xsl:element> element is necessary because XSLT elements cannot be used as output elements. This example shows how to output an <xsl:template> element.
Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.
XML File (item.xml)
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="element.xsl" ?> <root> <item>My Item</item> </root>
XSLT File (element.xsl)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="item">
<xsl:element name="xsl:template">
<xsl:attribute name="match">cost</xsl:attribute>
<xsl:attribute name="xml:space">preserve</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output
This is the formatted output:
My Item
The following is the processor output, with line breaks added for clarity.
<?xml version="1.0"?>
<xsl:template match="cost"
xml:space="preserve"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
My Item</xsl:template>
See Also
Generating Comments, Processing Instructions, and Elements | Qualified Names
