我需要在ie9或更高版本中使用xslt将xml转换为另一个xml。
我试图在ie9中使用xslt转换xml。当我使用transformNode()函数时,它在ie8中工作得很好(代码::resultDocument =XML.transformNode(XSL);但是在ie9 transformNode函数中没有定义显示错误::SCRIPT438: Object不支持属性或方法'transformNode' )。
我为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中还有其他函数,这将是有帮助的。
提前感谢
发布于 2013-09-16 12:24:06
对于早期版本的IE,responseXML文档过去是MSXML文档,MSXML实现了XSLT和transformNode。对于较新的IE版本,responseXML文档为您提供一个IE文档,而IE不为其DOM文档/节点实现XSLT和transformNode。IE文档也没有试图在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,请参阅try { xhr.responseType = 'msxml-document'; } catch(e){}行中的http://blogs.msdn.com/b/ie/archive/2012/07/19/xmlhttprequest-responsexml-in-ie10-release-preview.aspx。
在代码中检查对象的整个方法是错误的,检查要使用的对象或属性或方法(例如if (typeof XSLTProcessor !== 'undefined') { // now use XSLTProcessor here }),而不是document.implementation这样的完全不同的对象。
发布于 2013-10-04 17:04:08
我在IE9/10/11中也有SCRIPT16389: The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document错误。
你的代码:
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”。告诉我是怎么回事!
https://stackoverflow.com/questions/18771597
复制相似问题