<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt">

<xsl:template match="/">
   <cd_catalog>
   <xsl:apply-templates />
   <style type="text/css">
   body {
      font-family: trebuchet ms;
      font-size: 10pt;
   }
   h1 {
      font-family: trebuchet ms;
      font-size: 18pt;
   }
   h2 {
      font-family: trebuchet ms;
      font-size: 14pt;
   }
   em {
      font-family: trebuchet ms;
      font-size: 12pt;
      font-style: italic;
   }
   th {
      font-family: trebuchet ms;
      font-size: 12pt;
      font-style: italic;
      text-align: left;
   }      
   td.album {
      background-color: #b8860b;
   }
   td.songs (
      background-color: #f0fff0;
   }
   </style>
   </cd_catalog>
</xsl:template>
<xsl:template match="cd">
   <cd>
   <table width="100%" cellpadding="12" border="3" rules="basic">
   <tr>
   <td class="album">
   <title_text>
      <em>
      <![CDATA[CD Catalog No.]]>
      <xsl:value-of select="@cat_no" /><br />
      </em>
   </title_text>
   </td>
   <td class="album" colspan="4">
      <cover_img>
      <img src="{cover_img}" />
      </cover_img>
   </td>
   </tr>
   <tr>
   <th>Artist</th>
   <th>Album</th>
   <th>Track No.</th>
   <th>Song Title</th>
   <th>Length mm:ss</th>
   </tr>
   <xsl:apply-templates select="track" />
   <tr>
   <td class="album" colspan="2">
   <![CDATA[Total tracks]]>
   </td>
   <td class="album">
   <xsl:value-of select="count(track)" />
   </td>
   <td class="album">
   <![CDATA[Total playing time]]>
   </td>
   <td class="album">
   <xsl:call-template name="CalculateLength" />
   </td>
   </tr>
   </table>
   </cd>
</xsl:template>
<xsl:template match="track">
   <xsl:for-each select=".">
   <tr>
      <td class="songs">
         <artist>
            <xsl:value-of select="../artist" />
         </artist>
      </td>
      <td class="songs">
         <album>
            <xsl:value-of select="../title" />
         </album>
      </td>
      <td class="songs">
         <track>
            <em>
            <xsl:value-of select="@trk_no" />
            </em>
         </track>
      </td>
      <td class="songs">
         <song_title>
            <xsl:value-of select="song_title" />
         </song_title>
      </td>
      <td class="songs">
         <length>
            <xsl:value-of select="length" />
         </length>
      </td>
   </tr>
   </xsl:for-each>
</xsl:template>

<xsl:template name="CalculateLength">
<!-- with help from http://sources.redhat.com/ml/xsl-list/2001-08/msg01320.html and
     msdn.microsoft.com -->

<xsl:variable name="minutesnode">
<xsl:for-each select="track">
   <minutes>
      <xsl:value-of select="substring-before(length,':')" />
   </minutes>
</xsl:for-each>
</xsl:variable>

<xsl:variable name="secondsnode">
<xsl:for-each select="track">
   <seconds>
      <xsl:value-of select="substring-after(length,':')" />
   </seconds>
</xsl:for-each>
</xsl:variable>

<xsl:variable name="totalseconds">
   <xsl:value-of select="(sum(msxsl:node-set ($minutesnode)/minutes)*60)+sum(msxsl:node-set ($secondsnode)/seconds)" />
</xsl:variable>

<xsl:value-of select="format-number($totalseconds div 60, '#')" />:<xsl:value-of select="format-number($totalseconds mod 60, '00')" />

</xsl:template>
</xsl:stylesheet>
