XSL和HTML标记的问题

XSL和HTML标记的问题,html,xml,xslt,Html,Xml,Xslt,我只是在学习XML和XSL,如果这是一个愚蠢的问题,请容忍我。我似乎找不到一个简单的答案,所以我假设我犯了一个简单的错误,写指南的人不会想到解决这个问题 下面是我从w3c中提取的一个片段,用于学习: <catalog>My CD Collection <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</c

我只是在学习XML和XSL,如果这是一个愚蠢的问题,请容忍我。我似乎找不到一个简单的答案,所以我假设我犯了一个简单的错误,写指南的人不会想到解决这个问题

下面是我从w3c中提取的一个片段,用于学习:

<catalog>My CD Collection
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
然后是以下XSL:

    <xsl:template match="/">
    <html>
    <body>
    <xsl:for-each select="/">
    <h2><xsl:value-of select="catalog" /></h2>
    </xsl:for-each>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title" /></td>
        <td><xsl:value-of select="artist" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
出于某种原因,XSL没有读取结束h2标记,也没有读取开始表标记,从而将整个内容呈现为一个大标题

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="/">
<h2><xsl:value-of select="catalog" /></h2>
</xsl:for-each>
<table border="1">
  <tr bgcolor="#9acd32">
    <th>Title</th>
    <th>Artist</th>
  </tr>
  <xsl:for-each select="catalog/cd">
  <tr>
    <td><xsl:value-of select="title" /></td>
    <td><xsl:value-of select="artist" /></td>
  </tr>
  </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
试试这个。首先,将完整的catalog元素输出到标头。除此之外,您可能还应该添加以下内容:

<xsl:output omit-xml-declaration="yes" indent="yes"/>
在你的床单上。输出是您所期望的:

<html>
 <body>
  <h2>My CD Collection

  </h2>
  <table border="1">
     <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
     </tr>
     <tr>
        <td>Empire Burlesque</td>
        <td>Bob Dylan</td>
     </tr>
  </table>
  </body>
</html>

好的,我明白了。当我选择标签时,它会把所有的东西都放在标签里。谢谢你的帮助!