Skip to content

xsl:attribute

Creates an attribute on the nearest enclosing result element.

<xsl:attribute name="class" select="'highlight'"/>

Useful when an attribute name or value depends on runtime values, or when an attribute should only be added conditionally. The instruction must run before any non-attribute content is written to the parent element.

The value can be supplied via the select attribute or via a sequence constructor body. The name and namespace attributes are attribute value templates.

Attributes

Attribute Description
name Attribute name as an AVT. Required.
namespace Namespace URI as an AVT.
select XPath expression for the attribute value.
separator Separator when the value is a multi-item sequence.

Examples

Conditional attribute:

<a>
  <xsl:if test="@external">
    <xsl:attribute name="target">_blank</xsl:attribute>
  </xsl:if>
  <xsl:value-of select="."/>
</a>

Computed name and value:

<element>
  <xsl:attribute name="data-{@key}" select="@value"/>
</element>