Skip to content Skip to sidebar Skip to footer

How To Run Xsl File Using Javascript / Html File

i want to run xsl file using javascript function. I wrote a javascrpt function which is working well with Firefox and Crom but it is not working on Internet Explorer function

Solution 1:

To apply xsl trasformation I use, and promote, Sarissa. It is a crossbrowser library that envelope the xml apis of the different browser.

For an example of a trasformation with Sarissa you can head straight to the howto page but it is similar to this:

<!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>sarissa test1</title><scripttype="text/javascript"src="sarissa.js"></script></head><body><scripttype="text/javascript">functionloadXMLDoc(dname) {
       xhttp = newXMLHttpRequest();
       xhttp.open("GET",dname,false);
       xhttp.send("");
      return xhttp.responseXML;
     }

     var processor = newXSLTProcessor();
     var theXML = loadXMLDoc('xml.xml');
     var theXSL = loadXMLDoc('xsl.xsl');

     // prepare the processor
     processor.importStylesheet(theXSL);

     var theResult = processor.transformToDocument(theXML);
     // now you have a DomDocument with the result// if you want to serialize (transform to a string) it you van usedocument.write(newXMLSerializer().serializeToString(theResult));  
  </script></body></html>

Quick, easy, mantained and you can focus your efforts to the xsl creation instead than struggling to overcome the browsers differences.

The single glitch, to me is a plus but someone may see it as a limitation, the code is released under GPL 2.1 or higher or, if you prefer Apache Licence 2.0 or higher

Edit: Mine fault, I posted an old (and wrong code) without checking it. Created a new version miming your script (and tested it) on firefox, chrome, ie8, ie7 and it worked flawlessy. I used the first two xsl/xml found on a google search (I report them below for completeness). Try the code as is and with your xsl/xml. If it goes wrong report the malfunction too (we can be more effective with a deeper error description than - don't work - ). What happens when the code misbehave? (the browser freeze, report an error, return a blank result)

xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!--?xml-stylesheet type="text/xsl" href="xsl.xsl"?--><tutorials><tutorial><name>XML Tutorial</name><url>http://www.quackit.com/xml/tutorial</url></tutorial><tutorial><name>HTML Tutorial</name><url>http://www.quackit.com/html/tutorial</url></tutorial></tutorials>

xsl:

<?xml version="1.0"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><!-- try to enable/disable the xsl:output --><!-- xsl:output method="xml" 
                version="1.0" 
                encoding="iso-8859-1" 
                indent="yes"/--><xsl:templatematch="/"><html><head><title>XML XSL Example</title><styletype="text/css">body
{
margin:10px;
background-color:#ccff00;
font-family:verdana,helvetica,sans-serif;
}

.tutorial-name
{
display:block;
font-weight:bold;
}

.tutorial-url
{
display:block;
color:#636363;
font-size:small;
font-style:italic;
}
</style></head><body><h2>Cool Tutorials</h2><p>Hey, check out these tutorials!</p><xsl:apply-templates/></body></html></xsl:template><xsl:templatematch="tutorial"><spanclass="tutorial-name"><xsl:value-ofselect="name"/></span><spanclass="tutorial-url"><xsl:value-ofselect="url"/></span></xsl:template></xsl:stylesheet>

Post a Comment for "How To Run Xsl File Using Javascript / Html File"