attribute-set_1-input.xml
<?xml version="1.0"?>
<images>
<image
href="mermaid.png"/>
<image
href="tivoli.jpg"/>
<image
href="copenhagen.png"/>
<image
href="airport.png"/>
</images>
attribute-set_1-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
indent="yes"/>
<xsl:attribute-set
name="img-std_attributes">
<xsl:attribute
name="width">300px</xsl:attribute>
<xsl:attribute
name="length">200px</xsl:attribute>
<xsl:attribute
name="alt"/>
</xsl:attribute-set>
<xsl:template
match="/">
<images>
<xsl:for-each
select="images/image">
<img
src="{@href}" xsl:use-attribute-sets="img-std_attributes"/>
</xsl:for-each>
</images>
</xsl:template>
</xsl:stylesheet>
attribute-set_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<images>
<img
width="300px" length="200px" alt="" src="mermaid.png"/>
<img
width="300px" length="200px" alt="" src="tivoli.jpg"/>
<img
width="300px" length="200px" alt="" src="copenhagen.png"/>
<img
width="300px" length="200px" alt="" src="airport.png"/>
</images>
xsl:attribute-set works hand in hand with the attribute "use-attribute-sets" (a space separated list if we need more than one attribute-set. In the above example xsl:attribute-set is not of much use, but if we have more templates also creating "img" elements in need of the same standard attributes, xsl:attribute-set could come in handy.
Updated 2009-03-19