Problems with XSL and HTML tags
I'm just learning XML and XSL, so please bear with me if this is a stupid question. I can't seem to find a开发者_StackOverflow社区n easy answer to this, so I'm assuming I'm making a simple mistake that folks who write guides wouldn't think to address.
I have the following XML (just a snippet, here), that I lifted from w3c for learning:
<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>
And then the following 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>
For some reason, the XSL is not reading the closing h2 tag and it's not reading the opening table tag, rendering the entire thing into one huge heading.
<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>
Try this. First you were outputing the complete catalog element to the header. Other than this you should probably add this :
<xsl:output omit-xml-declaration="yes" indent="yes"/>
On the top of your sheet. The output is what you would expect :
<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>
精彩评论