table with different attribute sets : attribute set « XSLT stylesheet « XML Tutorial






File: Data.xml
<?xml version="1.0"?>
<!-- albums.xml -->
<list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>


File: Transform.xslt

<?xml version="1.0"?>
<!-- attribute-set.xsl -->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html"/>

  <xsl:attribute-set name="bold-table">
    <xsl:attribute name="style">
      font-weight: bold; 
    </xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="spacious-table" 
    use-attribute-sets="bold-table">
    <xsl:attribute name="cellpadding">8</xsl:attribute>
    <xsl:attribute name="cellspacing">8</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="reverse-table"> 
    <xsl:attribute name="bgcolor">black</xsl:attribute>
    <xsl:attribute name="style">color: white;</xsl:attribute>
  </xsl:attribute-set>

  <xsl:template match="/">
    <html>
      <head>
        <title><xsl:value-of select="/list/title"/></title>
      </head>
      <body style="font-family: sans-serif;">
        <xsl:apply-templates select="*"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="list">
    <h1><xsl:value-of select="title"/></h1>
    <table xsl:use-attribute-sets="spacious-table" border="2">
      <xsl:for-each select="listitem">
        <tr>
          <td xsl:use-attribute-sets="reverse-table">
            <xsl:value-of select="."/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
    <h1>Here's the same table with different attribute sets:</h1>
    <table border="2" xsl:use-attribute-sets="bold-table">
      <xsl:for-each select="listitem">
        <tr>
          <td>
            <xsl:value-of select="."/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

</xsl:stylesheet>

Output:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>title 1</title>
   </head>
   <body style="font-family: sans-serif;">
      <h1>title 1</h1>
      <table style="&#xA;      font-weight: bold; &#xA;    " cellpadding="8" cellspacing="8" border="2">
         <tr>
            <td bgcolor="black" style="color: white;">item 1</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 2</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 3</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 4</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 5</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 6</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 7</td>
         </tr>
      </table>
      <h1>Here's the same table with different attribute sets:</h1>
      <table style="&#xA;      font-weight: bold; &#xA;    " border="2">
         <tr>
            <td>item 1</td>
         </tr>
         <tr>
            <td>item 2</td>
         </tr>
         <tr>
            <td>item 3</td>
         </tr>
         <tr>
            <td>item 4</td>
         </tr>
         <tr>
            <td>item 5</td>
         </tr>
         <tr>
            <td>item 6</td>
         </tr>
         <tr>
            <td>item 7</td>
         </tr>
      </table>
   </body>
</html>








5.37.attribute set
5.37.1.table with different attribute sets
5.37.2.Several attributes can be grouped together and then used at once with xsl:attribute-set and xsl:use-attribute-sets