xsl:attribute-set

  1. attribute-set_1-input.xml

  2. <?xml version="1.0"?>
  3. <images>
  4.   <image href="mermaid.png"/>
  5.   <image href="tivoli.jpg"/>
  6.   <image href="copenhagen.png"/>
  7.   <image href="airport.png"/>
  8. </images>
  1. attribute-set_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4.   <xsl:output indent="yes"/>
  5.   <xsl:attribute-set name="img-std_attributes">
  6.     <xsl:attribute name="width">300px</xsl:attribute>
  7.     <xsl:attribute name="length">200px</xsl:attribute>
  8.     <xsl:attribute name="alt"/>
  9.   </xsl:attribute-set>
  10.   <xsl:template match="/">
  11.     <images>
  12.       <xsl:for-each select="images/image">
  13.         <img src="{@href}" xsl:use-attribute-sets="img-std_attributes"/>
  14.       </xsl:for-each>
  15.     </images>
  16.   </xsl:template>
  17. </xsl:stylesheet>
  1. attribute-set_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <images>
  4.   <img width="300px" length="200px" alt="" src="mermaid.png"/>
  5.   <img width="300px" length="200px" alt="" src="tivoli.jpg"/>
  6.   <img width="300px" length="200px" alt="" src="copenhagen.png"/>
  7.   <img width="300px" length="200px" alt="" src="airport.png"/>
  8. </images>

Comment

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