0

我需要在 ie9 或更高版本中使用 xslt 将一个 xml 文档转换为另一个 xml 文档。

我正在尝试在 ie9 中使用 xslt 转换 xml 文档。当我使用 transformNode() 函数时,它在 ie8(code:: resultDocument = XML.transformNode(XSL);) 中工作正常,但在 ie9 中未定义 transformNode 函数,显示错误:: SCRIPT438: Object doesn't support property or method'转换节点'

我找到了 ie9 的解决方案,如下所示

if (window.ActiveXObject) {
                console.log('inside hi');
                var xslt = new ActiveXObject("Msxml2.XSLTemplate");
                var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
                xslDoc.loadXML(xsltDoc.xml);
                console.log(xslt.styleSheet);
                xslt.stylesheet = xslDoc;
                var xslProc = xslt.createProcessor();
                xslProc.input = xmlDoc;
                xslProc.transform();
                return xslProc.output;
            }

但是当我运行它时,我得到一个错误:SCRIPT16389:样式表不包含文档元素。样式表可能是空的,也可能不是格式良好的 XML 文档。

我是 javascript/jquery 的新手。谁能帮我解决这个问题。如果在 javascript 或 jquery 中有任何其他功能,那将会很有帮助。

提前致谢

4

2 回答 2

1

对于早期版本的 IE,该responseXML文档曾经是一个 MSXML DOM 文档,而 MSXML 实现了 XSLT 和transformNode. 对于较新的 IE 版本,该responseXML文档为您提供 IE DOM 文档,而 IE 不实现 XSLTtransformNode及其 DOM 文档/节点。IE DOM 文档也没有xml您尝试在xslDoc.loadXML(xsltDoc.xml);.

尝试将那部分代码更改为

if (typeof XMLSerializer !== 'undefined') {
  xslDoc.loadXML(new XMLSerializer().serializeToString(xsltDoc));
  // now use xslDoc here
}

xslDoc.loadXML(xmlHttp.responseText);如果您仍然可以访问 XMLHttpRequest,则可以使用不同的选项。还有一个选项可以确保您获得 MSXML responseXML,请参阅http://blogs.msdn.com/b/ie/archive/2012/07/19/xmlhttprequest-responsexml-in-ie10-release-preview 中try { xhr.responseType = 'msxml-document'; } catch(e){}的行。 .aspx _

您在代码中检查对象的整个方法是错误的,请检查您要使用的对象或属性或方法(例如if (typeof XSLTProcessor !== 'undefined') { // now use XSLTProcessor here }),而不是完全不同的对象,例如document.implementation.

于 2013-09-16T12:24:06.727 回答
0

SCRIPT16389: The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document在 IE9/10/11 中也遇到了错误。我发现以下修复它:

你的代码:

if (window.ActiveXObject) {
    console.log('inside hi');
    var xslt = new ActiveXObject("Msxml2.XSLTemplate");
    var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");

    xslDoc.loadXML(xsltDoc.xml);

    console.log(xslt.styleSheet);
    xslt.stylesheet = xslDoc;
    var xslProc = xslt.createProcessor();
    xslProc.input = xmlDoc;
    xslProc.transform();
    return xslProc.output;
 }

工作代码:

if (window.ActiveXObject) {
    console.log('inside hi');
    var xslt = new ActiveXObject("Msxml2.XSLTemplate");
    var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");

    xslDoc.load(xsltDoc);

    console.log(xslt.styleSheet);
    xslt.stylesheet = xslDoc;
    var xslProc = xslt.createProcessor();
    xslProc.input = xmlDoc;
    xslProc.transform();
    return xslProc.output;
 }

更改为第 6 行 - 将“loadXML”替换为“load”,将“xsltDoc.xml”替换为“xsltDoc”。让我知道事情的后续!

于 2013-10-04T17:04:08.823 回答