I’m just trying to create an example XML file with an XSL style sheet. The problem is, when i parse the two files, i get a strange output.
here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="mySchema.xsl"?>
<Artists>
<Artist>
<BandName>The Cure</BandName>
<Albums>
<Album>
<AlbumTitle>Disintegration</AlbumTitle>
<Tracks>
<track>
<Title>Plain Song</Title>
<Order>1</Order>
<Lenght>
<min>5</min>
<sec>12</sec>
</Lenght>
</track>
<track>
<Title>Pictures Of You</Title>
<Order>2</Order>
<Lenght>
<min>7</min>
<sec>24</sec>
</Lenght>
</track>
</Tracks>
</Album>
<Album>
<AlbumTitle>Wish</AlbumTitle>
<Tracks>
<track>
<Title>A Letter To Elise</Title>
<Order>4</Order>
<Lenght>
<min>5</min>
<sec>14</sec>
</Lenght>
</track>
<track>
<Title>From the Edge of the Deep Green Sea</Title>
<Order>2</Order>
<Lenght>
<min>7</min>
<sec>45</sec>
</Lenght>
</track>
</Tracks>
</Album>
</Albums>
</Artist>
<Artist>
<BandName>The Pogues</BandName>
<Albums>
<Album>
<AlbumTitle>If I Should Fall from Grace with God</AlbumTitle>
<Tracks>
<track>
<Title>Fairytale of New York</Title>
<Order>1</Order>
<Lenght>
<min>2</min>
<sec>20</sec>
</Lenght>
</track>
<track>
<Title>Sit Down by the Fire</Title>
<Order>13</Order>
<Lenght>
<min>4</min>
<sec>10</sec>
</Lenght>
</track>
</Tracks>
</Album>
<Album>
<AlbumTitle>Peace And Love </AlbumTitle>
<Tracks>
<track>
<Title>Young Ned Of The Hill</Title>
<Order>3</Order>
<Lenght>
<min>2</min>
<sec>45</sec>
</Lenght>
</track>
<track>
<Title>Boat Train</Title>
<Order>11</Order>
<Lenght>
<min>2</min>
<sec>40</sec>
</Lenght>
</track>
</Tracks>
</Album>
</Albums>
</Artist>
</Artists>
and here is the XSLT file:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Artist">
<html>
<body>
<xsl:apply-templates select="BandName"/>
<br></br>
</body>
</html>
</xsl:template>
<xsl:template match="BandName">
<b>Found a band!</b>
</xsl:template>
</xsl:stylesheet>
When i render the two, using visual studio ide, i can see how the XML file transformed to XHTML. the output looks like this:
<?xml version="1.0" encoding="utf-8"?>
<html><body><b>Found a band!</b><br /></body></html>
<html><body><b>Found a band!</b><br /></body></html>
When i look at this in the browser, its fine, but i’m not happy how the < html >< body >< /body >< /html > is displayed more that once. What am i doing wrong?
thanks
jason
Your
match="Artist"template is getting applied twice because there are twoArtistin your source document. This is what’s resulting in twohtmlelements in the result tree.Try using this slightly modified version of your stylesheet: