Skip to content

XSLT <xsl:attribute>

How to use the XSLT <xsl:attribute> element to dynamically create or replace attributes on XML elements, including syntax, required attributes, and practical examples.

XSLT <xsl:attribute>

The <xsl:attribute> element is used to add attributes to elements.

Syntax


<xsl:attribute name="attributename" namespace="uri">

    <!-- Content:template -->

</xsl:attribute>

Attributes

Attribute Value Required/Optional Description
name attributename Required Specifies the name of the attribute
namespace URI Optional Defines the namespace URI for the attribute

Example

Let's see some examples:

Example 1

Add a source attribute to the picture element:

example1.xslxml

<picture>
    <xsl:attribute name="source"/>
</picture>

Example 2

Add a source attribute to the picture element and fill it with values from "images/name" :

example2.xslxml

<picture>
    <xsl:attribute name="source">
        <xsl:value-of select="images/name"/>
    </xsl:attribute>
</picture>

Example 3

Create an attribute-set that can be applied to any output element:

example3.xslxml

<xsl:attribute-set name="font">
    <xsl:attribute name="fname">Arial</xsl:attribute>
    <xsl:attribute name="size">15px</xsl:attribute>
    <xsl:attribute name="color">blue</xsl:attribute>
</xsl:attribute-set>
Navigation