Google AJAXSLT 0.2 Notes and Patches
Lately I’ve been working on a tutorial about Google AJAXSLT, which is Steffen Meschkat’s promising cross-browser AJAX / XSLT API. It provides a basic DOM implementation, and an XSLT 1.0 processor. A neatly-separated XPath implementation not only powers XSLT’s xsl:select, but can also be used to query documents outside of XSLT, for use with DOM. All of this is written in pure Javascript (!), which means that all your browser needs to use it is a working Javascript implementation.
While the project name has the word “AJAX” in it, there aren’t, strictly speaking, any actual AJAX functions included in the API. What you get is a toolkit that provides what many of the AJAX stuff out there doesn’t provide—a powerful, standards-aware way of generating, accessing, and rendering the XML that your AJAX queries work with. Even at this stage, I’ve been able to drop in a few formerly-server-side XSLT stylesheets without modification for use with AJAX output.
The major drawback with Google AJAXSLT at this stage (version 0.2 at time of writing) is that the API is very much in beta, and there are some rough edges. Until Steffen decides to release 0.3, some of us are already itching to play with this thing on a daily basis! What follow are some patches that I’ve been applying to the API to deal with existing issues.
misc.js:
The file misc.js in v0.2 has an issue with attribute output during serialization, where the wrong type of quote, ’, is replaced with the quot entity. Here’s the patch to apply:
function xmlEscapeAttr(s) {
//return xmlEscapeText(s).replace(/\'/g, '"');
return xmlEscapeText(s).replace(/\"/g, '"');
}
dom.js:
If you use DOM with any number-crunching code, you’ll quickly discover that something isn’t right when setting attribute values to numbers. Version 0.2 doesn’t cast your number value to a string, which causes regex problems during serialization. Here’s a patch:
XNode.prototype.setAttribute = function(name, value) {
for (var i = 0; i < this.attributes.length; ++i) {
if (this.attributes[i].nodeName == name) {
if(typeof(value)=='number') value=value+'';
this.attributes[i].nodeValue = value;
return;
}
}
if(typeof(value)=='number') value=value+'';
this.attributes.push(new XNode(DOM_ATTRIBUTE_NODE, name, value));
}
Internet Explorer has a strange split() method, which causes some issues with DOM. Here’s a patch I found on the newsgroups to fix this issue:
First, replace:var x = xml.split( /</ );
with this:
var x = stringSplit( xml, '<' );
And finally, replace:
var xx = x[i].split( />/ );
with:
var xx = stringSplit( x[i], '>' );
Those are all the patches I’ve needed to survive thus far. Happy coding!
