Search This Blog

Friday, June 12, 2009

How to tranform xml Document into html table using XSLT and C#

XMl Document: books.xml
-------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<books>
   <book>
     <name> Ajax 2.0 </name>
     <specialization> Programming Language </specialization>
     <author>Wiley ,Bill</author>
     <publication>Wrox</publication>
     <price>200.00</price>
   </book>
   <book>
     <name> ASP.Net 3.5</name>
     <specialization>Web Programming Using C# and VB     </specialization>
     <author>Bill Thomas</author>
     <publication>Wrox</publication>
     <price>560.00</price>
   </book>
   <book>
     <name>C# 3.5 </name>
     <specialization> C# 2008 Powered by Microsoft     </specialization>
     <author> Microsoft Team </author>
     <publication>Wrox</publication>
     <price>600.00</price>
   </book>
</books>
-------------------------------
XSLT file: books.xsl
------------------------------

<?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"/>
   <xsl:template match="/">

    <xsl:element name="books">
     <table >
     <tr bgcolor="gray">
        <td>Name</td>
        <td>Specialization</td>
        <td>Author</td>
        <td>Publication</td>
       <td>Price</td>
      </tr>
      <xsl:apply-templates select="//book" ></xsl:apply-templates>
     </table>

    </xsl:element>

  </xsl:template>
   <xsl:template match="book">

     <tr>
     <xsl:if test="price > 400">
     <td width="100">
        <xsl:value-of select="name"/>
     </td>
        <td width="100">
     </td>
     <td width="100">
        <xsl:value-of select="author"/>
     </td>
    <td width="100">
<xsl:value-of select="publication"/>
</td>
       <td width="100">
     </td>
     </xsl:if>
     </tr>

   </xsl:template>


</xsl:stylesheet>



-----------------------------
Method 1

Step1:
add a literal control to the page like below:
<asp:Literal ID="ltr1" runat="server"></asp:Literal>

Step2:
C#
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public partial class xmlTesting : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       XPathDocument xPathDoc = new XPathDocument(Server.MapPath("books.xml"));
       XslTransform xTrans = new XslTransform();
       xTrans.Load(Server.MapPath("Books.xsl"));

       StringWriter Sw = new StringWriter();

       xTrans.Transform(xPathDoc, null, Sw);

       ltr1.Text = Sw.ToString();
   }
}
Method 2

add a xml control to the page:
<asp:Xml ID="xml1" runat="server" DocumentSource="~/books.xml" TransformSource="~/Books.xsl"></asp:Xml>

-------------------------------------------------------------------
Output

-------------------------------------------------------------------










namespecializationauthorpublicationprice
ASP.Net 3.5Web Programming Using C# and VB Bill ThomasWrox560.00
C# 3.5 C# 2008 Powered by Microsoft Microsoft Team Wrox600.00

1 comment: