Blog

Tutorial: Amazon.com Search API

Posted on

Transforming XML with XSLT

Transforming XML with XSLT

Figure 5

Transforming XML with XSLT

XSL is an XML-based language for transforming XML tags into either HTML or an alternate set of XML tags. With XSL, a novice developer can produce rich content without complex parsing or programming knowledge.

Since this isn’t a tutorial on XSL, I will not go into too much detail about how the XSL language is used to parse XML. There are many tutorials that do this, and I strongly recommend reading the following, if you are new to XSL.

XSL Introduction by Jan Egil Refsnes
An introduction to XSL – The style sheet language of XML. What XSL is and what it can do.

XSL Transformation by Jan Egil Refsnes
How XSL can be used to transform XML documents into HTML documents, by inserting a reference to an XSL stylesheet into the XML document.

XSL on the Client by Jan Egil Refsnes
How the XML parser can be used to transform an XML document to an HTML document on the client.

XSL Sorting by Jan Egil Refsnes
How to let the XML parser sort your XML document before it is transformed to HTML.

XSL Filtering by Jan Egil Refsnes
How to let the XML parser filter your XML document before it is transformed to HTML.

Using XSLT with ECS by Amazon Associates Developer’s Guide
A brief overview of how XSLT is used in transforming requests to the Amazon web service.

An important part of the XSLT stylesheet is the namespace attribute. You must first create a namespace attribute with a url that matches the namespace URL that is returned to you in the response to your web services request. The namespace URL will typically be in the second line of the response and will look something like the namespace URL in the example below.

xmlns:xsl=http://www.w3.org/1999/XSL/Transform
xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2004-03-19">

In the above example, the third line that starts with xmlns defines a namespace prefix and URL for the style sheet. The namespace prefix is aws, and the namespace URL is http://webservices.amazon.com/AWSECommerceService/2004-03-19. In your style sheet, you may name the namespace prefix anything.

Once you have created your namespace prefix, use it to match elements in the Amazon web service ECS response. For instance, if you are trying to match an element called ItemLookupResponse, and your prefix is aws, the matching string would be aws:ItemLookupResponse.

Below is a sample XSLT stylesheet for the Amazon ECS web service.

<!--?xml version="1.0" encoding="UTF-8"?-->
xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2004-03-19" exclude-result-prefixes="aws"></pre>
<table style="width: 90%; padding: 5px;" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="50"><img alt="" />

 0</td>
<td valign="top">
 <span style="font-size: 10px;">

 by

 by

 by

 by

 from

 </span>

 <span style="font-size: 11px;">
 List Price: select="aws:ItemAttributes/aws:ListPrice/aws:FormattedPrice" />
 </span></td>
</tr>
</tbody>
</table>
<pre>