对不起伙计们。 我知道这个问题已被提出,但我只能找到本地加载的xml文件的答案。
我需要将带有一个参数的样式表应用于xml文档。这两个文件都是通过XHR从远程服务器检索的。
在Firefox中它运行正常。我只有IE的问题。
这是我的javascript代码:
var data = loadXMLDoc("dataset_L190_2012.xml");
var styleSheet = loadXMLDoc("dataset_L190.xsl");;
// on document ready the xml gets displyed with no parameter set
// "datiL190" is the destination <div> for the parsed output
$(document).ready(function() {
$("#datiL190").append(getParsedXML(data, styleSheet));
});
// when user clicks button the xml document should get displayed with parameter set
function searchButtonClick(form) {
$("#datiL190").empty();
$("#datiL190").append(getParsedXML(data, styleSheet));
}
// remotelly load xml from supplyed address
function loadXMLDoc(dname) {
var xhttp = null ;
if (window.ActiveXObject) {
xhttp = new ActiveXObject("Msxml2.XMLHTTP.6.0");
} else {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", dname, false);
xhttp.send("");
return xhttp.responseXML;
}
// "merges" xml and xsl document to get html output
function getParsedXML(xmlDoc, xslDoc) {
var result = null ;
// $("#settore") is a text input that contains the parameter value
var settore = $("#settore").val() ;
// code for IE
if (window.ActiveXObject) {
// This would be OK if I had no parameters
// result = xmlDoc.transformNode(xslDoc);
// But I have just 1 parameter, so I need a free threaded document
var xsl = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");
// and a xslt processor that uses the xslDoc to transform the xmlDoc
var xslt = new ActiveXObject("Msxml2.XSLTemplate.6.0");
// xsl is empty: how do I fill it with xslDoc?
xslt.stylesheet = xsl;
// if I assign xslDoc to xslt.stylesheet directly I get this error:
// SCRIPT16389: The XSL stylesheet document must be free threaded
// in order to be used with the XSLTemplate object.
// xslt.stylesheet = xslDoc;
var xslproc = xslt.createProcessor();
xslproc.input = xmlDoc;
xslproc.addParameter("settore", settore);
xslproc.transform();
result = xslproc.output ;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
xsltProcessor.setParameter(null, "settore", settore);
result = xsltProcessor.transformToFragment(xmlDoc, document);
}
return result ;
}
感谢您的帮助。
答案 0 :(得分:0)
基于此页面:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms762312%28v=vs.85%29.aspx
您可以通过这种方式填充xsl
变量,并且我已经确认它确实有效:
// But I have just 1 parameter, so I need a free threaded document
var xsl = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");
// and a xslt processor that uses the xslDoc to transform the xmlDoc
var xslt = new ActiveXObject("Msxml2.XSLTemplate.6.0");
xsl.async = false;
xsl.load("dataset_L190.xsl");
xslt.stylesheet = xsl;