Using XLS Template In C#

G

Grep J

Hello,

I'm using Visual Studio.NET 2003 and I'm wondering that
is it possible to call XSL file's template to transform a XML file?

I have following files.

A XML file below
<?xml version="1.0" encoding="UTF-8"?>
<MODEL>
<Charge id="1">ABC</Charge>
<Charge id="2">DEF</Charge>
</MODEL>

and XLS file below containing two XSL templates, TempalteA and TemplateB

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!-- This is template "block" 1 -->
<xsl:template match='TemplateA'>
<table border="1">
<xsl:for-each select="Charge">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<!-- This is template "block" 2 -->
<xsl:template match='TemplateB'>
<table border="1">
<xsl:for-each select="Charge">
<tr bgcolor="#0000FF">
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

So, it it possible in C# to ask certain XML/XSL classes to transform
XML file above using e.g. XSL file's TemplateB?

I have had tried classes like XPathDocument, XslTransform and
XPathNavigator
but haven't succeeded. Can anyone please help?

Cheer,
 
N

Nicholas Paldino [.NET/C# MVP]

Grep,

You definitely should be using the XslTransform class. What is
happening when you use that?
 
M

Martin Honnen

Grep J wrote:

I'm using Visual Studio.NET 2003 and I'm wondering that
is it possible to call XSL file's template to transform a XML file?

Yes, using XslTransform.
I have following files.

A XML file below
<?xml version="1.0" encoding="UTF-8"?>
<MODEL>
<Charge id="1">ABC</Charge>
<Charge id="2">DEF</Charge>
</MODEL>

and XLS file below containing two XSL templates, TempalteA and TemplateB

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!-- This is template "block" 1 -->
<xsl:template match='TemplateA'>

That template will be applied to elements with the element name
TemplateA but your XML above has elements named MODEL and Charge so that
template will not be used at all when you run the stylesheet against the
XML.

<xsl:template match='TemplateB'>

Same for that template, it will be applied to elements with name
TemplateB but your XML document does not have any.
 

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

Similar Threads

xslt and xml problem 4
xslt transform xml data into to excel worksheet -- Urgent 2
xslt 2
wrong element in transformed xml 5
xml & xsl transform 1
Bug in XSLT with c# ? 1
Puzzling xsl problem 3
XSLT 1

Top