简体   繁体   中英

XSL parameters using JavaScript

I have a html page like:

<html>
<head>
<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

function displayResult()
{
xml=loadXMLDoc("1.xml");
xsl=loadXMLDoc("2.xsl");
// code for IE
if (window.ActiveXObject)
  {
  xml.addParameter("rss", test); 
  ex=xml.transformNode(xsl);
  document.getElementById("example").innerHTML=ex;
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xsltProcessor=new XSLTProcessor();
  xsltProcessor.importStylesheet(xsl);
  resultDocument = xsltProcessor.transformToFragment(xml,document);
  document.getElementById("example").appendChild(resultDocument);
  }
}
</script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html>

Now I would like to pass a variable (javascript) to the XSL like for example "course_name" so that in the XSL I can use it like

<xsl:param name="course_name" />
<xsl:value-of select="$course_name" />

Any help with the approach to solve this problem is highly appreciated.

You can do it like this:

xsltProcessor.setParameter(null, "course_name", "whatever the name is");

and for IE, you will need to change your code as per http://msdn.microsoft.com/en-us/library/windows/desktop/ms762312%28v=vs.85%29.aspx , but the command is:

xslproc.addParameter("param1", "Hello");

...but given that you added "xslt-2.0" as a tag, you should know that built-in XSLT support in browsers only goes up to XSL 1.0.

The following may be of additional help:

The Saxon-CE XSLT 2.0 processor runs in the browser as a JavaScript library/framework. Because its JavaScript there's a high level of interoperability, so you can pass parameters to the XSLT processor, but you can also call JavaScript functions from the XSLT, which gives added flexibility.

When passing values across, arrays in JavaScript correspond to XSLT 2.0 sequences, but some care is still required to ensure types are preserved across the language boundary.

The JavaScript API for Saxon-CE has 2 distinct forms of syntax - one is the same as that in your code, but below is an example of the alternate syntax - the same code runs on all the major browsers:

<!DOCTYPE html>
   <html>
      <head>
         <title>stacko</title>
         <script type="text/javascript" language="javascript"
                       src="../Saxonce/Saxonce.nocache.js"></script>
         <script type="text/javascript" language="javascript">

         var my_course = "physics";

         onSaxonLoad = function() {   
            proc = Saxon.run( {
            stylesheet:   'stacko.xsl',
            source:       'stacko.xml',
            parameters: {
                           course_name: my_course,
                           tutor:       "Mr smith"
                        }  
             } );
         }
         </script>
       </head>
       <body>
          <div id="example"></div>
       </body>
    </html>

There's no need to return the result fragment back to JavaScript with this processor (though you can if necessary), because the XSLT 2.0 xsl:result-document instruction has been extended so it can use an href attribute that specifies the id of the element in the HTML page to update, for example:

<xsl:result-document href="#example" method="replace-content">
    <p><xsl:value-of select="ixsl:source()/books/book[@title = 'One']/></p>
</xsl:result-document>

There's more detail in the API section of the Saxon-CE documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM