November 2003 Technical Tip Example - XSLT Transformation Program

The following is the full text of the XSLT transformation program used to generate the COBOL Curriculum table in the November 2003 Technical Tip example:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                              xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xsl:output method="html" encoding="UTF-8" media-type="text/html" />
   
   <xsl:template match="/">
      <html>
         <head>
            <title>Table Tricks With XML</title>
         </head>
         <body>
            <table cellPadding="2" border="1">
               <caption>COBOL Curriculum</caption>
               <tbody>
                  <tr>
                     <th>Course</th>
                     <th>Description</th>
                     <th>Days</th>
                  </tr>
                  <colgroup>
                     <col align="middle" width="100" />
                     <col align="left" width="250" />
                     <col align="middle" width="75" />
                  </colgroup>
                  <xsl:for-each select="/courses/course">
                     <xsl:choose>
                        <xsl:when test="position() mod 2 != 0">
                           <tr bgcolor="#CCCCCC">
                              <xsl:apply-templates />
                           </tr>
                        </xsl:when>
                        <xsl:otherwise>
                           <tr bgcolor="#FFFFFF">
                              <xsl:apply-templates />
                           </tr>
                        </xsl:otherwise>
                     </xsl:choose>
                  </xsl:for-each>
               </tbody>
            </table>
         </body>
      </html>
   </xsl:template>
   
   <xsl:template match="courseno">
      <td>
         <xsl:value-of select="." />
      </td>
   </xsl:template>
   
   <xsl:template match="coursename">
      <td>
         <xsl:value-of select="." />
      </td>
   </xsl:template>
   
   <xsl:template match="duration">
      <td>
         <xsl:value-of select="." />
      </td>
   </xsl:template>
   
</xsl:stylesheet>

Go to the articles index. Copyright 2003 by Caliber Data Training 800.938.1222