<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"
	    doctype-public="-//W3C//DTD XHTML 1.1//EN"
	    doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
	    indent="yes"
	    />

<xsl:template match="/">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>Bob Ham's reading list</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
    <h1>Bob Ham's reading list</h1>
    <p>This is a list of books I would like to possess (in order of preference).  I prefer second-hand books to new.</p>
    
    <ul style="list-style-type: none;">
      <xsl:for-each select="reading-list/book">
	<xsl:sort select="priority" order="descending" />
	<xsl:if test="not(receipt)">
	  <li>
	    <xsl:element name="div">
	      <xsl:attribute name="style">
		<xsl:variable name="gb" select="255 - round( (number(priority) div 100) * 255 )" />
		<xsl:text>background-color: rgb(255,</xsl:text>
		<xsl:value-of select="$gb" />
		<xsl:text>,</xsl:text>
		<xsl:value-of select="$gb" />
		<xsl:text>);</xsl:text>
		<xsl:text>width: 1em;</xsl:text>
		<xsl:text>float: left;</xsl:text>
<!--		<xsl:text>height: 1.0em;</xsl:text> -->
	      </xsl:attribute>
	      <xsl:text disable-output-escaping="yes"> </xsl:text>
	    </xsl:element>

	    <xsl:call-template name="book-info" />

	    <xsl:if test="isbn">
	      <xsl:text> ISBN: </xsl:text>
	      <xsl:value-of select="isbn" />
	    </xsl:if>
	  </li>
	</xsl:if>
      </xsl:for-each>
    </ul>

    <p>Many thanks for</p>
    <ul>
      <xsl:for-each select="reading-list/book">
	<xsl:if test="receipt/giver">
	  <li>
	    <span style="text-decoration: line-through;">
	      <xsl:call-template name="book-info" />
	    </span>
	    
	    <xsl:text> given by </xsl:text>
	    <xsl:value-of select="receipt/giver" />
	    <xsl:text> on </xsl:text>
	    <xsl:value-of select="receipt/date/day" />
	    <xsl:text>/</xsl:text>
	    <xsl:value-of select="receipt/date/month" />
	    <xsl:text>/</xsl:text>
	    <xsl:value-of select="receipt/date/year" />
	  </li>
	</xsl:if>
      </xsl:for-each>
    </ul>

    <p>Other books I now possess</p>
    <ul>
      <xsl:for-each select="reading-list/book">
	<xsl:if test="receipt/date and not(receipt/giver)">
	  <li>
	    <span style="text-decoration: line-through;">
	      <xsl:call-template name="book-info" />
	    </span>
	    
	    <xsl:text> received on </xsl:text>
	    <xsl:value-of select="receipt/date/day" />
	    <xsl:text>/</xsl:text>
	    <xsl:value-of select="receipt/date/month" />
	    <xsl:text>/</xsl:text>
	    <xsl:value-of select="receipt/date/year" />
	  </li>
	</xsl:if>
      </xsl:for-each>
    </ul>

    <p style="margin-top: 4em; text-align: center;">
      <img
	 src="http://www.w3.org/Icons/valid-xhtml11"
	 alt="Valid XHTML 1.1" height="31" width="88"
	 style="border: none;"/>
    </p>
  
  </body>
  
</html>
</xsl:template>

<xsl:template name="book-info">
  
  <a xmlns="http://www.w3.org/1999/xhtml">
    <xsl:if test="url or receipt/url">
      <xsl:attribute name="href">
	<xsl:if test="receipt/url">
	  <xsl:value-of select="receipt/url" />
	</xsl:if>
	<xsl:if test="not(receipt/url)">
	  <xsl:value-of select="url" />
	</xsl:if>
      </xsl:attribute>
    </xsl:if>
    
    <xsl:value-of select="author" />
    <xsl:text> - </xsl:text>
    <xsl:value-of select="title" />
    <xsl:if test="subtitle">
      <xsl:text>: </xsl:text>
      <span style="font-size: 0.7em;">
	<xsl:value-of select="subtitle" />
      </span>
    </xsl:if>
  </a>
</xsl:template>

</xsl:stylesheet>

