Show me the code! – By Davanum Srinivas

September 3, 2008

[ubiquity] View atom feeds (aka. how to fetch/parse an xml and transform it using a xslt stylesheet)

Filed under: Uncategorized — Davanum Srinivas @ 11:50 pm

Basically shows how to fetch XML from a url, create a dom from a string, run a xsl transformation etc. Just point the browser to a web site that has atom feeds like http://enthusiasm.cozy.org/ or http://planet.intertwingly.net/ and run “view-atom-feed” to see it in action.

CmdUtils.CreateCommand({
    name: "view-atom-feed",
    preview: function(pblock) {
        var document = context.focusedWindow.document;
        
        // Look for <link> with appropriate rel and type attributes
        links = document.getElementsByTagName("link");
        href = "";
        for (i = 0; i < links.length; i++) {
            rel = links&#91;i&#93;.getAttribute("rel");
            type = links&#91;i&#93;.getAttribute("type");

            if ((type == "application/atom+xml" && rel == "alternate")) {
                // found one!
                href = links&#91;i&#93;.getAttribute("href");
                break;
            }
        }

        if (href != "") {
            // Get the ATOM feed
            jQuery.ajax({
                async: false,
                type: "GET",
                url: href,
                dataType: "xml",
                success: function(xml) {

                    // create a dom for the xsl and import the stylesheet
                    xsltProcessor = new window.XSLTProcessor();
                    xsl = '<?xml version="1.0" encoding="utf-8"?>' + 
                      '<xsl:stylesheet version="1.0"' + 
                      '     xmlns:atom="http://www.w3.org/2005/Atom"' +
                      '     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"' +
                      '     xmlns:dc="http://purl.org/dc/elements/1.1/">' +
                      '     <xsl:template match="/atom:feed">' +	   
                      '         <ul>' +	
                      '             <xsl:apply-templates select="atom:entry"/>' +	  
                      '         </ul>' +
                      '     </xsl:template>' +
                      '     <xsl:template match="atom:entry">' +
                      '         <li><a href="{atom:link/@href}"><xsl:value-of select="atom:title"/></a></li>' +
                      '     </xsl:template>' +
                      '</xsl:stylesheet>';
                    xslDocument = new window.DOMParser().parseFromString(xsl, 'application/xml');
                    xsltProcessor.importStylesheet(xslDocument);

                    // run the transform to generate html
                    var result = xsltProcessor.transformToFragment(xml, pblock.ownerDocument);

                    // append the generated html to the preview block
                    pblock.appendChild(result);
                }

            });
        } else {
            pblock.innerHTML = "<h3> This web site has no ATOM feeds </h3>";
        }
    },
    execute: function(statusText) {}
})

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: