<?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">
	<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"/>
        <xsl:call-template name="search-allconsuming">
            <xsl:with-param name="asin" select="Asin" />
        </xsl:call-template>
	</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>
	</item>
</xsl:template>

<xsl:template name="search-allconsuming">
    <xsl:param name="asin"/>
    <xsl:variable name="allc-weblogs" select="document(concat('http://allconsuming.net/soap-client.cgi?weblog_mentions_for_book=1&amp;isbn=', $asin, '&amp;days_back=60&amp;format=xml'))"/>
    <!-- TODO: make it perform better by computing the count when generating the rest of the list -->
    &lt;br/&gt;According to &lt;a href=&quot;http://allconsuming.net/&quot;&gt;All Consuming&lt;/a&gt; there are <xsl:value-of select="count($allc-weblogs/opt/weblogs)"/> mentions of this book in weblogs
    <xsl:apply-templates select="$allc-weblogs/opt">
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="weblogs">
&lt;br/&gt;&lt;a href=&quot;<xsl:value-of select="@url"/>&quot;&gt;<xsl:value-of select="@url"/>&lt;/a&gt;(&lt;a href=&quot;http://allconsuming.net/weblog.cgi?url=<xsl:value-of select="@url"/>&quot;&gt;site info&lt;/a&gt;) <xsl:value-of select="@days_ago"/> days ago
</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>
