XSLT - Creating Dynamic Elements with <xsl:element> in XSLT

The <xsl:element> instruction is used in XSLT to create an XML element dynamically. Unlike normal literal elements, where the element name is written directly in the stylesheet, <xsl:element> allows the element name to be determined at transformation time.

This is particularly useful when the name of an output element depends on a value in the source XML, a variable, a parameter, or some other calculated expression.

1. What is <xsl:element>?

Normally, an XSLT stylesheet can create an element simply by writing its name:

<xsl:template match="/">
    <student>
        <name>John</name>
    </student>
</xsl:template>

Here, student and name are fixed element names.

With <xsl:element>, the name can be dynamic:

<xsl:element name="student">
    <name>John</name>
</xsl:element>

The output will be:

<student>
    <name>John</name>
</student>

Although this example uses a fixed name, the major advantage is that the name attribute can contain an XPath expression.

2. Basic Syntax

The general syntax is:

<xsl:element name="expression">
    content
</xsl:element>

The name attribute specifies the name of the element that should be created.

For example:

<xsl:element name="book">
    <xsl:text>XML Programming</xsl:text>
</xsl:element>

Output:

<book>XML Programming</book>

The important point is that <xsl:element> creates an element in the result tree during transformation.

3. Creating an Element Name from Source XML

One of the most useful applications is creating an output element whose name comes from the source document.

Suppose the source XML is:

<products>
    <product>
        <category>Book</category>
        <name>XML Fundamentals</name>
    </product>
    <product>
        <category>Software</category>
        <name>XSLT Processor</name>
    </product>
</products>

The value of category can be used as the name of an output element.

Example:

<xsl:template match="product">
    <xsl:element name="{category}">
        <xsl:value-of select="name"/>
    </xsl:element>
</xsl:template>

The result could be:

<Book>XML Fundamentals</Book>
<Software>XSLT Processor</Software>

Here, the element name is not hard-coded. It is obtained from the source XML.

4. Attribute Value Templates

The expression:

name="{category}"

uses an attribute value template.

The curly braces {} tell XSLT that the expression inside them should be evaluated.

For example:

<xsl:element name="{category}">

means:

  1. Find the value of category.

  2. Evaluate that value.

  3. Use the resulting value as the name of the new element.

If:

<category>Book</category>

is found, XSLT creates:

<Book>

If the value is:

<category>Magazine</category>

XSLT creates:

<Magazine>

This makes the transformation dynamic.

5. Creating Multiple Dynamic Elements

<xsl:element> is especially useful inside loops.

Consider:

<students>
    <student>
        <name>Arun</name>
    </student>
    <student>
        <name>Priya</name>
    </student>
</students>

An XSLT stylesheet could create elements dynamically based on student names:

<xsl:template match="students">
    <result>
        <xsl:for-each select="student">
            <xsl:element name="student-{position()}">
                <xsl:value-of select="name"/>
            </xsl:element>
        </xsl:for-each>
    </result>
</xsl:template>

Possible output:

<result>
    <student-1>Arun</student-1>
    <student-2>Priya</student-2>
</result>

The element names are generated during the transformation.

6. Using Variables with <xsl:element>

A variable can also determine the element name.

Example:

<xsl:variable name="elementName" select="'employee'"/>

<xsl:element name="{$elementName}">
    <xsl:text>Employee Information</xsl:text>
</xsl:element>

Output:

<employee>Employee Information</employee>

The variable contains the element name, and <xsl:element> uses it to construct the result element.

This is useful when the same dynamic name needs to be reused in several places.

7. Using Calculated Element Names

The name does not have to come directly from one XML element. It can be calculated.

For example:

<xsl:element name="item-{position()}">
    <xsl:value-of select="."/>
</xsl:element>

If the current position is 3, the output element will be:

<item-3>...</item-3>

Similarly, if the position is 5:

<item-5>...</item-5>

This demonstrates how XPath expressions can be used to construct dynamic element names.

8. Creating Dynamic Elements with Attributes

Dynamic elements can contain attributes as well.

Example:

<xsl:element name="{category}">
    <xsl:attribute name="id">
        <xsl:value-of select="@id"/>
    </xsl:attribute>

    <xsl:value-of select="name"/>
</xsl:element>

Suppose the source XML contains:

<product id="101">
    <category>Book</category>
    <name>XML Fundamentals</name>
</product>

The result can be:

<Book id="101">XML Fundamentals</Book>

The element name is dynamic, while the attribute is also generated from the source data.

9. Dynamic Element Names vs Literal Elements

There is an important difference between a literal result element and <xsl:element>.

A literal element:

<book>
    <xsl:value-of select="title"/>
</book>

always creates:

<book>...</book>

The name book is fixed.

With:

<xsl:element name="{category}">
    <xsl:value-of select="title"/>
</xsl:element>

the name depends on the value of category.

If:

<category>book</category>

the output is:

<book>...</book>

If:

<category>magazine</category>

the output becomes:

<magazine>...</magazine>

Therefore, <xsl:element> should be used when the element name needs to be determined dynamically.

10. Creating Elements in Different Namespaces

<xsl:element> can also be used when working with XML namespaces.

For example:

<xsl:element name="p:product"
             namespace="http://example.com/products">
    <xsl:text>Computer</xsl:text>
</xsl:element>

The namespace attribute specifies the namespace URI associated with the generated element.

This is useful when transforming XML documents that use multiple namespaces.

11. Important Attributes of <xsl:element>

The most important attribute is:

name

It specifies the name of the element to be created.

Example:

<xsl:element name="employee">

Another useful attribute is:

namespace

It specifies the namespace URI for the generated element.

Example:

<xsl:element name="product"
             namespace="http://example.com/products">

In modern XSLT, additional attributes can be used for namespace-related control depending on the XSLT version and processor.

12. Example: Dynamic Table Structure

Suppose the source XML is:

<data>
    <field name="name">John</field>
    <field name="age">25</field>
    <field name="city">Bangalore</field>
</data>

We can create elements based on the name attribute:

<xsl:template match="data">
    <result>
        <xsl:for-each select="field">
            <xsl:element name="{@name}">
                <xsl:value-of select="."/>
            </xsl:element>
        </xsl:for-each>
    </result>
</xsl:template>

The result will be:

<result>
    <name>John</name>
    <age>25</age>
    <city>Bangalore</city>
</result>

This is a practical example because the source XML controls the structure of the output XML.

13. Example: Dynamic HTML Elements

<xsl:element> can also be useful when generating HTML structures dynamically.

For example:

<xsl:variable name="tag" select="'h2'"/>

<xsl:element name="{$tag}">
    <xsl:text>Welcome to XSLT</xsl:text>
</xsl:element>

The result is:

<h2>Welcome to XSLT</h2>

If the variable contains h3 instead:

<xsl:variable name="tag" select="'h3'"/>

the result becomes:

<h3>Welcome to XSLT</h3>

Thus, the same stylesheet can produce different element types based on data.

14. When Should <xsl:element> Be Used?

It is appropriate when:

  • The element name comes from source XML.

  • The element name is stored in a variable.

  • The element name depends on an XPath expression.

  • The output structure needs to be generated dynamically.

  • Namespace information needs to be controlled dynamically.

  • The transformation must generate different element names based on input data.

15. When Should It Not Be Used?

If the element name is always known, a literal result element is generally simpler.

Instead of:

<xsl:element name="student">
    <xsl:value-of select="name"/>
</xsl:element>

you can simply write:

<student>
    <xsl:value-of select="name"/>
</student>

Using <xsl:element> unnecessarily can make a stylesheet harder to read.

Therefore, it is best reserved for situations where the element name genuinely needs to be dynamic.

16. Advantages of <xsl:element>

The major advantages are:

  1. Dynamic element creation
    Element names can be determined at transformation time.

  2. Flexible XML transformation
    It allows the output structure to depend on source data.

  3. Variable-based naming
    Variables can control the names of generated elements.

  4. XPath-based naming
    Element names can be generated from XPath expressions.

  5. Namespace support
    It can be used to create elements associated with particular namespaces.

  6. Reusable stylesheets
    A stylesheet can handle different input structures without hard-coding every possible output element name.

17. Important Precautions

The dynamically generated name must be a valid XML element name. If the source contains an invalid name, the transformation processor may report an error.

For example, a value such as:

123student

cannot normally be used directly as an XML element name because it begins with a number.

Similarly, dynamically generated names should be handled carefully when namespace prefixes are involved.

18. Summary

<xsl:element> is an XSLT instruction for creating elements dynamically. Its main advantage over ordinary literal elements is that the output element name can be determined from source data, variables, parameters, or XPath expressions.

The basic structure is:

<xsl:element name="expression">
    content
</xsl:element>

For example:

<xsl:element name="{@type}">
    <xsl:value-of select="."/>
</xsl:element>

If the source contains:

<item type="book">XML Guide</item>

the output becomes:

<book>XML Guide</book>

Therefore, <xsl:element> is particularly valuable when an XSLT transformation needs to construct an output XML or HTML structure whose element names cannot be known in advance.