如何使用 XSLT 转换 XML 文档并在 IE 中添加参数?

发布于 2024-10-29 16:27:36 字数 1374 浏览 1 评论 0原文

我刚开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨小沫ゞ 2024-11-05 16:27:36

您可以尝试使用 Sarissa,它是一个提供跨浏览器 XSLT 转换 API 的抽象层。

You might try using Sarissa, which is an abstraction layer that provides a cross-browser XSLT transformation API.

任性一次 2024-11-05 16:27:36

根据此页面,您可以使用

XSLTProcessor.addParameter("Parameter Name", "Parameter Value");

XSLTProcessor所在的位置创建与

var XSLTCompiled = new ActiveXObject("MSXML2.XSLTemplate");
XSLTCompiled.stylesheet = XSL.documentElement;
var XSLTProcessor = XSLTCompiled.createProcessor();

转换调用也不同。

XSLTProcessor.transform();

不管怎样,看起来对你所要求的内容有一个非常详细的解释。

我不久前进行了跨浏览器 XSLT 转换,这是我使用的代码。 createDocument 只是一个返回 DOM 文档的函数。我没有做样式表参数,所以也许这有点偏离主题,但无论如何它适用于 IE6+ 和 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";
}

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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文