Bug in XSLT with c# ?

M

Matthew Hazlett

I can apply an XSLT to an XML document fine in C#.
But an issue occures if the XSLT has a for-each. For some reason the xmldom
seems to ignore it. I have read posts from other people with this problem
but no solution, any ideas? (Can some kind person try to reproduce this
bug)

Heres the code I use for the transform:

StringWriter sw = new StringWriter();

XmlDocument srcDoc = new XmlDocument();
srcDoc.Load("test.xml");

XslTransform srcXSLT = new XslTransform();
srcXSLT.Load("test.xslt");

srcXSLT.Transform(srcDoc, null, sw);
Output.Text = sw.ToString();

XML Doc:
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<catalog>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious agent known only
as Oberon helps to create a new life for the inhabitants of London. Sequel
to Maeve Ascendant.</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology conference, tempers
fly as feathers get ruffled.</description>
</book>
</catalog>

XSL Doc:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:blush:utput method="html"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr><td><b>Author</b></td><td><b>Title</b></td></tr>
<xsl:for-each select="//book">
<tr>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="title"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Here is the output I get:

<html>
<body>
<table border="1">
<tr>
<td>
<b>Author</b>
</td>
<td>
<b>Title</b>
</td>
</tr>
</table>
</body>
</html>
 
H

Hans Kesting

I don't know about the <xsl:for-each> element, but you can rewrite
that part as <xsl:apply-templates select="//book" />

with an extra template to handle the "book":
<xsl:template match="//book">
<tr>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="title"/></td>
</tr>
</xsl:template>

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top