<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <HTML>
      <HEAD>
        <TITLE>Bookstore Inventory</TITLE>
      </HEAD>
      <BODY BGCOLOR="#AAAAAA">
        <H1>Bookstore Inventory</H1>
     <H2>Books Sorted by Different Criteria</H2>
      <TABLE>  
     <TR BGCOLOR="#FFFFFF"><TD WIDTH="100"></TD><TD>AUTHOR</TD><TD>TITLE</TD><TD>PRICE</TD></TR>
     <TR><TD COLSPAN="4" BGCOLOR="#EE0000"><B>All Books Sorted by Ascending Price</B></TD></TR>
     <xsl:apply-templates select="//book">
          <xsl:sort select="price" data-type="number"/>
     </xsl:apply-templates>
     <TR><TD COLSPAN="4" BGCOLOR="#EE0000"><B>All Books Sorted by Titles</B></TD></TR>
     <xsl:apply-templates select="//book">
          <xsl:sort select="title" data-type="text"/>
     </xsl:apply-templates>
     <TR><TD COLSPAN="4" BGCOLOR="#EE0000"><B>All Books Sorted by Authors</B></TD></TR>
     <xsl:apply-templates select="//book">
          <xsl:sort select="author/name|author/last-name" data-type="text"/>
          <xsl:sort select="author/first-name" data-type="text"/>
     </xsl:apply-templates>   
     <TR><TD COLSPAN="4" BGCOLOR="#EE0000"><B>Genres sorted alphabetically</B></TD></TR>  
     <TR BGCOLOR="#FFFFFF"><TD>GENRE</TD><TD COLSPAN="3">BOOKS</TD></TR>
     <xsl:apply-templates select="//book[not(@genre=preceding-sibling::book/@genre)]/@genre">
          <xsl:sort select="." data-type="text"/>
     </xsl:apply-templates>
      </TABLE>
          <H2>Summary</H2>
     <TABLE>
          <TR>
               <TD BGCOLOR="#FFFFFF">Total Number of Books in the Store</TD>
               <TD BGCOlOR="#AAAA00" ALIGN="RIGHT"><xsl:number value="count(//book)" format="1"/></TD></TR>
          <TR>
               <TD BGCOLOR="#FFFFFF">Total Value of the Books in the Store</TD>
               <TD BGCOLOR="#AAAA00" ALIGN="RIGHT">
                    <xsl:choose>
                         <xsl:when test="count(//*[@currency!=//@currency[1]])>0">
                              Error! This function supports only one currency!
                         </xsl:when>
                         <xsl:otherwise>
                              <xsl:value-of select="sum(//book/price)"/>
                              <xsl:text> </xsl:text>
                              <xsl:value-of select="//@currency[1]"/>
                         </xsl:otherwise>
                    </xsl:choose>
               </TD>
          </TR>
     </TABLE>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="book">
    <TR>
      <TD BGCOlOR="#EEEEEE" ALIGN="RIGHT">[<xsl:number/>]</TD>
      <TD BGCOLOR="#DDDDDD"><xsl:apply-templates select="author"/></TD>
      <TD BGCOlOR="#CCCCCC"><B><xsl:value-of select="title"/></B></TD>
      <TD BGCOLOR="#AAAAAA" ALIGN="RIGHT"><xsl:apply-templates select="price"/></TD>
    </TR>
  </xsl:template>


  <xsl:template match="author">
     <xsl:choose>
          <xsl:when test="boolean(name)">
               <I><xsl:value-of select="name"/></I>
          </xsl:when>
          <xsl:otherwise>
               <I><xsl:value-of select="last-name"/>, <xsl:value-of select="first-name"/></I>
          </xsl:otherwise>
     </xsl:choose>
  </xsl:template>  


  <xsl:template match="price">
  <B><xsl:value-of select="."/><xsl:text> </xsl:text><xsl:value-of select="@currency"/></B>
  </xsl:template>  

  <xsl:template match="@genre">
      <TR>
      <TD BGCOlOR="#EEEEEE"><xsl:value-of select="."/></TD>
      <TD BGCOLOR="#DDDDDD" COLSPAN="3">
               <xsl:variable name="genre"><xsl:value-of select="."/></xsl:variable>
               <xsl:for-each select="//book[@genre=$genre]">
               [<xsl:number/>]
               <xsl:apply-templates select="author"/>:
               <xsl:value-of select="title"/>
               <xsl:choose>
                    <xsl:when test="not(position()=last())">
                         <xsl:text>, </xsl:text>
                    </xsl:when>
               </xsl:choose>
          </xsl:for-each>
      </TD>
      </TR>    
  </xsl:template>

</xsl:stylesheet>
