如何使用 XSLT 转换 XML 文档并在 IE 中添加参数?
我刚开始学习 XSLT,并且遇到了一些我不太理解的东西。我需要在转换文档之前添加 XSLT 参数。我可以为非 IE 浏览器执行此操作,如下所示:
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);
}
}
现在,我可以为非 IE 浏览器执行此操作,创建一个新的 XSLT Processor 对象,导入样式表,然后只需在转换过程之前添加参数即可。不过,对于 IE 版本的代码来说,这一切似乎都没有发生,而且我不能简单地在转换之前添加参数。我在 google 上进行了大量搜索,看到不同的东西告诉我创建各种不同 MSXML 版本的新 ActiveX 对象,整个事件让我深感困惑。
使用上面的代码,我该怎么做:xsltProcessor.setParameter(null,"PARAMNAME","PARAMVALUE");
除了 IE 之外,如果可能的话,有人可以解释 IE 如何与 FF/O/C/其他文明浏览器不同地处理 XSLT 的整个概念吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用 Sarissa,它是一个提供跨浏览器 XSLT 转换 API 的抽象层。
You might try using Sarissa, which is an abstraction layer that provides a cross-browser XSLT transformation API.
根据此页面,您可以使用
XSLTProcessor
所在的位置创建与转换调用也不同。
不管怎样,看起来对你所要求的内容有一个非常详细的解释。
我不久前进行了跨浏览器 XSLT 转换,这是我使用的代码。
createDocument
只是一个返回 DOM 文档的函数。我没有做样式表参数,所以也许这有点偏离主题,但无论如何它适用于 IE6+ 和 Firefox 1.5+。According to this page, you can use
where
XSLTProcessor
is created withThe transform call is also different.
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+.