<?xml version="1.0"?>
<!-- this stylesheet created by Gordon Weakliem, 28 July 2003.  This code
  is hereby released into the public domain
Modified on 19 April, 2007 to produce Atom 1.0 output instead of 0.3 -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:dt="http://www.eighty-twenty.net/amznsyndication#months"
		xmlns:et="http://www.eighty-twenty.net/amznsyndication"
	   xmlns:atom="http://www.w3.org/2005/Atom"
  extension-element-prefixes="dt"
  exclude-result-prefixes="dt" version="1.0">
  <xsl:output indent="yes"/>
  <xsl:variable name="associd" select="the8020sol-20"/>
  <xsl:variable name="dev-t" select="D37Z8BH266HXU9"/>
  <xsl:template match="/">  
    <atom:feed >
      <xsl:apply-templates /> 
    </atom:feed>
  </xsl:template>
  
  <xsl:template match="ProductInfo">    
    <xsl:apply-templates select="Request"/>   
    <atom:updated>
    <xsl:for-each select="Details[1]/ReleaseDate">
  	  <xsl:call-template name="make-ISO-8601-date"/>
  	</xsl:for-each>
    </atom:updated>
    <xsl:apply-templates select="Details"/>
  </xsl:template> 
  
  <xsl:template match="Request">    
    <atom:title>Amazon.com - <xsl:value-of select="Args/Arg[@name='bcm']/@value"/>
      <xsl:if test="Args/Arg[@name='KeywordSearch']">
        - Search Results for &quot;
        <xsl:value-of select="Request/Args/Arg[@name='KeywordSearch']/@value"/>&quot;
        in
        <xsl:value-of select="Args/Arg[@name='mode']/@value"/>        
      </xsl:if>   
    </atom:title>    
    <atom:subtitle>Up-to-date listings of Amazon.com's top
      <xsl:value-of select="Args/Arg[@name='mode']/@value"/> (by Sales Rank).
    </atom:subtitle>
    <atom:link rel="alternate" type="text/html">
    <xsl:attribute name="href">
      <xsl:choose>        
        <xsl:when test="Args/Arg[@name='BrowseNodeSearch']">http://www.amazon.com/exec/obidos/tg/browse/-/<xsl:value-of select="Args/Arg[@name='BrowseNodeSearch']/@value"/>/1/?rank=%2Bsalesrank%26dev-t=<xsl:value-of select="dev-t"/></xsl:when>       
        <xsl:when test="Args/Arg[@name='KeywordSearch']">http://www.amazon.com/exec/obidos/external-search?mode=<xsl:value-of select="Args/Arg[@name='mode']/@value"/>%26keyword=<xsl:value-of select="Args/Arg[@name='KeywordSearch']/@value"/>%26rank=%2Bsalesrank%26tag=amznRss</xsl:when>     
      </xsl:choose>
      </xsl:attribute>
    </atom:link>
    <atom:id>
      <xsl:choose>        
        <xsl:when test="Args/Arg[@name='BrowseNodeSearch']">http://www.amazon.com/exec/obidos/tg/browse/-/<xsl:value-of select="Args/Arg[@name='BrowseNodeSearch']/@value"/>/1/?rank=%2Bsalesrank%26dev-t=<xsl:value-of select="dev-t"/></xsl:when>       
        <xsl:when test="Args/Arg[@name='KeywordSearch']">http://www.amazon.com/exec/obidos/external-search?mode=<xsl:value-of select="Args/Arg[@name='mode']/@value"/>%26keyword=<xsl:value-of select="Args/Arg[@name='KeywordSearch']/@value"/>%26rank=%2Bsalesrank%26tag=amznRss</xsl:when>     
      </xsl:choose>
    </atom:id>
  </xsl:template> 
  <xsl:template match="Details">    
    <atom:entry>     
      <atom:title><xsl:value-of select="ProductName"/></atom:title>      
      <atom:summary><xsl:value-of select="ProductName"/></atom:summary>      
      <atom:author>        
        <atom:name>Amazon.com</atom:name>       
        <atom:uri>http://www.amazon.com</atom:uri>
      </atom:author>     
      <atom:link rel="alternate" href="{@url}" type="text/html"/>
      <atom:id><xsl:value-of select="@url"/></atom:id>      
      <xsl:apply-templates select="ReleaseDate"/>
      <atom:content type="xhtml" xmlns="http://www.w3.org/1999/xhtml">
	<div>
	  <img src="${ImageUrlSmall}" alt="${ProductName}" />
        <xsl:variable name="a" select="boolean(Availability='Not yet released')"/>      
        <a href="{@url}">
          <xsl:value-of select="ProductName"/>
        </a>      
        <xsl:choose>        
          <xsl:when test="$a">Will be released by </xsl:when>
          <xsl:otherwise>Released by </xsl:otherwise>
        </xsl:choose>     
        <xsl:value-of select="Manufacturer"/> on <xsl:value-of select="ReleaseDate"/>.
        -- <xsl:value-of select="OurPrice"/>
	</div>
      </atom:content>      
    <et:price currency="USD"><xsl:value-of select="substring(OurPrice,2)"/></et:price>
      <atom:link rel="enclosure" type="image/jpg" href="{ImageUrlLarge}"/>
    </atom:entry>  
  </xsl:template>

  <xsl:template match="ReleaseDate">
  	<atom:published>
  	<xsl:call-template name="make-ISO-8601-date"/>
  	</atom:published >
  	<atom:updated>
  	<xsl:call-template name="make-ISO-8601-date"/>
  	</atom:updated>
  </xsl:template>

  <!-- Amazon gives us dates like "15 July, 2002", or possibly omitting the day, we need
    to convert this to 2002-07-15T08:00:00-08:00 -->
  <!-- oddly, document() doesn't seem to work with whatever XSLT engine amazon uses, so 
    the month lookup bit doesn't work :( -->
  <xsl:template name="make-ISO-8601-date">
  	<xsl:variable name="day">
  	<xsl:choose>
	  	<xsl:when test="string(number(substring(.,1,2)))='NaN'">
            <xsl:text>01</xsl:text>
	  	</xsl:when>
	  	<xsl:otherwise>
            <xsl:value-of select="substring(.,1,2)"/>
        </xsl:otherwise>
        </xsl:choose>
  	</xsl:variable>
  	<xsl:variable name="year" select="substring-after(.,', ')"/>
  	<xsl:variable name="month">
  	<xsl:choose>
	  	<xsl:when test="not(string(number(substring(.,1,2)))='NaN')">
            <xsl:value-of select="substring-before(substring-after(.,concat($day,' ')),',')"/>
	  	</xsl:when>
	  	<xsl:otherwise>
            <xsl:value-of select="substring-before(.,',')"/>	  	
        </xsl:otherwise>
        </xsl:choose>
  	</xsl:variable>
  	<xsl:variable name="monthnum" select="document('')/*/dt:month[@name=$month]/@number"/>
  	<xsl:value-of select="concat($year,'-',$monthnum,'-',$day,'T08:00:00-0800')"/>
  </xsl:template>
  
  <dt:month name="January" number="01"/>
  <dt:month name="February" number="02"/>
  <dt:month name="March" number="03"/>
  <dt:month name="April" number="04"/>
  <dt:month name="May" number="05"/>
  <dt:month name="June" number="06"/>
  <dt:month name="July" number="07"/>
  <dt:month name="August" number="08"/>
  <dt:month name="September" number="09"/>
  <dt:month name="October" number="10"/>
  <dt:month name="November" number="11"/>
  <dt:month name="December" number="12"/>
</xsl:stylesheet>