Search This Blog

Wednesday, July 31, 2013

How can convert XML file in to HTML format using by XSLT file.

What is XSLT ?

XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.
XSLT stands for XSL Transformations. In this tutorial you will learn how to use XSLT to transform XML documents into other formats, like XHTML.


xsl_convert_html.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xsl_convert_html.aspx.cs" Inherits="xsl_convert_html" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ask-dotnet.blogspot.in/">

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

xsl_convert_html.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Configuration;

public partial class xsl_convert_html : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      xsl();
    
    }

    public void xsl()
    {

        //XmlReader loads XML file
        XmlReader reader = XmlReader.Create(Server.MapPath("~/xmlfile.xml"));

        //XmlTextWriter creats output file
        XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/xmlfile.htm"), null);

        //XslCompiledTransform loads XSLT file
        XslCompiledTransform xsl = new XslCompiledTransform();
        xsl.Load(Server.MapPath("~/test.xsl"));
        xsl.Transform(reader, writer);
        writer.Close();
        Response.Redirect("xmlfile.htm");

    }
}

XML File:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<category>
  <list>
    <sno>1</sno>
    <hotel>Uday Palace Hotel</hotel>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </list>
  <list>
    <sno>2</sno>
    <hotel>Hotel Sunstar Residency</hotel>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </list>
  <list>
    <sno>3</sno>
    <hotel>Aura De Asia Hotel</hotel>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </list>
</category>

test.xsl file:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ask-dotnet.blogspot.in/">

  <xsl:template match="/">
    <html>
      <body>
        <h2>Hotel  List</h2>
        <table border="1">
          <tr bgcolor="#FFCCFF">
            <th>Sno</th>
            <th>Hotel</th>
            <th>Country</th>
          </tr>
          <xsl:for-each select="category/list">
            <tr>
              <td>
                <xsl:value-of select="sno"/>
              </td>
              <td>
                <xsl:value-of select="hotel"/>
              </td>
              <td>
                <xsl:value-of select="country"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

OUTPUT:



No comments :