<?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">

    <!-- part of URL for a Google search -->
    <xsl:param name="GoogleStart">http://www.google.com/search?q=</xsl:param>
        
    <!-- 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:key name="catalog" match="cat:wine" use="@code"/>

    <!-- matches the root node -->
    <xsl:template match="/">
        <html>
            <head>
                <title>Cellar of <xsl:value-of 
                    select="cellar-book/owner/name"/>
                </title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    
    <!-- output of the content of the cellar: 
         addresses of the owner and cellar 
         followed by a table of wines -->
    <xsl:template match="cellar-book">
        <h1>Cellar of <xsl:value-of select="owner/name"/></h1>
        <table border="1">
            <tr>
                <th>Personal address</th>
                <th>Cellar address</th>
            </tr>
            <tr><td><xsl:for-each select="owner/*[position()>1]">
                        <xsl:apply-templates/><br/>
                    </xsl:for-each>
                </td>
                <td><xsl:for-each select="location/*">
                        <xsl:apply-templates/><br/>
                    </xsl:for-each>                   
                </td>
            </tr>
        </table>
        <p/>
        <xsl:apply-templates select="cellar"/>
    </xsl:template>
    
    <!-- content of the cellar as a table  -->
    <xsl:template match="cellar">
        <table border="1">
            <tr><!-- head of the table -->
                <xsl:for-each 
                select="'Code','Name','Purchase Date','Rating','Nb bottles'">
                    <th><xsl:value-of select="."/></th>
                </xsl:for-each>
            </tr>
            <xsl:apply-templates select="wine"><!-- each row -->
                <xsl:sort select="@code" order="ascending"/>
            </xsl:apply-templates>
            <tr>
                <td colspan="3">Estimated value</td>
                <td align="right">
                <!-- compute the estimated value of the cellar  -->
                    <xsl:value-of select=" 
                        sum (for $w in wine return  
                       $w/quantity * key('catalog',$w/@code)/cat:price)"/>
                </td>
                <td align="right">
                    <!-- compute the total number of bottles -->
                    <xsl:value-of select="sum(wine/quantity)"/>
                </td>
            </tr>
        </table>
        <!-- put comment section if at least one comment appears -->
        <xsl:if test="count(wine/comment)>0">
            <h3>Comments</h3>
            <p>            
                <xsl:apply-templates select="wine/comment">
                    <xsl:sort select="../@code" order="ascending"/>
                </xsl:apply-templates>
            </p>
        </xsl:if>
    </xsl:template>

    <!-- information about a wine -->
    <xsl:template match="wine">
        <tr>
            <td><xsl:value-of select="substring(@code,2)"/></td>
            <td>
                <xsl:call-template name="nameAndUrl">
                    <xsl:with-param name="wine" select="key('catalog',@code)"/>
                </xsl:call-template>
            </td>
            <td align="right"><xsl:value-of select="purchaseDate"/></td>
            <td align="center">
              <xsl:value-of select="substring('*****',1,
                  if (rating/@stars) then rating/@stars else 0)"/></td>
            <td align="right"><xsl:value-of select="quantity"/></td>
        </tr>
    </xsl:template>

    
    <!-- output the name of the wine with a link for "googling" it  -->
    <xsl:template name="nameAndUrl">
        <xsl:param name="wine"/>
        <xsl:variable name="name" select="encode-for-uri($wine/@name)"/>
        <!-- dynamic creation of an element and its attributes -->
        <a>
            <xsl:attribute name="href">
                <xsl:value-of select="$GoogleStart"/>
                <xsl:value-of select="$name"/>
            </xsl:attribute>
            <xsl:value-of select="$wine/@name"/>
        </a>
        <!-- link creation using an Attribute Value Template -->
        <br/>
        <i>
            <a href="{$GoogleStart}{$name}">
                <xsl:value-of select="$wine/@name"/>
            </a>
        </i>
    </xsl:template>
    
    <!-- comment preceded by the code corresponding to it -->
    <xsl:template match="comment"><xsl:text>
        </xsl:text>
        <xsl:value-of select="../@code"/>
        <xsl:text> : </xsl:text>
        <xsl:apply-templates/><br/>
    </xsl:template>
    
    <!-- global change of <bold> tags to html <b> tags -->
    <xsl:template match="cat:bold">
        <b><xsl:apply-templates/></b>
    </xsl:template>
    
</xsl:stylesheet>