XSLT Tranformation with Javascript in Google Chrome?

Could someone who’s skilled in javascript please help me solve this.

I’m trying to get the W3C tutorial XSLT - On the Client to work in the Google Chrome Browser.
It works fine in IE and FF but in Chrome it doesn’t work at all, no error message even.

I read some things about XMLdocument.load isn’t supported in Chrome so I’m guessing it’s something to do with that but I have no idea how to implement a fix.

I read this on a different forum. I’ve tried implementing it in the code but cannot get it to work.

Try replacing:

var xmlDoc = document.implementation.createDocument("", " ", null);
xmlDoc.async = false;
xmlDoc.load(xmlsrc);

With:

var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", xmlsrc, false);
xmlhttp.send(null);
var xmlDoc = xmlhttp.responseXML.documentElement;

Here is the code I’m trying to get to work:

<html>
<head>
<script>
function loadXMLDoc(fname)
{
  var xmlDoc;
  // code for IE
  if (window.ActiveXObject)
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation
  && document.implementation.createDocument)
  {
    xmlDoc=document.implementation.createDocument("","",null);
   }
  else
  {
    alert('Your browser cannot handle this script');
  }
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}

function displayResult()
{
xml=loadXMLDoc("cdcatalog.xml");
xsl=loadXMLDoc("cdcatalog.xsl");
// code for IE
if (window.ActiveXObject)
  {
    ex=xml.transformNode(xsl);
    document.getElementById("example").innerHTML=ex;
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation
  && document.implementation.createDocument)
  {
    xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    resultDocument = xsltProcessor.transformToFragment(xml,document);
    document.getElementById("example").appendChild(resultDocument);
  }
}
</script>
</head>
<body id="example" onLoad="displayResult()">
</body>
</html>

Thanks for any help!!

No one? :frowning:

I realise this thread is >17 months old, but just for archival purposes and for “closure” I am posting a version of the OP’s script with a couple of tiny changes which made it “work for me” (at least it did on Firefox, Chrome, Internet Explorer, and Dragon - I don’t know about Safari, Konqueror, etc but I presume they would be like Chrome, due to also being Webkit-based?):

<html>
<head>
<script>
function loadXMLDoc(fname)
{
  var xmlDoc;
  // code for IE
  if (window.ActiveXObject)
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLHTTP");
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (window.XMLHttpRequest)
  {
    xmlDoc=new XMLHttpRequest();
   }
  else
  {
    alert('Your browser cannot handle this script');
  }
xmlDoc.open("GET",fname,false);
xmlDoc.send("");
return(xmlDoc.ResponseXML);
}

function displayResult()
{
xml=loadXMLDoc("cdcatalog.xml");
xsl=loadXMLDoc("cdcatalog.xsl");
// code for IE
if (window.ActiveXObject)
  {
    ex=xml.transformNode(xsl);
    document.getElementById("example").innerHTML=ex;
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (window.XMLHttpRequest)
  {
    xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    resultDocument = xsltProcessor.transformToFragment(xml,document);
    document.getElementById("example").appendChild(resultDocument);
  }
}
</script>
</head>
<body id="example" onLoad="displayResult()">
</body>
</html>

This wraps the generated code inside the <body> tags, so make sure the xsl doesn’t also create <html> or <body> tags (if you care about validation…).