Using XSLT in the WSO2 Mashup Server

Maybe it’s just my long history with the technology, but I often find XSLT a convenient technology for manipulating XML.  When I scrape a Web page with the WSO2 Mashup Server, I generally use XSLT to extract and manipulate the values (e.g. my National Geographic Picture of the Day Feed mashup which I described here).  I find it performs better than extracting values individually through an XPath or RegExp filter or through E4X itself.

But what about when you’re not scraping a page?  While E4X does most XML manipulation tasks pretty well, it doesn’t provide some higher-level functions such as sorting.  The last thing I want to do is implement a sorting algorithm in Javascript, when XSLT already does the task very well.

The Mashup Server doesn’t have direct access to an XSLT processor, but you can use the Scraper object to execute a transformation even without performing a scrape.

Here’s a simple function that you can add to a mashup to support transformations inside your mashup.

transform.visible = false;
function transform(source, stylesheet) {
  var config =
    <config>
      <var-def name=’response’>
        <xslt>
          <xml>
            <template>{source.toXMLString()}</template>
          </xml>
          <stylesheet>
            <template>{stylesheet.toXMLString()}</template>
          </stylesheet>
        </xslt>
      </var-def>
    </config>;

  var scraper = new Scraper(config);
  var result = scraper.response;

  // strip off xml declaration and any PIs, E4X can’t parse them
  while (result.indexOf("<?") == 0)
    result = result.substring(result.indexOf("?>")+2);

  return new XML(result);
}

Usage is simple:

var xslt = <xsl:stylesheet version="1.0"
                xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
             …
           </xsl:stylesheet>;

var xml = <source/>;
var result = transform(xml, xslt);

Note that the function strips of any XML declarations or leading processing instructions, but you can (and should) also include <xsl:output method="xml" omit-xml-declaration="yes"/> inside your XSLT to make that extra cleanup step unnecessary.  Enjoy!

Blog Constellations Mashup

I finally got around to putting a few finishing touches on my Blog Constellations mashup.  It provides a visualization of a set of feeds, providing an intuitive sense of activity in a set of blogs, including frequency and size of posts, cross-links between the feeds, and highlighting links to "domains of interest."  This information proves useful in providing a feedback loop on the "quality" of blogging in support of the promotion of specific web sites, something WSO2 relies on as an open source company.

Some notes about the design of the mashup:

  • The blog analysis is performed by the mashup service (try it here), which has the following capabilities:
    • Subscription operations: add a feed to the analysis (trackBlog), remove a feed from the analysis(unTrackBlog), and list the feeds being analyzed (showTrackedBlogs).
    • Groups: Feeds are grouped under usernames so multiple groups of feeds are supported.  Groups can also be listed (listGroups) or removed (removeGroup).
    • Passwords: When you create a group by providing a password, adding or removing items from that group, or deleting the group entirely, requires a password.  Non password protected groups are also supported.
    • A utility method (fetchBlog) fetches a blog and analyses it in terms of size and the links it contains.
    • The analysis for a group of blogs can be easily obtained (getActivity).
  • The graphics are created entirely in the browser using the <canvas> tag and the excanvas.js library for IE.  Works pretty well although the graphic items don’t stay dynamic (and therefore can’t easily be used as links.)
  • The code contains some interesting performance techniques that may be useful for other mashup authors:
    • Reuses the feedCache mashup service to cache feeds for faster response.
    • Does some local caching of group analysis as well for faster response.
    • The mashup calls itself (getActivity calls fetchBlog) asynchronously to enable feeds in a group to be fetched in parallel.

The mashup is of course hosted on mooshup.com, our online version of the WSO2 Mashup Server.  This means you can try it out online or download it and run it locally on your Mashup Server, or use the service with your own front end or whatever.  The mashup page has all these links, metadata, script libraries, and everything you need to reuse this service or its source code.

Want to make some improvements?  How about these:

  • True multi-user capability - each user has their own groups, and can keep those groups private or make them public.
  • A mashup that finds a blogroll and publishes it into this mashup as a new group.
  • Adding animation (e.g. smooth zooming and spinning) and linking to the graphic (hover over a dot to see a preview of the blog?)
  • URLs for groups including options (so you can bookmark a particular visualization.)

Enjoy!