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

标签 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

样本.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/

相关文章:

java - JAXB 解码删除换行符

sql - 什么时候可以将JSON或XML数据保存在SQL表中

xml - 使用 PowerShell 将 XSL 应用于 XML : Exception calling "Transform"

xml - 创建 XSL 样式表以根据输入参数省略不需要的元素

xml - 我们如何将 Microsoft Word DOCX 文件转换为 XSLT 中的 HTML?

xslt - 具有动态匹配的xslt调用模板

javascript - 检索旧记录的行项目

JavaScript 不引人注目

javascript - 如何让 div/table 改变它们的大小?

javascript - "containment:parent"工作不正常