通过(JavaScript)将样式表(XSL)应用于IE中的(XML)时出现问题

通过(JavaScript)将样式表(XSL)应用于IE中的(XML)时出现问题,javascript,xml,internet-explorer,xslt,Javascript,Xml,Internet Explorer,Xslt,这在Firefox中有效,但在IE中不起作用。IE抱怨第18行: docCache.stylesheet = xsl; 报告说: Message: The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document. 以下是函数: function display(dname, compt) { doc

这在Firefox中有效,但在IE中不起作用。IE抱怨第18行:

docCache.stylesheet = xsl;
报告说:

Message: The stylesheet does not contain a document element.  The stylesheet may be empty, or it may not be a well-formed XML document.
以下是函数:

function display(dname, compt) {    
document.getElementById(dname+"Table").innerHTML="";

// IE
if (window.ActiveXObject) {
    xml = new ActiveXObject("MSXML2.DOMDocument.3.0");
    xml.async = false;
    xml.load(dname+".xml");

    xsl = new ActiveXObject("MSXML2.FreeThreadedDOMDocument.3.0");
    xsl.async = false;
    xsl.load(dname+".xsl");

    docCache = new ActiveXObject("MSXML2.XSLTemplate.3.0");
    docCache.stylesheet = xsl;

    docProcessor = docCache.createProcessor();
    docProcessor.input = xml;

    docProcessor.addParameter("competitor", compt);

    docProcessor.transform();

    document.getElementById(dname+"Table").innerHTML = docProcessor.output;
}
// Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {

        xml=loadXMLDoc(dname+".xml");
        xsl=loadXMLDoc(dname+".xsl");
        xsltProcessor=new XSLTProcessor();
        xsltProcessor.importStylesheet(xsl);    
        xsltProcessor.setParameter(null, "competitor", compt);          
        resultDocument = xsltProcessor.transformToFragment(xml,document);
        document.getElementById(dname+"Table").appendChild(resultDocument);
}
}

和XSL文件:


参数值为:

有人知道为什么会发生这种情况吗?

该错误表明MSXML2在读取XSL(XML文件)时遇到问题。如果在IE中打开XSL文件,它是否完全正确地显示而没有错误

一个可能的原因是XSL文件使用了IE和MSXML2不会自动检测到的编码,并且在顶部没有包含如下XML编码语句:

<?xml version="1.0" encoding="Windows-1252"?>

也可能是XSL文件的实际编码与其XML编码语句不匹配


您还可以检查FireFox是否能够打开并完全显示XSL文件。如果文件格式不正确,FireFox将向您显示错误所在(而IE通常会向您显示问题所在,或者可能会向您完全显示其他内容)。

您是否从文件系统访问文件

如果您是从file://协议而不是http://加载文件,则浏览器安全限制可能会阻止您读取文件。它这样做是为了防止人们从您的文件系统读取其他(私有)文件


尝试在Web服务器上托管,看看您的错误是否消失。

随便猜一猜,您的样式表在第一个标记之前有一些空格?是的,有。但是清除空白没有任何作用:\n在
load
调用后,检查
xsl.parseError.errorCode
xsl.parseError.reason
,找出哪里出了问题。如果解析器发现错误,那么
errorCode
应该与0不同,并且
reason
应该告诉您错误消息。它说:系统错误:-2146697210。只是好奇,发生了什么问题,
xsl.parseError.reason
告诉了您什么?
<?xml version="1.0" encoding="Windows-1252"?>