Using AIR for XSLT Processing

One of the features we’re investigating for the next version of the media player requires that we be able to do client-side XSL transformations.  I had heard a few rumors that AIR exposes the XSLT processor that’s built into WebKit (which is AIR’s embedded HTML engine), so I shot off a couple of emails to the AIR team.  Sure enough, this functionality is exposed through AIR’s Javascript API.

Here’s a Javascript method that takes an XML document, an XSL transformation, and returns the result of transforming the document:

function transformXML(xml,xsl)
var domParser = new DOMParser();
var xmlObject = domParser.parseFromString(xml,"text/xml");
var xslObject = domParser.parseFromString(xsl,"text/xml");
var xsltProcessor = new XSLTProcessor();
var result = xsltProcessor.transformToFragment(
var serializer = new XMLSerializer();
return serializer.serializeToString(result);

If you’re building a pure-Javascript AIR app, then this is probably all you need.  But if you want to use this functionality in an ActionScript-based AIR app, then there’s one small problem:  WebKit’s API isn’t available in ActionScript!  Fortunately, there’s a workaround: use the script bridging capabilities of AIR to call a Javascript function from ActionScript. First, you create a new instance of an HTMLLoader and pass in the URL of the HTML file that contains the transformXML function:

var html:HTMLLoader = new HTMLLoader();
var urlReq:URLRequest = new URLRequest("xslt.html");

Then invoke the transformXML method via the HTMLLoader’s window property:

var result:String = html.window.transformXML(xml,xsl);

Here’s a simple AIR app that demonstrates this.  Just enter some XML in the top text box, an XSLT in the second text box, click “Process”, and you’ll see the output in the lower text box.  Right click on the application to see source code.

Opening Credits

Here’s my intro, short and sweet.  My name is Brian Riggs, and I’m the lead engineer for the Adobe Media Player project.  For the last year and a half, our team has been heads-down on development, but now that the media player has actually shipped (woohoo!), I’ve got a little bit more free time on my hands.  I’m planning to use this blog to share my thoughts on developing on Flex and AIR.

AIR is an insanely cool platform on which to build a rich internet application. It boggles my mind that you can build and deploy an application that works on Windows, Mac, and (soon) Linux without writing any platform-specific code and without having to create any installers.  For these reasons alone, AIR is probably the best technology I’ve seen for creating one-off demo apps and proofs of concept.  And the combination of Flex/Flash with HTML/Javascript opens up some very interesting possibilities.

I’m also hoping to talk about some of the interesting features in the media player, and how we implemented them.  Speaking of the media player, anyone notice that The Daily Show and The Colbert Report are now available?