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();
xsltProcessor.importStylesheet(xslObject);
var result = xsltProcessor.transformToFragment(
xmlObject,document);
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");
html.load(urlReq);

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.

5 Responses to Using AIR for XSLT Processing

  1. Bob W. says:

    Brian, This is exactly what I was looking how to do.. thank you.

  2. thank you brian , i think you know that AIR/javascript also support Xpath document manipulation through document.evaluate 😉

  3. Brian,
    Thanks for the sample app. Thanks to you, I didn’t have to write a single line of code.
    I had the original XML, I just needed to write the XSLT. Nice little tool this air app is!

  4. Anoop Menon says:

    Hello Brian,
    Nice tutorial but I am stuck with an issue. I am porting Splunk to work on adobe webkit based browser named spinr. The application works well in all browsers. When I traced the code what I could find was no data is returned after the XSLT transformation.
    The control gets stuck with this line of code
    var fragment = proc.transformToFragment(incomingDocument, document);
    Input xsl and xml are correct above this line of code.but the value of fragment when I parse in a loop and alert I get nothing on air browser but in Mozilla/IE I get UL, DIV, FORM, SPAN etc
    I checked with other air based browsers too there also the same problem exists. Can you throw some light on what to look for?
    I will greatly appreciate any help.

  5. Neil Davis says:

    Thanks Brian, but has anyone been able to get this to work with an XSLT stylesheet that includes an xsl:import element ? I can’t seem to get this to work no matter how I define the href attribute for the xsl:import (file://, full native path, uri encoded) etc