Menu

Generating RSS with XSLT and Amazon ECS

August 30, 2006

Craig Noeldner and Brian Swan

One choice you have to make when working with web services is how you will process the web service response. You could build the processing into a rich client, but that would likely require some type of installation for others to use your application. You could process the web service response on a server, but that requires, well, a server. Amazon ECS has an alternate solution: the XSLT service.

XSLT is a language for transforming XML into other formats. The XSLT service with Amazon ECS can transform a REST response using an XSLT file you identify by adding a parameter that specifies the location of the file and returning the transformed result. This means that a call to the Amazon ECS web service can return HTML, text, or any other format you want and makes it a compelling alternative to building a rich-client or server-based solution.

In this article we'll walk through the steps for generating an RSS feed for Amazon Wish Lists using Amazon ECS and the XSLT service. We're assuming you have some experience with XSLT, but the Additional Resources section has some links for learning more about the technologies we're using. There's also a complete XSLT file that you can download for your own use.

Finding a Wish List ID

The manual way to find your Wish List ID is to grab it off of the retail web site. For example, the URL for Brian's Wish List is:



http://www.amazon.com/gp/registry/registry.html?type=wishlist&id=30BOZ74K6RSKJ

In this case, the Wish List ID is 30BOZ74K6RSKJ.

The more programmatic way to get a Wish List ID is to use the ListSearch operation and search for lists associated with a specific e-mail address or name. For example, this request searches for a Wish List:

http://ecs.amazonaws.com/onca/xml?

   Service=AWSECommerceService&

   Version=2006-06-28&

   AWSAccessKeyId=0525E2PQ81DD7ZTWTK82&

   Operation=ListSearch&

   ListType=WishList&

   Email=noeldner@amazon.com

Starting with an Amazon ECS Request

To create a RSS feed for Wish Lists, let's first start out with a basic request to the Amazon ECS REST interface that returns information about a Wish List. (The fact that it's one of the author's Wish Lists is completely random.)



http://webservices.amazon.com/onca/xml?

   Service=AWSECommerceService&

   Version=2006-06-07&

   AWSAccessKeyId=1CE7SK4ZPTNDQZCWBP82&

   Operation=ListLookup&

   ListType=WishList&

   ListId=30BOZ74K6RSKJ&

   Sort=DateAdded&

   ResponseGroup=ItemAttributes,ListItems,ListInfo,Offers

This request returns information about each item on the Wish List. This is a snippet of the information we'll transform with our XSLT file:



<ListLookupResponse<

  <Lists>

    <List>

      <ListURL>http://www.amazon.com/...</ListURL>

      <CustomerName>Brian Swan</CustomerName>

      <ListItem>

        <QuantityDesired>1</QuantityDesired>

        <QuantityReceived>0</QuantityReceived>

        <Item>

          <DetailPageURL>http://www.amazon.com/...</DetailPageURL>

          <ItemAttributes>

            <Title>Product Name</Title>

          </ItemAttributes>

          <OfferSummary>

            <LowestNewPrice>

              <FormattedPrice>$9.99</FormattedPrice>

            </LowestNewPrice>

          </OfferSummary>

        </Item>

      </ListItem>

    </List>

  </Lists>

</ListLookupResponse>

Building the XSLT File

Next, we'll create an XSLT file that transforms this information into an RSS feed. This XSLT file uses the Wish List name and URL for the RSS feed title and uses information about the products to create separate items in the RSS feed.



<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="1.0" 

  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

  xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2006-06-07">

  

  <xsl:output method="xml"/>



  <xsl:template match="/">

  <rss version="2.0">

    <channel>

    <xsl:apply-templates select="aws:ListLookupResponse/aws:Lists/aws:List" />

    </channel>

  </rss>

  </xsl:template>



  <xsl:template match="aws:List">

    <title>WishList for <xsl:value-of select="aws:CustomerName" /></title>

    <link><xsl:value-of select="aws:ListURL"/></link>

    <xsl:apply-templates select="aws:ListItem"/>

  </xsl:template>



  <xsl:template match="aws:ListItem">

  <!--Filter out items that have already been purchased-->

  <xsl:if test="aws:QuantityDesired > aws:QuantityReceived">

    <item>

    <title><xsl:value-of select="aws:Item/aws:ItemAttributes/aws:Title" /></title>

    <link><xsl:value-of select="aws:Item/aws:DetailPageURL" /></link>

    <description>Lowest New Price: 

      <xsl:value-of select="aws:Item/aws:OfferSummary/aws:LowestNewPrice/aws:FormattedPrice"/>

    </description>

    <guid><xsl:value-of select="aws:Item/aws:DetailPageURL" /></guid>

    </item>

  </xsl:if>

  </xsl:template>

  

</xsl:stylesheet>

Amazon ECS requires that we declare a namespace and namespace prefix for processing the web service response. For example, our reference to aws:Item in the XSLT file corresponds to the <Item> element in the XML response. Be sure the version number in the namespace matches the version number in your request to Amazon ECS.

We added a link to the detail page for each product returned. We do this to be in compliance with the Amazon Web Services License Agreement.

Making the XSLT Service Request

We'll need to make a few changes to use the XSLT service with our original Amazon ECS request:

  • Add the Style parameter to the request
  • Send the request to the special XSLT service endpoint: http://xml-us.amznxslt.com

We also need to store the XSLT file on the web so Amazon ECS can access it. We stored our file using Amazon S3. Here's the request to the XSLT service:



http://xml-us.amznxslt.com/onca/xml?

   Service=AWSECommerceService&

   Version=2006-06-07&

   AWSAccessKeyId=1CE7SK4ZPTNDQZCWBP82&

   Operation=ListLookup&

   ListType=WishList&

   ListId=30BOZ74K6RSKJ&

   ResponseGroup=ItemAttributes,ListItems,ListInfo,Offers

   Sort=DateAdded&

   Style=http://s3.amazonaws.com/amazon_xslt_files/ecs_to_rss-wishlist.xslt

Consuming the RSS Feed

The XSLT service allows you to directly enter your request to Amazon ECS as a URL in your favorite RSS reader. There are a ton of RSS readers available, so just pick one that works best for you. We're using Sage in Firefox, so the feed looks like Figure 1:

Figure 1
Figure 1. Wish List feed

Creating Your Own Feeds

You can customize the example above for your own use. The basic steps are as follows:

  1. Download the XSLT file under Additional Resources.
  2. Place the XSLT file in a publicly-accessible place. Using Amazon S3 with a public-read policy works great!
  3. Update the Wish List ID in the Amazon ECS request with your ID.

You can also add your Associate ID to the Amazon ECS request, which allows you to earn a referral fee for any purchases that originate from your RSS feed. If you don't already have an Associate ID, you can sign up to get one.

You can also use the XSLT service to create any type of output you want. There are just a few things you should keep in mind:

  • The XSLT service isn't best for debugging, so a great way to test your XSLT is to save the standard Amazon ECS response as an XML file, then use a local XSLT processor to transform your saved XML file using the XSLT file you're developing.
  • Be sure the version number in the XSLT file namespace matches the version number in your request to Amazon ECS.
  • Be sure to include a link to the product.

Additional Resources