本文介绍了XSLT无法在IE 11上运行,无法转换xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试呈现来自API的XSLT样式表,认为它在Chrome,FF(除IE之外)上都能正常工作.

Trying to render XSLT stylesheet that's coming from API, thought it's working fine on Chrome, FF except IE.

我尝试使用w3c中的示例,该示例可以工作,但是从文件中调用XML和XSLT,因为我的文件来自AJAX调用成功响应.

I tried using the example from w3c which works but that's calling the XML and XSLT from a file, where as mine is coming from AJAX call success response.

W3school示例 XSLT示例

W3school sample XSLT sample

我的版本是这个

function getJson() {
 $.get(url)..
 var json2XMLResult = J2XML.json2xml_str(data);
 getResultXsl(json2XMLResult )
}

function getResultXsl(json2xml) {
    $.get(url)
        .then(function (data) {
            let resDefinition = data.Results.ResponseDisplayDefinition;
            let xmlString = '<?xml version="1.0"?><Response>' + json2xml + '</Response>';
            if (typeof DOMParser != "undefined") {
                parseXml = function (xmlStr) {
                    return (new DOMParser()).parseFromString(xmlStr, "text/xml");
                };
            }
            else if (typeof ActiveXObject != "undefined" &&
                new ActiveXObject("Microsoft.XMLDOM")) {
                parseXml = function (xmlStr) {
                    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async = "false";
                    xmlDoc.loadXML(xmlStr);
                    return xmlDoc;
                };
            }

            else {
                throw new Error("No XML parser found");
            }

            displayResult(xmlString, resDefinition);
        })
}

在html中显示XSLT时,下面的 alert()确实显示了您是要在Chrome还是IE上呈现它,

Displaying XSLT in the html, the alert() below does show whether you're trying to render it on Chrome or IE,

function displayResult(xmlStrToConvert, xslStrToConvert) {
    var xmlConverted = parseXml(xmlStrToConvert);
    var xslConverted = parseXml(xslStrToConvert);
    if (window.ActiveXObject || "ActiveXObject" in window) {
        alert('It is IE but not showing anything');
        var ex = xmlConverted.transformNode(xslConverted)
        $('#xmlJson').append(ex);
    } else {
        alert('its not IE');
        // code for Chrome, Firefox, Opera, etc.
        var xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(xslConverted);
        var resultDocument = xsltProcessor.transformToFragment(xmlConverted, document);
        $('#xmlJson').append(resultDocument);

    }
}

还尝试了 var ex = xmlConverted.transformToFragment(xslConverted,document);

有人可以指出这是怎么回事吗?也无法在IE11上打开开发工具,这很难调试,但是我可以通过上面的代码告诉它出了点问题.

Can someone point-out what's wrong with this? Also couldn't open dev tool on IE11 which is harder to debug, but I can tell its something wrong with my code above.

编辑 尽管 transformNode()返回 Object不支持属性或方法'transformNode' XSLTProcessor()未定义

function transformXML(json2xml) {
$.ajax({
    type: 'GET',
    url: window.parent.__env.apiManagement + 'Preview/TypeDefinition?objectName=' + apiObjectResponse,
    beforeSend: function (xhr, settings) {
        if (window.ActiveXObject) {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        }
        else {
            xhr = new XMLHttpRequest();
        }
        try { xhr.responseType = "msxml-document"; } catch (err) { }
    },
    success: function (data, status, xhr) {
        var parseXml = new DOMParser();
        var xslStylesheet = parseXml.parseFromString(data.Results.ResponseDisplayDefinition, "text/xml");
        var xmlString = '<?xml version="1.0"?><Response>' + json2xml + '</Response>';
        var convertedXML = parseXml.parseFromString(xmlString, "text/xml");

        // // cross-browser logic omitted for simplicity
        if(window.ActiveXObject || xhr.responseType == "msxml-document") {
            var ex = convertedXML.transformNode(xslStylesheet);
            console.log('>>> ', convertedXML)
            alert(xmlString)
            $('#xmlJson').append(ex);
        }
        // code for Chrome, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument) {
            var xsltProcessor = new XSLTProcessor();
            xsltProcessor.importStylesheet(xslStylesheet);
            var resultDocument = xsltProcessor.transformToFragment(convertedXML, document);
            $('#xmlJson').append(resultDocument);
        }

    }
});

}

推荐答案

IE 11支持DOMParser,但使用它可以构建不支持XSLT的IE XML DOM文档.因此,您至少需要更改检查的顺序,如果您要为IE进行编码并且要执行XSLT,则请确保使用ActiveXObject创建MSXML DOM文档,然后可以在其上使用 transformNode .

IE 11 supports DOMParser but using it builds an IE XML DOM document which does not have any support for XSLT. So you at least need to change the order of checks, if you are coding for IE and want to do XSLT then make sure you create an MSXML DOM document using ActiveXObject, then you can use transformNode on it.

由于您似乎想从字符串中解析XML和XSLT,然后使用客户端XSLT转换,我建议使用类似 https://martin-honnen.github.io/xslt/2016/test2016123001.html ,它可以

As you seem to want to parse XML and XSLT from strings and then use client-side XSLT transformation I would suggest to use an approach like in https://martin-honnen.github.io/xslt/2016/test2016123001.html, which does

  function parseXmlStringForTransformation(xml) {
      try {
          var doc = new ActiveXObject('Msxml2.DOMDocument.6.0');
          doc.loadXML(xml);
          return doc;
      }
      catch (e) {
          var domParser = new DOMParser();
          var doc = domParser.parseFromString(xml, 'application/xml');
          return doc;
     }
  }

,然后使用受支持的XSLTProcessor或相应的MSXML 6 ActiveX XSLT API来运行转换:

and then uses XSLTProcessor where supported or the corresponding MSXML 6 ActiveX XSLT API to run the transformation:

  function transform(xmlDoc, xslDoc, xsltParams, targetElement) {
      if (typeof XSLTProcessor !== 'undefined') {
        var proc = new XSLTProcessor();
        proc.importStylesheet(xslDoc);

        for (var prop in xsltParams) {
          proc.setParameter(null, prop, xsltParams[prop]);
        }

        var resultFrag = proc.transformToFragment(xmlDoc, targetElement.ownerDocument);

        targetElement.textContent = '';
        targetElement.appendChild(resultFrag);
      }
      else {
          var template = new ActiveXObject('Msxml2.XslTemplate.6.0');
          template.stylesheet = xslDoc;
          var proc = template.createProcessor();

          for (var prop in xsltParams) {
            proc.addParameter(prop, xsltParams[prop]);
          }

          proc.input = xmlDoc;

          proc.transform();

          var resultHTML = proc.output;

          targetElement.innerHTML = resultHTML;
      }
  }

然后您可以像在

  document.addEventListener('DOMContentLoaded', function() {
    transform(
        parseXmlStringForTransformation('<root>...<\/root>'),
        parseXmlStringForTransformation('<xsl:stylesheet ...>...<\/xsl:stylesheet>'),
        { }, // empty parameter object if you don't want to pass parameters from Javascript to XSLT
        document.getElementById('d1')  // target element in your HTML to insert the transformation result into
    );
  })

这篇关于XSLT无法在IE 11上运行,无法转换xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:24