<?xml version="1.0" encoding="utf-8"?>
<!--
	Copyright © 2003 Ned Martin
	http://copyright.the-i.org/

	Sort music into artists and albums then output

-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" encoding="utf-8"/>
	<xsl:key name="albums" match="song" use="@album"/>
	<!-- Top Level -->
	<xsl:template match="music">
		<ul title="Songs">
			<xsl:apply-templates select="song[generate-id()=generate-id(key('albums',@album))]" mode="albums"/>
		</ul>
	</xsl:template>
	<!-- Second level - Albums -->
	<xsl:template match="song" mode="albums">
		<li>
			<!-- album -->
			<xsl:attribute name="title"><xsl:value-of select="@genre"/><xsl:text> (</xsl:text><xsl:value-of select="@year"/><xsl:text>) </xsl:text><xsl:value-of select="@comment"/></xsl:attribute>
			<xsl:value-of select="@artist"/>
			<!-- Determine if various artists -->
			<xsl:apply-templates select="key('albums',@album)" mode="songs-artists">
				<xsl:with-param name="first-artist" select="@artist"/>
			</xsl:apply-templates>
			<xsl:text> – </xsl:text>
			<xsl:value-of select="@album"/>
			<xsl:text>  (</xsl:text>
			<xsl:value-of select="@genre"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="@year"/>
			<xsl:text>) </xsl:text>
			<xsl:text disable-output-escaping="yes">&lt;br /></xsl:text>
			<em>
				<xsl:value-of select="@comment"/>
			</em>
			<ul title="Songs">
				<xsl:apply-templates select="key('albums',@album)" mode="songs">
					<xsl:sort select="@tracknr" data-type="number"/>
					<xsl:with-param name="first-artist" select="@artist"/>
				</xsl:apply-templates>
			</ul>
		</li>
	</xsl:template>
	<!-- Test if all artists are the same -->
	<xsl:template match="song" mode="songs-artists">
		<xsl:param name="first-artist" select="@artist"/>
		<xsl:if test="$first-artist != @artist">
			<xsl:text>, </xsl:text>
			<xsl:value-of select="@artist"/>
		</xsl:if>
	</xsl:template>
	<!-- Third Level - Songs	 -->
	<xsl:template match="song" mode="songs">
		<xsl:param name="first-artist"/>
		<li>
			<xsl:attribute name="title"><xsl:text>(Track </xsl:text><xsl:value-of select="@tracknr"/><xsl:text>)</xsl:text></xsl:attribute>
			<xsl:if test="$first-artist != @artist">
				<xsl:value-of select="@artist"/>
				<xsl:text> - </xsl:text>
			</xsl:if>
			<xsl:value-of select="@title"/>
		</li>
	</xsl:template>
</xsl:stylesheet>
