无法使用JavaScript在网页上加载XML文档

时间:2015-02-17 15:21:57

标签: javascript xml xslt

我正在尝试加载我的解析我的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

Click here to view the code

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>

1 个答案:

答案 0 :(得分:1)

我刚刚测试了您的示例,并且正在使用调整功能向HTML添加id="content"元素:

<body onLoad="outputXML('content', 'assets/xml/sample.xml', 'assets/xml/sample.xsl');">
  <div id="content"></div>
</body>

使用function outputXML(location, xml, xsl) { ...}作为参数content的值调用的location调用function processXML (location, xml, xsl) { ... }
此函数会将resultDocument附加到id的元素,其值为location

document.getElementById(location).appendChild(resultDocument);