<xsl:element> Element
Creates an output element with the specified name.
<xsl:element
name = "element-name"
namespace = "uri-reference"
use-attribute-sets = QName
</xsl:element>
Attributes
- name
Required. The name of the element to create. If this value is a Qualified Names, the element node is created in the namespace currently bound to the prefix, unless overridden by anamespace
attribute. The value of thename
attribute 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 thename
attribute contains a QName, the prefix specified there will be bound to the namespace specified in thenamespace
attribute. This might require additional namespace declarations when serializing. Thenamespace
value is interpreted as an attribute value template.
- use-attribute-sets
A white space separated list of attribute sets, specified as a Qualified Names. 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.
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>