0

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?

  • 1
    You do not want to "parse XML through XSLT". You want to "transform XML using XSLT". I suggest adapting the question. Side note: While it is technically possible to write an XML parser in XSLT (XSLT being a turing complete language), you definitely do not want to go there. – ndim Mar 07 '11 at 15:23
  • I want solution and I want write script from core. – Damian Czapiewski Mar 07 '11 at 16:40
  • Damian, which versions of IE do you target? To load XML documents cross-browser since IE 7 you can use XMLHttpRequest e.g. `var req = new XMLHttpRequest(); req.open('GET', 'data.xml', false); req.send(null); var xml = req.response.XML;`. That should work with Chrome too. Applying XSLT with Chrome should work as with Mozilla (i.e. with new XSLTProcessor()), with IE you can use the transformNode method (http://msdn.microsoft.com/en-us/library/ms761399%28v=VS.85%29.aspx) on the responseXML you get, that gives you a string of the XSLT result you can then insert with insertAdjacentHTML. – Martin Honnen Mar 07 '11 at 17:22
  • Martin, I know about XMLHttpRequest, but I want use other object, eg. XSLTProcessor. And I don't know this object compatible with IE and/or Chrome. ;] – Damian Czapiewski Mar 07 '11 at 18:10
  • @Alejandro: Why do you remove the `xsl` or `xslt` tags from XSLT related questions? People subscribed to these tags won't see these questions if you remove them. – Max Toro Mar 07 '11 at 21:28
  • @Max Toro: This is a question about the invocation of an specific XSLT processor. Such questions are tagged as `xsltprocessor`. –  Mar 07 '11 at 21:32
  • @Alejandro: I know, the question is why? you are not helping anyone. Tags do not need to be super specific. In fact, this question has `xml` and `xsltprocessor`, so unless I want to browse all `xml` questions (very broad topic) or `xsltprocessor` (very specific) I won't see this question. – Max Toro Mar 07 '11 at 21:39
  • @Max Toro: If you want to discuss what's the better approach you can post a question on http://meta.stackoverflow.com/ . I don't think that the site intention is to bring visibility by tags. –  Mar 07 '11 at 21:49
  • @Alejandro: Just let people use the tags they want, there's no 'better approach'. – Max Toro Mar 08 '11 at 01:45

2 Answers2

3

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

Gregory Nozik
  • 3,296
  • 3
  • 32
  • 47
  • Could xslt object just use Microsoft.XMLDOM? If not does MSXML2.FreeThreadedDOMDocument have an inline, loadFromString type method? – 1.21 gigawatts Mar 24 '17 at 22:41
0

This is what I'm using, it's cross-browser compatible and you've got access to the source code as well in case there are any issues.

http://johannburkard.de/software/xsltjs/

daz-fuller
  • 1,191
  • 1
  • 10
  • 18
  • A framework or library is definitely a good choice for cross-browser compatibility of XSL transforms. Last I checked there weren't a ton of these, another one worth looking into is Sarissa (http://dev.abiss.gr/sarissa/) – Matt Molnar Mar 07 '11 at 21:16