我想将我的XML文件链接到我的HTML页面

前端之家收集整理的这篇文章主要介绍了我想将我的XML文件链接到我的HTML页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个 XML文件,我希望它可以在我创建的HTML页面显示.有人能告诉我怎么做.
<?xml version="1.0"?>
<Family>
<Mom>Alison</Mom>
<age>44</age>
<son>Ian</son>
<age>8</age>
<son>Seth</son>
</Family>

我想从我的html页面中读到这个

您可以使用 XSLT语言来转换XML文档.也许这符合您的需求.

我已经修改了你提供的XML,因为我觉得它结构不好.所以,如果我们有这样的文件

<?xml version="1.0"?>
<?xml-stylesheet href="bla.xsl" type="text/xsl" ?>
<family>
   <person>
      <role>Mom</role>
      <name>Alison</name>
      <age>44</age>
   </person>
   <person>
      <role>Father</role>
      <name>Ben</name>
      <age>45</age>
   </person>
   <person>
      <role>Son</role>
      <name>Ian</name>
      <age>8</age>
   </person>
</family>

XSLT文件看起来像这样:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Family</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
    <th>Role</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
      <xsl:for-each select="family/person">
          <tr>
        <td><xsl:value-of select="role"/></td>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="age"/></td>
          </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

猜你在找的XML相关文章