sponsored links

Simple sample for transforming XML to HTML by using XSLT

1. Create a general web application.

2. Create the XML file called "Book.xml"

<?xml version="1.0" encoding="utf-8" ?> <ric> <catalog> <book price="75"> <author>Kalen Delaney</author> <name>Inside SQL Server 2000</name> </book> <book price="200"> <author>Ken Henderson</author> <name>The Guru's Guide to SQL Server Architecture</name> </book> </catalog> </ric>

3. Create the XSLT file called "Test.xslt"

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="utf-8"/> <xsl:template match="/"> <table cellpadding="0" cellspacing="0" style="font-size:14px;" mce_style="font-size:14px;"> <tr> <th>Book Name</th> <th>Author</th> <th>Price</th> </tr> <xsl:for-each select="//ric/catalog/book"> <tr> <td> <xsl:value-of select="name"></xsl:value-of> </td> <td style="padding-left:15px; padding-right:15px;" mce_style="padding-left:15px; padding-right:15px;"> <xsl:value-of select="author"></xsl:value-of> </td> <td style="background-color:Red; color:Yellow; font-weight:bold;" mce_style="background-color:Red; color:Yellow; font-weight:bold;"> <xsl:value-of select="@price"/> </td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet>

4. Add the NameSpaces,

using System.Xml; using System.Xml.Xsl; using System.IO;

5. Add the code in the page load event.

StringWriter output = new StringWriter(); XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(Server.MapPath("test.xslt")); transform.Transform(Server.MapPath("book.xml"), null, output); // RealContent is <div id="RealContent" runat="server /> RealContent.InnerHtml = output.ToString();

6. Run the page and see the transformation result.