The <xsl:attribute> element allows you to dynamically or conditionally add attributes.
<xsl:attribute
name = { qname }
namespace = { uri-reference }>
<!-- Content: template -->
</xsl:attribute>
An example of <xsl:attribute> being used conditionally:
<xsl:template name="graphic">
<img src="{@fileref}">
<xsl:if test="@align">
<xsl:attribute name="align">
<xsl:value-of select="@align"/>
</xsl:attribute>
</xsl:if>
</img>
</xsl:template>
Note that the <img> tag has logical content in the stylesheet. The serializer is expected to render this appropriately for HTML or XML.