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:
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:
...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:
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:
There's more detail in the API section of the Saxon-CE documentation