[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] XSLT interacting with Javascript


Subject: [xsl] XSLT interacting with Javascript
From: Robert G <nyjets3422@xxxxxxxxx>
Date: Tue, 2 Jun 2009 10:18:48 -0700 (PDT)

I have a single html page start with random information on it, then replaces a <div> with new info, which it gets via client-side javascript. The Javascript takes my XSL and XML file and transforms it fine but when I want to add more javascript to using my xsl file it puts the javascript in the html page code but is not runnable. I have tried a bunch of things to get the javascript to work but nothing seems to work. Only the html pages javascript works. I am trying to get this to work on Firefox.

Heres the code. In this example, the button that is on the screen does not work, and the error console says the function is not defined. I have tried putting CDATA tag around the javascript, and tried setting trythis=function() but none of that worked.

HTML Page

<html>

<head>
<script>
function displayResult()
{
// code for IE
if (window.ActiveXObject)
  {
       **** TRYING ON FIREFOX FIRST*****************
  }
           
 // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument)
{
              
    xml=document.implementation.createDocument("","",null);
    xml.async=false;
    xml.load("test.xml");

    xsl = document.implementation.createDocument("","",null);
    xsl.async=false;
    xsl.load("newer.xsl");
        
    var xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    xsltProcessor.setParameter(null,"type","html");
    xsltProcessor.setParameter(null,"type2","java"); 
  
    var result= xsltProcessor.transformToDocument(xml);
    var serial= new XMLSerializer();
    var out= serial.serializeToString(result.documentElement);
    var output= document.getElementById("example");
    output.innerHTML=out;
               
}
}
</script>
<title>Another Test Page </title>
</head>

<body onLoad='displayResult()'>
<p>Before change <p>

<div id="example">THis is GONE! </div>

<p>After change </p>

</body>
</html>



XSL file

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="type"/>
<xsl:param name="type2"/>
<xsl:output method="html"/>
<xsl:template match="/">
                
                <xsl:apply-templates select="page/content"/>

</xsl:template>
        <xsl:template match="content">
        
         <xsl:if test="$type2 = 'java'">
                <script type="text/javascript">
                function trythis(){
                        alert("IT WORKS!") 
                }
                </script>
          </xsl:if>
          
          <xsl:if test="$type = 'html'">
                <h2> Heres a Page </h2>
                <p> A paragraph </p>
                <p> More paragraphs </p>
                <input type="button" value='try' onclick='trythis()'/>             
          </xsl:if>
        
        </xsl:template>
</xsl:stylesheet>


Current Thread
Keywords