<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output
  method="html"
  doctype-public="-//W3C//DTD XHTML 1.1//EN"
  doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
  version="1.0"
  encoding="utf-8"
  indent="yes"
 />
 
 <xsl:template match="/">
  <xsl:variable name="tracks" select="plist/dict/dict/dict" />
  <html lang="en" xml:lang="en">
   <head>
    <title>iTunes tracks</title>
    <link rel="stylesheet" rev="stylesheet" href="http://www.faqintosh.com/xsl/ituneslibrary.css" type="text/css" charset="utf-8" />
    <link rel="SHORTCUT ICON" href="http://www.faqintosh.com/favicon.ico" />
   </head>
   <body>
    <h1>
     <xsl:text>Total tracks: </xsl:text>
     <xsl:value-of select="count($tracks)" />
    </h1>
    <div class="trackheaders">
     <div class="name">Track name</div>
     <div class="album">Album</div>
     <div class="artist">Artist</div>
     <div class="size">MB</div>
    </div>
    <xsl:apply-templates select="$tracks">
     <xsl:sort select="string[preceding-sibling::key[1]='Name']" data-type="text" order="ascending" />
    </xsl:apply-templates>
    <div id="footer">
     <a href="http://www.faqintosh.com/xsl/ituneslibrary.xsl">XSLT transformation stylesheet</a>
     <xsl:text> by Marco Balestra</xsl:text>
    </div>
   </body>
  </html>
 </xsl:template>
 
 <xsl:template match="dict">
  <div>
   <xsl:attribute name="class">
    <xsl:text>track</xsl:text>
    <xsl:if test="(position() mod 2) = 0">
     <xsl:text> dark</xsl:text>
    </xsl:if>
   </xsl:attribute>
   <div class="name">
    <xsl:apply-templates select="key[text()='Name']" mode="getValue" />
    <xsl:text>&#160;</xsl:text>
   </div>
   <div class="album">
    <xsl:apply-templates select="key[text()='Album']" mode="getValue" />
    <xsl:text>&#160;</xsl:text>
   </div>
   <div class="artist">
    <xsl:apply-templates select="key[text()='Artist']" mode="getValue" />
    <xsl:text>&#160;</xsl:text>
   </div>
   <div class="size">
    <xsl:variable name="size">
     <xsl:apply-templates select="key[text()='Size']" mode="getValue" />
    </xsl:variable>
    <xsl:value-of select="round($size div 10240) div 100" />
   </div>
  </div>
 </xsl:template>
 
 <xsl:template match="key" mode="getValue">
  <xsl:value-of select="following-sibling::*[1]" />
 </xsl:template>
 

</xsl:stylesheet>