如何将XML和XSLT文件加载到浏览器中

时间:2014-01-12 17:44:22

标签: html xml xslt

我创建了一个XML和XSL文件,以HTML格式加载到网页上。由于某种原因,这不起作用..数据没有显示在Web浏览器的表格中。

以下是我的代码,有什么我想念的吗?请帮忙吗?

XML文件代码:

<?xml version="1.0"?>
<?xml-stylesheet href="skymovies.xsl" type="text/xsl"?>
<collection>
  <film>
    <title>Happy Gilmore</title>
    <year>1991</year>
    <genre>Comedy</genre>
  </film>
  <film>
    <title>Rango</title>
    <year>1991</year>
    <genre>Comedy</genre>
  </film>
  <film>
    <title>Happy Gilmore</title>
    <year>1991</year>
    <genre>Comedy</genre>
  </film>
  </movie>
</collection>

XSLT文件代码:

<?xml version="1.0" encoding="IS0-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/collection">
    <html>
    <body>
      <table border="1">
        <tr>
          <th>Title</th>
          <th>Year</th>
          <th>Genre</th>
        </tr>

        <xsl:for-each select="film">
         <xsl:value-of select="title" /></td>
         <xsl:value-of select="year" /></td>
         <xsl:value-of select="genre" /></td>
        </xsl:for-each>
      </table>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

有没有我做错了?请帮忙吗?

2 个答案:

答案 0 :(得分:0)

您的XML无效。您的XML中有</movie>,没有开始标记。尝试使用此XML:

<?xml version="1.0"?>
<?xml-stylesheet href="skymovies.xsl" type="text/xsl"?>
<collection>
  <film>
    <title>Happy Gilmore</title>
    <year>1991</year>
    <genre>Comedy</genre>
  </film>
  <film>
    <title>Rango</title>
    <year>1991</year>
    <genre>Comedy</genre>
  </film>
  <film>
    <title>Happy Gilmore</title>
    <year>1991</year>
    <genre>Comedy</genre>
  </film>
</collection>

答案 1 :(得分:0)

您的XSLT也无效:您有从未打开的关闭</td>标记。你也忘了为每部电影创作一行。它应该看起来像:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/collection">
    <html>
    <body>
      <table border="1">
        <tr>
          <th>Title</th>
          <th>Year</th>
          <th>Genre</th>
        </tr>
        <xsl:for-each select="film">
        <tr>
           <td><xsl:value-of select="title" /></td>
           <td><xsl:value-of select="year" /></td>
           <td><xsl:value-of select="genre" /></td>
         </tr>
        </xsl:for-each>
      </table>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>