Chrome and Safari XSLT using JavaScript Chrome and Safari XSLT using JavaScript google-chrome google-chrome

Chrome and Safari XSLT using JavaScript


If your XSLT is using xsl:include you might receive weird unexplainable errors but always with the same end result: your transformation failing.

See this chromium bug report and please support it!http://code.google.com/p/chromium/issues/detail?id=8441

The bug is actually in webkit though. For more info here's another link which goes into more detail why it doesn't work.

The only way around this is to pre-process the stylesheet so that it injects the included stylesheets. Which is what a crossbrowser XSLT library like Sarissa will do for you automatically.

If your looking for jQuery solution:
http://plugins.jquery.com/project/Transform/ is a cross browser XSL plug-in. I've succesfully used this to get xsl:include working in the past without much hassle. You don't have to rewrite your xsl's this plugin will pre-process them for you. Definitely worth looking at as it's more lightweight then Sarissa.

UPDATE:

<html><head><script language="javascript" src="jquery-1.3.2.min.js"></script> <script language="javascript" src="jquery.transform.js"></script>  <script type="text/javascript">function loadXML(file){    var xmlDoc = null;    try //Internet Explorer    {        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");        xmlDoc.async=false;        xmlDoc.load(file);    }    catch(e)    {        try //Firefox, Mozilla, Opera, etc.        {            xmlDoc=document.implementation.createDocument("","",null);            xmlDoc.async=false;            xmlDoc.load(file);        }        catch(e)        {            try //Google Chrome            {                var xmlhttp = new window.XMLHttpRequest();                xmlhttp.open("GET",file,false);                xmlhttp.send(null);                xmlDoc = xmlhttp.responseXML.documentElement;            }            catch(e)            {            error=e.message;            }        }    }    return xmlDoc;}function xslTransform(xmlObject, xslObject){    try     {        $("body").append("<div id='test'></div>");        var a = $("#test").transform({ xmlobj: xmlObject, xslobj: xslObject });    }    catch (exception)     {        if (typeof (exception) == "object" && exception.message)             alert(exception.message);        else alert(exception);    }}var xmlObject = loadXML("input.xml");var xslObject = loadXML("transform.xsl");$(document).ready(function()  {    xslTransform(xmlObject, xslObject);});</script></head><body></body></html>

This test html page works both in Chrome/FireFox/IE.

input.xml is just a simple xml file containing <root />transform.xsl is the stripped down xsl you posted.

EDIT

It does however seem the $.transform has problems importing stylesheets from included files:

Here's how to fix this:

Locate

var safariimportincludefix = function(xObj,rootConfig) {

in jquery.transform.js and replace the entire function with this:

var safariimportincludefix = function(xObj,rootConfig) {    var vals = $.merge($.makeArray(xObj.getElementsByTagName("import")),$.makeArray(xObj.getElementsByTagName("include")));    for(var x=0;x<vals.length;x++) {        var node = vals[x];        $.ajax({            passData : { node : node, xObj : xObj, rootConfig : rootConfig},            dataType : "xml",            async : false,            url : replaceref(node.getAttribute("href"),rootConfig),            success : function(xhr) {                try {                    var _ = this.passData;                    xhr = safariimportincludefix(xhr,_.rootConfig);                    var imports = $.merge(childNodes(xhr.getElementsByTagName("stylesheet")[0],"param"),childNodes(xhr.getElementsByTagName("stylesheet")[0],"template"));                    var excistingNodes = [];                    try                     {                        var sheet = _.xObj;                        var params = childNodes(sheet,"param");                        var stylesheets = childNodes(sheet,"template");                        existingNodes = $.merge(params,stylesheets);                    }                    catch(exception)                     {                        var x = exception;                    }                    var existingNames = [];                    var existingMatches = [];                    for(var a=0;a<existingNodes.length;a++) {                        if(existingNodes[a].getAttribute("name")) {                            existingNames[existingNodes[a].getAttribute("name")] = true;                        } else {                            existingMatches[existingNodes[a].getAttribute("match")] = true;                        }                    }                    var pn = _.node.parentNode;                    for(var y=0;y<imports.length;y++) {                        if(!existingNames[imports[y].getAttribute("name")] && !existingMatches[imports[y].getAttribute("match")]) {                            var clonednode = _.xObj.ownerDocument.importNode(imports[y],true);                            //pn.insertBefore(clonednode,_.xObj);                            pn.insertBefore(clonednode,childNodes(_.xObj,"template")[0]);                        }                    }                    pn.removeChild(_.node);                } catch(ex) {                 }            }        });    }    return xObj;};

Now using the previously pasted test index.html use this for transform.xsl:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0"    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    >        <xsl:include href="include.xsl" />    <xsl:output method="html"/>    <xsl:template match="/">            <xsl:call-template name="giveMeAnIncludedHeader" />    </xsl:template></xsl:stylesheet>

And this for include.xsl

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template name="giveMeAnIncludedHeader">        <h1>Test</h1>    </xsl:template></xsl:stylesheet>

With the previously posted fix in jquery.transform.js this will now insert the included <h1>Test</h1> on all the browsers.

You can see it in action here: http://www.mpdreamz.nl/xsltest


This is not an answer to the original question but during my search over internet looking for a sample xslt transformation that works on chrome I found links to this thread many times. I was looking for a solution that doesn't use any open-source or third party libraries/plugins and works well with silverlight.

The problem with chrome and safari is the limitation that prevents loading xml files directly. The suggested workaround at http://www.mindlence.com/WP/?p=308 is to load the xml file via any other method and pass it as a string to the xslt processor.

With this approach I was able to perform xsl transformations in javascript and pass on the result to silverlight app via HTML Bridge.