Transform XML using XSLT in Internet Explorer

Damian Czapiewski picture Damian Czapiewski · Mar 7, 2011 · Viewed 7.2k times · Source

I need JavaScript script to parsing of XML files through XSLT sheet to XHTML code. I've code compatible with Firefox, Opera and Safari.

  function loadXMLFile(path) {
   var file = document.implementation.createDocument("", "", null);
   file.async = false;
   file.load(path);
   return file;
  }
  function parseXMLFile() {
   var xml = loadXMLFile("data.xml");
   var xsl = loadXMLFile("data.xsl");
   var xslt = new XSLTProcessor();
   xslt.importStylesheet(xsl);
   var xhtml = xslt.transformToFragment(xml, document);
   document.firstChild.replaceChild(xhtml, document.firstChild);
  }
  parseXMLFile();

It is invalid code for Internet Explorer and Chrome. I know about Microsfot.XMLDOM library for IE, but I don't know how use it. How write good code for IE and optional Chrome?

Answer

Gregory Nozik picture Gregory Nozik · Sep 14, 2011

Here is example of the transformation of XSLT in IE

var xml = new ActiveXObject("Microsoft.XMLDOM"); 
var xslt = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
xml.load("data.xml");
xslt.load("data.xls");

var processor   = new ActiveXObject("Msxml2.XSLTemplate");
processor.stylesheet = xslt;

var objXSLTProc = processor.createProcessor();
objXSLTProc.input = xml;
objXSLTProc.transform();
var output  = objXSLTProc.output;

I wrote article about it in my blog