Convert an XML file to HTML file using XSLT
Data is best stored in XML format, but the presentations are better in HTML. Here is a case where the following data in a XML file needs to be converted to a HTML file
Here is the XML File for conversion
< ?xml version="1.0" encoding="utf-8"? >
< !-- Sample File For Dot Net Tips & Tricks-- >
< !-- http://dotnetdud.blogspot.com -- >
< invoice InvNo="DND1232" InvDate="3-Oct-2008" >
< customer >
< custID >CID134< /custID >
< custName >Allirajan Ramachandran< /custName >
< /customer >
< items >
< item >
< MovieID >F123455< /MovieID >
< MovieName >Happy Days< /MovieName >
< RentedDate >3-Oct-2008< /RentedDate >
< ReturnDate >5-Oct-2008< /ReturnDate >
< /item >
< item >
< MovieID >F123462< /MovieID >
< MovieName >Lord Of Rings< /MovieName >
< RentedDate >3-Oct-2008< /RentedDate >
< ReturnDate >5-Oct-2008< /ReturnDate >
< /item >
< item >
< MovieID >F123232< /MovieID >
< MovieName >Drona< /MovieName >
< RentedDate >1-Oct-2008< /RentedDate >
< ReturnDate >3-Oct-2008< /ReturnDate >
< /item >
< /items >
< /invoice >
The XML file should be transformed to HTML using XSLT. This needs a stylesheet (XSL) defining what needs to be transformed and how it needs to be done.
Here is a sample stylesheet
< ?xml version="1.0" encoding="utf-8"? >
< !--
< !--
< !--
< !--
< xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:vbadud="http://vbadud.in"
>
< msxsl:script implements-prefix="vbadud" language ="C#" >
< ![CDATA[
public string Defaulted(String RetDate)
{
if (DateTime.Parse(RetDate) < DateTime.Now)
{
return "Defaulted";
}
else
{
return "--";
}
}
public string Today()
{
return DateTime.Now.ToString();
}
]] >
< /msxsl:script >
< xsl:output method="html" indent="yes"/ >
< xsl:template match="invoice" >
< h1 >DND Video Library - Invoice< /h1 >
< p >
Invoice Date : =
< xsl:value-of select ="vbadud:Today()"/ >
< /p >
< p >
Invoice No : =
< xsl:value-of select ="@InvNo"/ >
< /p >
< xsl:apply-templates/ >
< /xsl:template >
< xsl:template match ="customer" >
=========== Customer Information =========
< br/ >
Customer ID :=
< xsl:value-of select ="custID"/ >
< br/ >
Customer Name :=
< xsl:value-of select ="custName"/ >
< br/ >
============================================
< br/ >
< xsl:apply-templates/ >
< /xsl:template >
< xsl:template match ="items" >
< table >
< th >Movie ID< /th >
< th >Movie Name< /th >
< th >Rented Date< /th >
< th >Return Date< /th >
< th >Defaulted< /th >
< xsl:apply-templates/ >
< /table >
< /xsl:template >
< xsl:template match ="item" >
< tr >
< td >< xsl:value-of select ="MovieID"/ > < /td >
< td >
< xsl:value-of select ="MovieName"/ >
< /td >
< td >
< xsl:value-of select ="RentedDate"/ >
< /td >
< td >
< xsl:value-of select ="ReturnDate"/ >
< /td >
< td >
< xsl:value-of select ="vbadud:Defaulted(ReturnDate)"/ >
< /td >
< /tr >
< /xsl:template >
< /xsl:stylesheet >
The above stylesheet uses C# scripting to identify defaulters
The following C# code will transform (convert) the XML to HTML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public void transform_Xml2Html()
{
XslCompiledTransform XSLT = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableScript = true ;
XSLT.Load(@"c:\shasurdata\forblogger\VideoLibXSLTFile.xslt", settings, new XmlUrlResolver() );
XSLT.Transform(XMLFile ,@"c:\shasurdata\forblogger\SalesInvoice.html");
}
settings.EnableScript = true enables scripting in XSL file. The code uses XslCompiledTransform methods to convert the XML files
Simple isn’t it. Here you have the output:
HTML Source
< h1 xmlns:vbadud="http://vbadud.in" >DND Video Library - Invoice< /h1 >
< p xmlns:vbadud="http://vbadud.in" >
Invoice Date : =
04-10-2008 20:37:02< /p >
< p xmlns:vbadud="http://vbadud.in" >
Invoice No : =
DND1232< /p >
=========== Customer Information =========
< br xmlns:vbadud="http://vbadud.in" >
Customer ID :=
CID134< br xmlns:vbadud="http://vbadud.in" >
Customer Name :=
Allirajan Ramachandran< br xmlns:vbadud="http://vbadud.in" >
============================================
< br xmlns:vbadud="http://vbadud.in" >
CID134
Allirajan Ramachandran
< table xmlns:vbadud="http://vbadud.in" >
< th >Movie ID< /th >
< th >Movie Name< /th >
< th >Rented Date< /th >
< th >Return Date< /th >
< th >Defaulted< /th >
< tr >
< td >F123455< /td >
< td >Happy Days< /td >
< td >3-Oct-2008< /td >
< td >5-Oct-2008< /td >
< td >--< /td >
< /tr >
< tr >
< td >F123462< /td >
< td >Lord Of Rings< /td >
< td >3-Oct-2008< /td >
< td >5-Oct-2008< /td >
< td >--< /td >
< /tr >
< tr >
< td >F123232< /td >
< td >Drona< /td >
< td >1-Oct-2008< /td >
< td >3-Oct-2008< /td >
< td >Defaulted< /td >
< /tr >
< /table >






















No comments:
Post a Comment