Using XSLT in XML File
I am using Visual Studio 2010 and I have a simple XML file which contain data like
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="XmlStyle.xslt"?>
<catalog>
<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 my XSLT File Contain Data like
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<html>
<body>
<h2>My CD Collection</h2>
<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:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
and now my problem is that when I run my XML file it shows result like this
My CD CollectionTi开发者_C百科tleArtistMy CD CollectionTitleArtistMy CD CollectionTitleArtist
My CD CollectionTitleArtistMy CD CollectionTitleArtist
My CD CollectionTitleArtistMy CD CollectionTitleArtistEmpire BurlesqueMy CD CollectionTitleArtist
My CD CollectionTitleArtistMy CD CollectionTitleArtistBob DylanMy CD CollectionTitleArtist
My CD CollectionTitleArtistMy CD CollectionTitleArtistUSAMy CD CollectionTitleArtist
My CD CollectionTitleArtistMy CD CollectionTitleArtistColumbiaMy CD CollectionTitleArtist
My CD CollectionTitleArtistMy CD CollectionTitleArtist10.90My CD CollectionTitleArtist
My CD CollectionTitleArtistMy CD CollectionTitleArtist1985My CD CollectionTitleArtist
My CD CollectionTitleArtist
What should I do to get the right result?
Strange result, but the stylesheet is much more strange. Use:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<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>
This template produces a correct table when used with a compliant XSLT processor. Note also that it is correct to use XML as output method even if you want html. But you can omit it, as XML is the default output method.
Also make sure to exploit template rule pattern. Example:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:apply-templates />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="catalog/cd">
<tr>
<xsl:apply-templates select="title|artist"/>
</tr>
</xsl:template>
<xsl:template match="title|artist">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
The output method is XML. Change it to HTML:
<xsl:output method="html" indent="yes"/>
It should be like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<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>
精彩评论