gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:06:53 24 4
gpt4 key购买 nike

加载 XML 和 XSLT 后无法在 IE11 中对其进行转换。
转换时我得到 Object does not 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>

最佳答案

如果您想在 IE 中使用 XSLT,则不能使用 DOMParser 来解析 XML,您需要使用 MSXML DOM 文档 new ActiveXObject('Msxml2.DOMDocument.6.0 ')loadXML 方法,但我认为尝试将 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>

关于javascript - 将内联 XML 和内联 XSL 转换为 XHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30321702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com