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

XML Tutorial
1. Introduction
2. Namespace
3. XML Schema
4. XPath
5. XSLT stylesheet
Java
XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML Tutorial » XSLT stylesheet » attribute set 
5. 37. 1. table with different attribute sets
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
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.