Re: [xsl] Dynamic processing of xml file using xsl and javascript

Subject: Re: [xsl] Dynamic processing of xml file using xsl and javascript
From: Geert Josten <Geert.Josten@xxxxxxxxxxx>
Date: Fri, 29 Oct 2004 21:41:07 +0200
Hi,

Combining the attempt of Matthew and Anton, I come to the following, which is nearly there to
support MS IE as well (if anyone would want that).

Note that the transformNodeToObject for iebrowser does not seem to work, but I'm not familiar enough
to get it working quickly. Anyone?

<script type="text/javascript" language="JavaScript">
   var iebrowser = false;
   var nsbrowser = false;

   if (document.implementation && document.implementation.createDocument) {
     nsbrowser = true;
   } else if (window.ActiveXObject) {
     iebrowser = true
   } else {
     alert('Your browser can\'t handle this script');
   }

var processor = createProcessor();

   var dataXML = createDocument();
   if (dataXML) {
     dataXML.load("data.xml");
   }

   var dataXSL = createDocument();
   if (dataXSL) {
     if (nsbrowser) {
       dataXSL.addEventListener("load", onload, false);
     }
     dataXSL.load("data.xsl");
   }

   function createDocument() {
     var doc = null;
     if (nsbrowser) {
       doc = document.implementation.createDocument("", "", null);
     } else if (iebrowser) {
       doc = new ActiveXObject("Microsoft.XMLDOM");
     }
     return doc;
   }

   function createProcessor() {
     var proc = null;
     if (nsbrowser) {
       proc = new XSLTProcessor();
     }
     return proc;
   }

   function onload()
   {
     if (nsbrowser) {
       processor.importStylesheet(dataXSL);
     }
   }

   function XSLTransform(outputElement)
   {
     if (nsbrowser) {
       var ownerDocument = createDocument();
       var newFragment = processor.transformToFragment(dataXML, ownerDocument);
       var oldLink = document.getElementById("xsllink");
       document.getElementById(outputElement).replaceChild(newFragment, oldLink);
     } else if (iebrowser) {
       //var newFragment = createDocument();
       //dataXML.transformNodeToNode(dataXSL, newFragment);
       var newFragment = dataXML.transformNode(dataXSL));
       var oldLink = document.getElementById("xsllink");
       document.getElementById(outputElement).replaceChild(newFragment, oldLink);
     }
   }

</script>

Grtz,
Geert

Current Thread