After I’ve finally managed to persuade IE7, FF and Transform jQuery plugin to work with my XSL documents, I’ve started testing the other two browsers I have: Opera and Chrome. The latest release of Opera might occasionally work. Chrome fails (as expected) as it doesn’t support xsl:import. Welcome to the next round of browser incompatibilities.
I’ve decided to ignore Chrome until the great minds @ Google decide to implement XSLT properly. Visitors using it will have reduced experience … but of course I have to detect whether I should use XSLT or not.
Here’s a small Christmas gift if you have similar issues: a jQuery extension that checks whether the current visitor can use XSLT transformations.
jQuery.xslt = { need: function(success,url,xml) { var componentCount = 0; var xslt; var el; var debug=1; function tryTransform() { if (typeof(xslt) != 'object') return; // XSL did not parse into XML object var html = $.transform({xmlstr: xml, xslobj: xslt, async: false}); alert("html="+html); if (!html) return; } function gotScript() { function transformFail(html,xsl,xml,obj,ex) { if(debug) alert ("$.xslt.need failed:"+ex.message); } function transfromDone(html,xsl,xml,obj) { $.xslt.has = html.search($.xslt.expectedResult) >= 0; if ($.xslt.has && success) success(); } el = $("<div>"); el.transform({xmlstr: xml, xsl: url, success: transfromDone, error: transformFail}); } url = url ? url : $.xslt.defaultURL; xml = xml ? xml : $.xslt.defaultXML; $.getScript($.xslt.transformScript,gotScript); }, has: false, defaultURL: "/forms/xml/static.xsl", defaultXML: "<section />", transformScript: "/common/js/jquery.transform.packed.js", expectedResult: /table/i }
The extension implements a simple function and a property: $.xslt.need() and $.xslt.has. You can pass the URL to the sample XSL document and the sample XML markup to the need function; it also supports a success callback to tell you whether you should install XSL-related action handlers.
The need function loads the jQuery Transform plugin (reducing the load time if the page does not need the XSLT functionality) and tries to transform a sample XSL document. The sample transformation should be as complex as possible: you should use xsl:import, xsl:include or the document() function if you use them in other transformations.
You could change the default parameters in the source code or write a setup function.
No comments:
Post a Comment