我正在尝试加载解析我的 XML 文档,但 HTML 页面仍然为空,是否有什么地方我做错了。
index.html
<script type="text/javascript" src="assets/js/script.js"></script>
<body onLoad="outputXML('content', 'assets/xml/sample.xml', 'assets/xml/sample.xsl');"></body>
script.js
样本.xml
<?xml version="1.0" encoding="utf-8"?>
<hitrecords>
<record genre="Ska">
<artist category="group" gender="male">Madness</artist>
<title>Baggy Trousers</title>
<length>
<minutes>3</minutes>
<seconds>30</seconds>
</length>
</record>
</hitrecords>
样本.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="hitrecords/record">
<p>
<xsl:value-of select="."/>
<xsl:value-of select="@genre"/>
<xsl:value-of select="artist"/>
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
最佳答案
我刚刚测试了您的示例,它正在进行调整以将带有 id="content"
的元素添加到 HTML:
<body onLoad="outputXML('content', 'assets/xml/sample.xml', 'assets/xml/sample.xsl');">
<div id="content"></div>
</body>
使用content
作为参数location
的值来调用函数outputXML(location, xml, xsl) { ...}
调用函数 processXML (location, xml, xsl) { ... }
。
此函数将 resultDocument
附加到 id
值为 location
的元素:
document.getElementById(location).appendChild(resultDocument);
关于javascript - 无法使用 JavaScript 在网页上加载 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28564820/