下面的代码有效,但是当我将XML文件activities.xml
的相对路径位置从Gitbub https://raw.githubusercontent.com/Podatus/web-files/master/activities.xml
更改为绝对文件时,它不起作用,出现此错误:
TypeError:XSLTProcessor.transformToFragment的参数1不是对象。
看起来他无法加载XML
function loadXMLDoc(filename){
if (window.ActiveXObject){
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", filename, false);
try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}
function displayResult(xslFileName){
xml = loadXMLDoc("activities.xml");
xsl = loadXMLDoc(xslFileName);
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
ex = xml.transformNode(xsl);
document.getElementById("showData").innerHTML = ex;
}
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
var temp = document.getElementById("showData") ;
while(temp.firstChild){ // delete all ancient data
temp.removeChild(temp.firstChild);
resultDocument = xsltProcessor.transformToFragment(xml, document);
}
document.getElementById("showData").appendChild(resultDocument);
}
}