无法在 JavaScript 的网页上加载 XML 个文档
Cannot load XML documents on web page with JavaScript
我正在尝试加载我的解析 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
sample.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>
sample.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
的值调用的 function outputXML(location, xml, xsl) { ...}
调用 function processXML (location, xml, xsl) { ... }
.
此函数将 resultDocument
附加到具有 id
且值为 location
:
的元素
document.getElementById(location).appendChild(resultDocument);
我正在尝试加载我的解析 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
sample.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>
sample.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
的值调用的 function outputXML(location, xml, xsl) { ...}
调用 function processXML (location, xml, xsl) { ... }
.
此函数将 resultDocument
附加到具有 id
且值为 location
:
document.getElementById(location).appendChild(resultDocument);