<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:cat="http://www.iro.umontreal.ca/lapalme/wine-catalog"
    xmlns="http://www.w3.org/1999/xhtml"
    version="2.0">
    <xsl:param name="color" select="'red'"/>
    <!-- to produce legal and validable XHTML ... -->
    <xsl:output method = "xml" 
        doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN"
        doctype-system =
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
        indent = "yes" encoding = "UTF-8"/>
    
    <xsl:template match="/">
        <html>
            <head><title>Wine Catalog</title></head>
            <body>
                <xsl:apply-templates/>
            </body>   
        </html>
    </xsl:template>
    
    <xsl:template match="cat:wine-catalog">
        <h1>
            Wine Catalog (<xsl:value-of select="$color"/> only)
        </h1>
        <table border="1">
            <tr>
                <xsl:for-each 
                    select="'Wine Name','Code','Color','Year','Price','ml','l'">
                    <th><xsl:value-of select="."/></th>
                </xsl:for-each>
            </tr>
            <xsl:apply-templates 
                select="cat:wine[cat:properties/cat:color=$color]"/>
        </table>        
    </xsl:template>
        
    <xsl:template match="cat:wine">
        <tr>
            <td><xsl:value-of select="@name"/></td>
            <td><xsl:value-of select="@code"/></td>
            <td><xsl:value-of select="cat:properties/cat:color"/></td>
            <td align="right"><xsl:value-of select="cat:year"/></td>
            <td align="right">
                <xsl:value-of select="format-number(cat:price,'$0.00')"/>
            </td>
            <td align="right">
                <xsl:value-of select="cat:toML(@format)"/>
            </td>
            <td  align="right">
                <xsl:call-template name="toL">
                    <xsl:with-param name="fmt" select="@format"/>
                </xsl:call-template>
            </td>
        </tr>
    </xsl:template>
    
    <xsl:function name="cat:toML">
        <xsl:param name="fmt"/>
        <xsl:value-of select="
            if ($fmt='375ml') then '375'
            else if ($fmt='750ml') then '750'
            else if ($fmt='1l') then '1000'
            else if ($fmt='magnum') then '1500'
            else 'big'"/>
    </xsl:function>
    
    <xsl:template name="toL">
        <xsl:param name="fmt"/>
        <xsl:choose>
            <xsl:when test="$fmt='375ml'">0.375</xsl:when>
            <xsl:when test="$fmt='750ml'">0.75</xsl:when>
            <xsl:when test="$fmt='1l'">1.0</xsl:when>
            <xsl:when test="$fmt='magnum'">1.5</xsl:when>
            <xsl:otherwise>big</xsl:otherwise>           
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>