Digg Widget for Blogger - XSLT Transformation on Response

Ok, so we have triggered a HTTP request to Digg and received the top posts as a XML and hold it in an object for processing. What we need to do now, is to process the received XML and identify the title of the post and link the title with a hyperlink to the actual URL. Once this is done, all we need to do is to push these results to our blog page. (we will not be using Java for that case)

To create these titles and links, we will be making use of XSLT. So, given an XML, what would be the best XSLT that we can device to get the output that we desire? . In the XML response, the tags that we are focussing are

1) stories/story/title
2) stories/story[link]

Putting our XSLT to work, we end up with the following style sheet to accomplish the task

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/> -->
    <xsl:template match="stories">
        <html>
            <body>
                <xsl:for-each select="story">
                    <a>
                        <xsl:attribute name="href"><xsl:value-of select="@link"/></xsl:attribute>
                        <xsl:value-of select="title"/>
                    </a>
                    <BR/>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

This is really a simple stylesheet, which when applied to the output XML from Digg, would give the results with proper hyperlink as we expect. As an example, we will get the output as

Link1
Link2

Now that we have an XLST, how do we integrate this to the HTTP request code that we created earlier in Java?. We have two options here:-

1) Parse the XML using VTD and send the result alone back.

2) Send the XML and apply the XSLT that we created to get the output.

I would leave this to the readers to choose the right one for their approach. We have covered how to use XSLT to achieve the linking. We can also use VTDs powerful featues to achive the same.

So, now we have the following tasks on hand:-

1) Use JQuery to simulate the GET request.

2) Apply the transformation on the resulting XML

3) Convert the output as a widget, dynamically picking the labels from the blog post.

We will cover this of in the days to come.

No comments:

Post a Comment