escutcheon

overview

This demo shows how the REST/XML-HTTP interfaces of the Google (via Xoomle) and Amazon web service interfaces can be combined using XSLT to provide useful results.

Update: I have another, somewhat more interesting example here.

demo files

test.xml a list of book titles
author.xsl the driver stylesheet
amazon.xsl encapsulates a call to the Amazon web service
google.xsl encapsulates a call to the Google web service via Xoomle
keys.xsl holds the url to a set of developer keys
somekeys.xml a set of developer keys

instructions

Note: Amazon appears to be rather loose with its enforcement of the use of keys, but Google is strict. Clicking on the test.xml file above directly will attempt to apply the XSLT tranformation using the null keys in the somekeys.xml file. This results in an author name and image from Amazon, but the links which would have been obtained from Google are null.

Note: Depending on the security constraints of your browser, if you try to execute the transformation directly by opening the test.xml file, a security warning dialog box may appear stating that the page is accessing external data. This is normal.

Note: Mozilla 1.1's implementation of the XSLT document() method which these transformations are dependent upon, does not allow cross-domain access, so the demo can not run directly in that browser. To see the results in a Mozilla browser, run the transformation directly using a compliant parser, such as Xalan, and view the resultant HTML file.

description

The test XML file contains the titles of two books. The file references a stylesheet, author.xsl, using a processing instruction:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="author.xsl" type="text/xsl"?>
<books>
  <title>The Brothers Karamazov</title>
  <title>A New Kind of Science</title>
</books>

The author.xsl stylesheet includes two other stylesheets that encapsulate the interfaces to Amazon and Google.

<xsl:include href="amazon.xsl"/>
<xsl:include href="google.xsl"/>

Amazon and Google both require developer keys to be included in the URL when calling the service. These keys are provide when signing up for the service. For convenience I have put these keys into a single file which is referenced by the individual service stylesheets. In a way, this is yet a third "service" that is made part of this demonstration; in the future personal information could be made available to trusted parties in a manner similar to this. Microsoft's Passport and the Liberty Alliance demonstrate the beginnings of these sorts of services.

The name of the author is obtained by passing the book title to a template defined in the amazon.xsl stylesheet:

<xsl:template match="title">

<xsl:variable name="author">
  <xsl:call-template name="get-author">
    <xsl:with-param name="title" select="."/>
  </xsl:call-template>
</xsl:variable>

The core of the "get-author" template is the document() XSLT function which actually dereferences the URL that is built up in the body of the template. The response from Amazon is an XML document that is parsed and the relevant data extracted.

<xsl:template name="get-author">
<xsl:param name="title"/>

<xsl:variable name="amazon-title"  
  select="translate(normalize-space($title), ' ', '+')"/>
<xsl:variable name="amazon-key"    
  select="document($key)//amazon"/>
<xsl:variable name="amazon-search" 
  select="concat($amazon, '&KeywordSearch=', 
  $amazon-title, '&dev-t=', $amazon-key)"/>
<xsl:variable name="amazon-result" 
  select="document($amazon-search)"/>

<xsl:value-of 
  select="($amazon-result//Authors)[1]/Author[1]"/>

</xsl:template>

$author is then passed to xoomle and a URL obtained in a similar manner:

<xsl:variable name="url">
  <xsl:call-template name="feeling-lucky">
    <xsl:with-param name="search-term" select="$author"/>
  </xsl:call-template>
</xsl:variable>

Amazon can also provide a URL to the book cover image. In the demo, this image is obtained in a similar manner to author. Now that $author, $image and $url have been obtained, the data is applied to the XHTML result.

Note: Obviously it would be preferable to obtain both the $author and $image information in a single call to Amazon and return them as a node-set. There is a major limitation to XSLT 1.0 however: the values could only be returned as a tree-fragment, which can't be parsed like a node-set. XSLT 2.0 will take care of this limitation which will allow for a better encapsulation of the services along the lines that I've tried to demonstrate here.

result

When the test file is transformed using authentic developer keys and a compliant parser, the result should look as in this file: