XSLT processing on IE11?

2019-04-29 20:24发布

What has happened to the XSLT processing in IE11?

On IE8/9/10, you can use:

if (window.ActiveXObject) {
    var xslt = new ActiveXObject("Msxml2.XSLTemplate");
    ....
}

On Chrome/Firefox/Safari, you can use:

else { 
    var xsltProcessor = new XSLTProcessor();
}

But on IE11, neither of these are supported. Does anyone know how this can be accomplished?

6条回答
【Aperson】
2楼-- · 2019-04-29 20:51

For me running site in a compatibility mode in IE - 11 solved the issue....

Note : This might not be a solution , but I was in a situation where one if my old site was using above mentioned code. But I'm not in a position to Re-code the site

查看更多
祖国的老花朵
3楼-- · 2019-04-29 20:52

Try

if (window.ActiveXObject || "ActiveXObject" in window)

This worked for me working with IE11 and allowed me to instantiate ActiveX objects since the standard old check was being bypassed.

查看更多
Viruses.
4楼-- · 2019-04-29 20:56

You could consider Saxon CE, an XSLT 2.0 processor implemented entirely in JavaScript. This would give you a consistent API across all browsers and would allow you to code using the more powerful XSLT 2.0 language rather than 1.0.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-04-29 21:00

You Can use ("ActiveXObject" in window) which will allow all the IE browsers to come inside the if condition . Exp :-

if ("ActiveXObject" in window) {
// Internet Explorer For all versions like IE8 , IE9 , IE10 or IE11 etc
}else{
// code for Mozilla, Firefox, Opera, etc.
        }
查看更多
Emotional °昔
6楼-- · 2019-04-29 21:03

The reason if(window.ActiveXObject) fails in IE11 is because for some reason window.ActiveXObject has become falsy, even though it is still a function. I've taken to being more explicit in my feature detection:

if(window.ActiveXObject !== undefined){
    ...
}

This approach also covers the case of checking for attributes that are present but not set to a truthy value:

if(document.createElement("span").draggable !== undefined){
    ...
}
查看更多
疯言疯语
7楼-- · 2019-04-29 21:05

This works for me on Chrome/Edge/Firefox/IE11

 function loadXMLDoc(filename) {
     if (window.ActiveXObject || "ActiveXObject" in window) {
         xhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } else {
         xhttp = new XMLHttpRequest();
     }
     xhttp.open("GET", filename, false);
     xhttp.send("");
     return xhttp.responseXML;
 }


 if (window.ActiveXObject || "ActiveXObject" in window) {
     ie();
 } else {

     xml = loadXMLDoc("input.xml");
     xsl = loadXMLDoc("mmlctop2_0.xsl");

     if (document.implementation && document.implementation.createDocument) {
         xsltProcessor = new XSLTProcessor();
         xsltProcessor.importStylesheet(xsl);
         resultDocument = xsltProcessor.transformToDocument(xml, document);

         var serializer = new XMLSerializer();
         var transformed = serializer.serializeToString(resultDocument.documentElement);

         alert(transformed);
     }
 }

 // https://msdn.microsoft.com/en-us/library/ms753809(v=vs.85).aspx
 function ie() {

     var xslt = new ActiveXObject("Msxml2.XSLTemplate.3.0");
     var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");
     var xslProc;
     xslDoc.async = false;
     xslDoc.load("mmlctop2_0.xsl");
     if (xslDoc.parseError.errorCode != 0) {
         var myErr = xslDoc.parseError;
         alert("You have error " + myErr.reason);
     } else {
         xslt.stylesheet = xslDoc;
         var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
         xmlDoc.async = false;
         xmlDoc.load("input.xml");
         if (xmlDoc.parseError.errorCode != 0) {
             var myErr = xmlDoc.parseError;
             alert("You have error " + myErr.reason);
         } else {
             xslProc = xslt.createProcessor();
             xslProc.input = xmlDoc;
             xslProc.addParameter("param1", "Hello");
             xslProc.transform();
             alert(xslProc.output);
         }
     }


 }
查看更多
登录 后发表回答