<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>

<xsl:template match="/">

	<rss version="0.91"
      xmlns:az="http://www.amazon.com/gp/aws/landing.html">
	<channel>
		<title> 
			<xsl:for-each select="//ProductInfo/Request/Args/Arg">
				<xsl:if test="@name[.='bcm']">
					Amazon.com - <xsl:value-of select="@value"/>
				</xsl:if>
				<xsl:if test="@name[.='KeywordSearch']">
					- Search Results for "<xsl:value-of select="@value"/>" in <xsl:for-each select="//ProductInfo/Request/Args/Arg"><xsl:if test="@name[.='mode']"><xsl:value-of select="@value"/></xsl:if></xsl:for-each>
				</xsl:if>				
			</xsl:for-each>
		</title> 
		<link>
			<xsl:for-each select="//ProductInfo/Request/Args/Arg">
				<xsl:if test="@name[.='BrowseNodeSearch']">
					http://www.amazon.com/exec/obidos/tg/browse/-/<xsl:for-each select="//ProductInfo/Request/Args/Arg"><xsl:if test="@name[.='BrowseNodeSearch']"><xsl:value-of select="@value"/></xsl:if></xsl:for-each>/1/?rank=%2Bsalesrank%26dev-t=amznRss
				</xsl:if>
				<xsl:if test="@name[.='KeywordSearch']">
					http://www.amazon.com/exec/obidos/external-search?mode=<xsl:for-each select="//ProductInfo/Request/Args/Arg"><xsl:if test="@name[.='mode']"><xsl:value-of select="@value"/></xsl:if></xsl:for-each>%26keyword=<xsl:for-each select="//ProductInfo/Request/Args/Arg"><xsl:if test="@name[.='KeywordSearch']"><xsl:value-of select="@value"/></xsl:if></xsl:for-each>%26rank=%2Bsalesrank%26tag=amznRss
				</xsl:if>				
			</xsl:for-each>		
		</link> 
			<description>Up-to-date listings of Amazon.com's top items (by Sales Rank).</description> 
			<language>en-us</language> 

			<docs>http://www.amazon.com/exec/obidos/subst/xs/syndicate.html</docs>
			<image>
				<title>Amazon.com--Earth's Biggest Selection</title>
				<url>http://images.amazon.com/images/G/01/rcm/logo2.gif</url>
				<width>120</width>
				<height>30</height>
				<link>http://www.amazon.com</link>
			</image>			
		  
		  	<xsl:apply-templates select="ProductInfo/Details">
				<xsl:sort select="SalesRank"/>
				</xsl:apply-templates>
			
	</channel>
	</rss>	
</xsl:template>
  
<xsl:template match="Details">
	<item>  	
	<title><xsl:value-of select="ProductName"/></title>
	<description>
		<xsl:value-of select="ProductName"/><xsl:apply-templates select="Authors"/><xsl:apply-templates select="Artists"/> -- <xsl:value-of select="OurPrice"/>
	</description>
	<link>http://www.amazon.com/exec/obidos/ASIN/<xsl:value-of select="Asin"/>/ref=nosimRss/<xsl:for-each select="//ProductInfo/Request/Args/Arg"><xsl:if test="@name[.='t']"><xsl:value-of select="@value"/></xsl:if></xsl:for-each>?dev-t=<xsl:for-each select="//ProductInfo/Request/Args/Arg"><xsl:if test="@name[.='dev-t']"><xsl:value-of select="@value"/></xsl:if></xsl:for-each></link>

    <xsl:apply-templates select="." mode="copy-details" />

  </item>
</xsl:template>

<xsl:variable name="azns" select="'http://www.amazon.com/gp/aws/landing.html'" />

<xsl:template match="Details" mode="copy-details">
  <xsl:element name="{local-name()}" namespace="{$azns}">
    <xsl:attribute name="url"><xsl:value-of select="./@url" /></xsl:attribute>
    <xsl:apply-templates select="node()" mode="copy-details" />
  </xsl:element>
</xsl:template>

<xsl:template match="*" mode="copy-details">
  <xsl:element name="{local-name()}" namespace="{$azns}">
    <xsl:apply-templates select="node()" mode="copy-details" />
  </xsl:element>
</xsl:template>

  
<xsl:template match="Authors">
	<xsl:choose>
		<xsl:when test="count(Author)&gt; 2"><xsl:text>, by </xsl:text>
			<xsl:for-each select="Author">
				<xsl:choose>
					<xsl:when test="not(position() = last())">
						<xsl:value-of select="."/><xsl:text>, </xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>and </xsl:text><xsl:value-of select="."/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:when>
		<xsl:when test="count(Author) = 2"><xsl:text>, by </xsl:text>
			<xsl:value-of select="Author[1]"/><xsl:text> and </xsl:text><xsl:value-of select="Author[2]"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>, by </xsl:text><xsl:value-of select="Author"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="Artists">
	<xsl:choose>
		<xsl:when test="count(Artist)&gt; 2"><xsl:text>, by </xsl:text>
			<xsl:for-each select="Artist">
				<xsl:choose>
					<xsl:when test="not(position() = last())">
						<xsl:value-of select="."/><xsl:text>, </xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>and </xsl:text><xsl:value-of select="."/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:when>
		<xsl:when test="count(Artist) = 2"><xsl:text>, by </xsl:text>
			<xsl:value-of select="Artist[1]"/><xsl:text> and </xsl:text><xsl:value-of select="Artist[2]"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>, by </xsl:text><xsl:value-of select="Artist"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
  
</xsl:stylesheet>
