I'm new to learning XSLT, and I've come across something I really don't quite understand. I need to add an XSLT parameter before transforming the document. I can do this for non-IE browsers like so:
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("cdcatalog.xml");
xsl = loadXMLDoc("cdcatalog.xsl");
// code for IE
if (window.ActiveXObject) {
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);
}
}
Now, I can do it for non-IE browsers, a new XSLT Processor object is made, stylesheet imported, and you simply add the parameter before the transformation process. None of this seems to be happening for the IE version of the code though, and I can't simply add the parameter before the transformation. I've googled profusely and seen different things telling me to create new ActiveX objects of various different MSXML versions, and I'm deeply confused by the whole affair.
开发者_开发技巧Taking the above code, how do I do this:
xsltProcessor.setParameter(null,"PARAMNAME","PARAMVALUE");
except for IE and if possible, can someone explain how IE deals with the whole concept of XSLT differently to FF/O/C/other civilised browsers?You might try using Sarissa, which is an abstraction layer that provides a cross-browser XSLT transformation API.
According to this page, you can use
XSLTProcessor.addParameter("Parameter Name", "Parameter Value");
where XSLTProcessor
is created with
var XSLTCompiled = new ActiveXObject("MSXML2.XSLTemplate");
XSLTCompiled.stylesheet = XSL.documentElement;
var XSLTProcessor = XSLTCompiled.createProcessor();
The transform call is also different.
XSLTProcessor.transform();
Anyway, it looks like there's a quite elaborate explanation of what you're asking for.
I did cross-browser XSLT transforms a while back, and here's the code I used. createDocument
is just a function to return a DOM document. I didn't do stylesheet parameters, so maybe this is a little off-topic, but anyway it works on IE6+ and Firefox 1.5+.
// arguments can be string (uri of document) or document node
function xslTransform( content, transform, options )
{
if ("string" == typeof content) content = createDocument( content );
if ("string" == typeof transform) transform = createDocument( transform );
var targetEle;
if (options && options.target) targetEle = document.getElementById(options.target);
if (targetEle && options.replace)
while (targetEle.hasChildNodes())
targetEle.removeChild( targetEle.firstChild );
if (window.XSLTProcessor)
{
var processor = new XSLTProcessor();
processor.importStylesheet( transform );
var frag = processor.transformToFragment( content, document );
if (targetEle)
targetEle.appendChild( frag );
}
else if (window.ActiveXObject)
{
if (targetEle)
targetEle.innerHTML = content.transformNode( transform );
}
else return "XSLT not supported";
}
精彩评论