Creates an output element with the specified name.
<xsl:element name = "element-name" namespace = "uri-reference" use-attribute-sets = QNames> </xsl:element>
namespace
attribute. The value of the name
attribute is interpreted as an attribute value templatethat 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.name
attribute contains a QName, the prefix specified there will be bound to the namespace specified in the namespace
attribute. This might require additional namespace declarations when serializing. The namespace
value is interpreted as an attribute value template.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 |
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.
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>
Generating Comments, Processing Instructions, and Elements | Qualified Names