« Amazon's Web Services and XSLT | Main | SAML »

July 21, 2008

XSLT for Amazon AWS book search to JSON

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" id="amazon2json"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2008-06-26"
                >
    <xsl:output method="text" indent="yes"/> 
    <xsl:strip-space elements="*" /> 
    <xsl:template match="/aws:ItemSearchResponse">
        {
        <xsl:apply-templates/>
        }
    </xsl:template>
    <xsl:template match="aws:Items">
        <xsl:for-each select="aws:Item">
            "<xsl:value-of select="aws:ASIN"/>" : {
            "isbn" : "<xsl:value-of select="aws:ASIN"/>",
            <xsl:if test="aws:ItemAttributes/aws:Author">"authors" : [
            <xsl:for-each select="aws:ItemAttributes/aws:Author">
                "<xsl:value-of select="."/>"
                <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
                ],</xsl:if>
            <xsl:if test="aws:ItemAttributes/aws:Author">
            "author" : "<xsl:for-each select="aws:ItemAttributes/aws:Author">
                <xsl:value-of select="."/>
                <xsl:if test="position() != last()">
                <xsl:text>, </xsl:text>
                </xsl:if>
            </xsl:for-each>",</xsl:if>
            "publisher" : "<xsl:value-of select="aws:ItemAttributes/aws:Manufacturer"/>",
            "title" : "<xsl:value-of select="aws:ItemAttributes/aws:Title"/>",
            "url" : "<xsl:value-of select="aws:DetailPageURL"/>",
            "price" : "<xsl:value-of select="aws:ItemAttributes/aws:ListPrice/aws:FormattedPrice"/>",
            "format" : "<xsl:value-of select="aws:ItemAttributes/aws:Binding"/><xsl:text> </xsl:text><xsl:value-of select="aws:ItemAttributes/aws:ProductGroup"/>",
            "publication_date" : "<xsl:value-of select="aws:ItemAttributes/aws:PublicationDate"/>",
            "edition" : "<xsl:value-of select="aws:ItemAttributes/aws:Edition"/>",
            "thumbnail" : "<xsl:value-of select="aws:MediumImage/aws:URL"/>"
            }
            <xsl:if test="position() != last()">
            <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="aws:OperationRequest">
    </xsl:template>
    <xsl:template match="aws:ItemLinks">
    </xsl:template>
</xsl:stylesheet>

Tags: XSLT

Posted by pj at July 21, 2008 03:21 PM

Comments