Hack 90 Create a Wireless Wish List

figs/expert.giffigs/hack90.gif

Take your Wish List wherever you go with AWS, XSLT, and WAP!

How many times have you been browsing at a bookstore or video store and you can't remember which books or movies you wanted to pick up? Sure, you could write them all down on a piece of paper and take it with you, but what fun would that be? Instead, you can use powerful cell networks and web applications to do the work for you. If you track wanted books and movies with your Amazon Wish List and have a WAP-enabled cell phone, you can always have your list handy.

WAP stands for Wireless Access Protocol, and it's used for delivering information to cell phones and other handheld devices. The information itself is formatted with WML (Wireless Markup Language), an XML format.

Just as HTML pages have <body> elements that contain the bulk of the page, WML pages have <card> elements. Each WML page can contain several cards, but only one card is displayed at a time. Also, links work much the same way as in HTML. Instead of <a href> tags, WML relies on <anchor> and <go href> tags. This example creates one card with a list of items. Each item links to the Amazon WAP product detail page for that item.

90.1 The Code

Making AWS responses available to a cell phone is just a matter of converting one XML format (AWS response) to another (WML). Once again, Amazon's XSLT service makes this quick work.

Create an XSL file called wap_wishlist.xsl with the following code:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/[RETURN]
Transform">
<xsl:output method="wml" doctype-public="-//WAPFORUM//DTD WML 1.1//EN" [RETURN]
doctype-system="http://www.wapforum.org/DTD/wml_1.1.xml"/>
<xsl:template match="/">
    <wml>
    <card id="Menu" title="Wishlist">
    <p><b>My Wishlist</b><br/><br/>
    <xsl:for-each select="ProductInfo/Details">
            <b><xsl:value-of select="Catalog" />:</b>
            <br/><xsl:value-of select="ProductName" />
            <xsl:if test="Artists">
                <br/>by 
                <xsl:value-of select="Artists/Artist" />
            </xsl:if>
            <xsl:if test="Authors">
                <br/>by 
                <xsl:value-of select="Authors/Author" />
            </xsl:if>
            <br/><xsl:value-of select="OurPrice" />
            <br/><anchor>Details<go><xsl:attribute name="href">http://[RETURN]
www.amazon.com/exec/obidos/redirect?tag=insert associate tag%26creative=[RETURN]
insert developer token%26camp=2025%26link_code=xm2%26path=ct/text/[RETURN]
vnd.wap.wml/-/tg/aa/xml/glance-xml/-/<xsl:value-of select="Asin" />[RETURN]
</xsl:attribute></go></anchor>
            <br/><br/>
    </xsl:for-each>
    </p>
    </card>
    </wml>
</xsl:template>
</xsl:stylesheet>

There are a few important things to note in this code. First, the output type has been set to WML with the <xsl:output> tag. Without this, the required <!DOCTYPE tag would not be included in the transformed page. Also notice the link to WAP Amazon detail pages. The URL format is the same (see [Hack #72]), but the ampersands have all been changed to the encoded %26. Ampersands aren't valid when used in URLs inside a WML file, and phones can't display the page if they aren't escaped.

90.2 Running the Hack

The code is just an Amazon query, so we need to get the right URL. Find your Wish List ID [Hack #18] and include it in a wish list search like this:

http://xml.amazon.com/onca/xml3?t=insert associate tag [RETURN]
&dev-t=insert developer token&WishlistSearch=[your wishlist]&type=lite&f=xml

Upload wap_wishlist.xsl to a publicly accessible server and change the value of f= to the URL for the file:

http://xml.amazon.com/onca/xml3?t=insert associate tag  [RETURN]
&dev-t=insert developer token &WishlistSearch=[your wishlist]&type=lite [RETURN]
&f=http://example.com/wap_wishlist.xsl 

There's just one more change that needs to be made. WAP browsers are looking for a certain content type. Content types are specified as HTTP headers, and there's no way to include those in our XSL file. Luckily, Amazon provides a way to specify an alternate content type header with the query itself. Add the ct= variable and set the content type to text/vnd.wap.wml.

http://xml.amazon.com/onca/xml3?t=insert associate tag  [RETURN]&dev-t=insert developer token &WishlistSearch=[your wishlist]&type=lite [RETURN]
&f=http://example.com/wap_wishlist.xsl&ct=text/vnd.wap.wml 

That's all there is to it. Unfortunately this URL is a bit long to type into your phone with your keypad, but there are a couple of ways to work around this. First, see if your cell provider has a web interface for adding bookmarks to your phone's WAP browser. If so, you could copy and paste this monster into your cell provider's site, and then have 1-click (ahem) access through your phone's bookmarks. Another method is to upload a WML file to your site that contains a link to your wish list via this URL. Then you'd just need to browse to your own WAP page first and follow the link. If you go this route, try giving your WAP page a short name, like a.wml; this will save some keying.