How do I add/insert an xsl:param into an xsl stylesheet before transform?
Given the following code (and not using jQuery) what would be a good way to dynamically insert an tag into the xsl before transformation? I would like someone to "fix" function addParam
function addParam(xsl,name,value) {
/** input:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">.....
output:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="parm1" value="parameter number 1" />
<xsl:template match="/">.....
*/
var parameter = document.createElement("xsl:param");
parameter.setAttribute("name",name);
parameter.setA开发者_运维技巧ttribute("value",value);
xsl.documentElement.insertBefore(parameter,xsl.documentElement.firstsChild)
}
function displayResult(pXml) {
var xsl = loadXMLDoc("cdcatalog.xsl");
// code for IE
if (window.ActiveXObject) {
/* I want to call it here >>> */
addParam(xsl,"parm1","parameter value1");
var ex = pXml.transformNode(xsl);
document.getElementById("availableSearchItems").innerHTML = ex;
}
// code for Mozilla, Firefox, Opera, Chrome, etc.
else if (document.implementation && document.implementation.createDocument) {
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
/* I want to call it here */
addParam(xsl,"parm1","parameter value1");
resultDocument = xsltProcessor.transformToFragment(pXml, document);
document.getElementById('availableSearchItems').innerHTML = "";
document.getElementById("availableSearchItems").appendChild(resultDocument);
}
}
I have read how to pass a parameter from an URL to a XSL stylesheet using jQuery? and understand that Firefox can do setParameter and IE can do
var strParam = "//xsl:param[@name='" + p + "']";
var xslParam = xObj.selectSingleNode(strParam);
xslParam.setAttribute("select",op[p]);
but that is assuming the xsl has a parameter already.
Pointers and corrections very welcome. Thanks
Why do you want to change the parameter name in the xsl ? Surely that could be static and you change what the value of that parameter is? You could then set what you want to pass into the xsl in the JavaScript and do it that way and its easy.
<xsl:param name="parm1"/>
Then that gets a value when you call the add and set parameter functions as show below. You could even test to make sure its not null before passing it through.
//code for ie
if (window.ActiveXObject)
{
var xslt = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
xslt.async = false;
xslt.load(xsl);
var template = new ActiveXObject("MSXML2.XSLTemplate");
template.stylesheet = xslt;
var process = template.createProcessor();
process.input = xml;
if(yourparam != ""){
process.addParameter("paramID", yourparam);
}
process.transform();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var xml_serializer = new XMLSerializer();
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
if(yourparam != ""){
xsltProcessor.setParameter(null, "paramID", yourparam);
}
}
精彩评论