将内联XML和内联XSL转换为XHTML

时间:2015-05-19 09:28:55

标签: javascript xml html5 xslt internet-explorer-11

加载XML后,XSLT无法在IE11中对其进行转换 转型时我得到了Object doesn't support property or method transformNode

<!DOCTYPE html>
<html>
<head>
<title>XML Data Block Demo</title>
<script id="GUI" type="application/xml">
<GUI>
  <option>
    <name>My Inbox</name>
  </option>
  <option>
    <name>My Home</name>
  </option>
</GUI>
</script>

<script id="GUIxsl" type="application/xml">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

  <h2>GUI</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Option</th>
      </tr>
      <xsl:for-each select="GUI/option">
      <tr>
        <td><xsl:value-of select="name"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>
</script>
<script>
function loadOnPage() {
    debugger;
    var xml = document.getElementById("GUI").textContent;
    var xsl = document.getElementById("GUIxsl").textContent;
    var parser = new DOMParser();
    var docxml = parser.parseFromString(xml, "application/xml");
    var docxsl = parser.parseFromString(xsl, "application/xml");
    ex = docxml.transformNode(docxsl);
    document.getElementById("example").innerHTML = ex;

}
</script>
</head>
<body onload="loadOnPage()";>
<div id="example" />

Demo did not run
</body>
</html>

1 个答案:

答案 0 :(得分:1)

如果您想在IE中使用XSLT,那么您不能使用DOMParser来解析XML,您需要使用带有new ActiveXObject('Msxml2.DOMDocument.6.0')loadXML方法的MSXML DOM文档,I不要认为尝试将XML或XSLT代码放入HTML中是个好主意,但是样本http://home.arcor.de/martin.honnen/html/test2015051901.html在IE 11中适用于我:

function loadOnPage() {
  var xml = document.getElementById("GUI").textContent;
  var xsl = document.getElementById("GUIxsl").textContent;


  var docxml = new ActiveXObject('Msxml2.DOMDocument.6.0');
  docxml.loadXML(xml);

  var docxsl = new ActiveXObject('Msxml2.DOMDocument.6.0');
  docxsl.loadXML(xsl);

  var htmlFrag = docxml.transformNode(docxsl);
  document.getElementById("example").innerHTML = htmlFrag;

}

window.onload = loadOnPage;
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Test</title>
  <style>
  </style>
</head>

<body>
  <script id="GUI" type="application/xml">
    <GUI>
      <option>
        <name>My Inbox</name>
      </option>
      <option>
        <name>My Home</name>
      </option>
    </GUI>
  </script>

  <script id="GUIxsl" type="application/xml">
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">

        <h2>GUI</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th style="text-align:left">Option</th>
          </tr>
          <xsl:for-each select="GUI/option">
            <tr>
              <td>
                <xsl:value-of select="name" />
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </xsl:template>
    </xsl:stylesheet>
  </script>


  <div id="example">

    Demo did not run
  </div>

</body>

</html>