Click here to Skip to main content
6,822,613 members and growing! (16,008 online)
Email Password   helpLost your password?
    Beginner License: The Code Project Open License (CPOL)

Beginners Introduction To XSL Transform : Rendering XML Data using XSL - Get HTML output

By Abhijit Jana

Beginners Introduction to Rendering XML data using XSL transformation, a step by step guide to rendering XML data using XSL
C#, Windows, .NET, ASP.NET, ADO.NET
Revision:10 (See All)
Posted:5 Jul 2009
Updated:7 Jul 2009
Views:14,161
Bookmarked:31 times
Technical Blog
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this technical blog.
Popularity: 3.75 Rating: 3.75 out of 5
3 votes, 30.0%
1

2
1 vote, 10.0%
3
2 votes, 20.0%
4
4 votes, 40.0%
5
A Technical Blog article. View entire blog here.

Download XSLTransform Source Code.zip - 4.13 KB

Overview   

Some times we need to display the XML data in our web application in specific format. XSLT provides the ability to display the XML document in some specific format like HTML, PDF etc. We can select a a XML file or a portion of XML File and using XSL Transformation we can display in some specific format.  

SampleFlow

An XSL transformation need an XML document to transform and an XSL style sheet describing how the transformation will take place. An XSLT engine then transforms the XML document via the XSL style sheet and will produce the output in the format specified by the style sheet.

Here I am just going to show you how we can display a XML data using XSL in our web page and which will help beginners to start with. This is an sample application. The XSL, which I have used over here is very simple. If you want to learn details on XSL please read tutorials from W3School.

How to Implement ? 

In this section I have described the steps which we need to follow to rendering a XML data using XSL. The source of the data can be any thing, we can load data from text file, database or can be an xml file itself. But at the time of XSLTranformation we need to provide the xml data format and the xsl file path.

The steps are,

1. Create Data Base : 

Rather than reading the data from XML, I have read the data from database. First of all I have created an DB, called Student with table name StudentDetails. Table contain some dummy data like, 

db

Fig: Sample Data

2. Add XSL File 

Before, reading the data from database, we have to create the XSL file, We can add XSL file by just right click on the project > Add New Item >Select XSLT File

AddXSL

Fig: Add New XSL File

I have put the xsl file in a specific folder called XSL. This is not mandatory to put the xsl file inside the XSL folder. we have to keep it in mind while provide the filepath for XSL rendering.

FileH

Fig : File Structure

3. Desing XSL 

Now, Designing XSL is one of the important task, and there are many things that is related with an XSL file desing . In my case XSL file is  very simple and there is no complexity to design the page, but if you need to learn in details, I will suggest you to read from W3School. First of all have a look into the XML data which I have got from the dataset.

xml

And based on that we need to desing the XSL File. Below is the StudentDetails XSL. 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
<table width="100%" align="center" cellpadding="0" cellspacing="0" border="1"
 style="background-color:#123456;font-family:verdana;font-size:10pt;border:1">
<tr>
<td  width="10%"  align="left" >
 Roll</td>
<td width="70%" align="left">
 Name</td>
<td width="20%" align="left">
 Address</td>
</tr>
</table>
<xsl:for-each select="Students/Table">
<table width="100%" align="center" cellpadding="0" cellspacing="0" border="1"
 style="font-family:verdana;font-size:10pt;border:1">
<tr >
<td  width="10%"  align="left"   >
 <xsl:value-of select="Roll"/></td>
<td width="70%" align="left" >
 <xsl:value-of select="Name"/></td>
<td  width="20%" align="left" >
 <xsl:value-of select="Address"/></td>
</tr>
</table>
</xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

 The XML root node is started with  "Studnets/Table", and the XSL will read the content for each and every child. Following section of the XSL will do the job for that.

<xsl:for-each select="Students/Table"> 

Now, have a look into the code,  

What I did? I have just read the data from database and stored the data in a dataset. then just read the xml content from dataset using ,

string XMLString=ds.GetXml();

Below  is the complete code for read the data from database and read the xml string from dataset.

public string strstudentDetails = string.Empty;
 protected void Page_Load(object sender, EventArgs e)
 {
 string    _strConnectionString = "Data Source=.\\sqlexpress;Initial Catalog=Student;Integrated Security=True";
 string _strquery = "select * from studentDetails";
 SqlConnection con = new SqlConnection(_strConnectionString);
 DataSet ds = new DataSet("Students");
 SqlDataAdapter da = new SqlDataAdapter(_strquery, con);
 da.Fill(ds);
 //Get the XML From DataSet
 string strXML = ds.GetXml();
 strstudentDetails=GetHtml(Server.MapPath("~/xsl/studentDetails.xsl"), strXML);
 }

Now I have wrote a function GetHTML. GetHtml function actually doing the job. Its taking XSL Stylesheet and XML data as parameter peforming the XSL transformation and returning the html output

 /// <summary>
 /// Get HTML From XML and XSL
 /// </summary>
 ///<param name="xsltPath">XSL File Path</param>
 ///<param name="xml">XML String</param>
 /// <returns>HTML Output</returns>
 public static string GetHtml(string xsltPath, string xml)
 {
 MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(xml));
 XPathDocument document = new XPathDocument(stream);
 StringWriter writer = new StringWriter();
 XslCompiledTransform transform = new XslCompiledTransform();
 transform.Load(xsltPath);
 transform.Transform(document, null, writer);
 return writer.ToString();
 }
 }

Now we have got the html data and we have to display the data to the page.  Below code is used to display the actual content into the page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>Student Page</title>
</head>
<body>
 <form id="form1" runat="server">
<div>
<table >
<tr>
<td>
 <b>Student Info : Displying using XSL Rendering</b></td>
</tr>
<tr>
<td>
 <%= strstudentDetails %></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Now, Run the web site, you will get the following output.

Output123

Hope this will help you to move ahead with XSL Transformation.

Thank you

Posted in General

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhijit Jana


Member
Abhijit has done Master Degree in Computer Application from Heritage Institute of Technology (HIT-K) ,Kolkata, West Bengal, India . He is an interested, committed, creative Software professional having Approx. 3 years of solid experience in web-based and windows based solutions in Microsoft Technologies using .NET 2.0, .NET 3.0 , .NET 3.5, ASP.NET 2.0, ASP.NET 3.5 C# 2.0, AJAX, Web Services, MS SQL Server 2005, Exchange Server, Active Directory, and Dot Net Nuke (DNN),Win Forms, WinServices, WSS (Windows Sharepoint Server 3.0 ), WPF, WWF. He is also an MCP (Microsoft Certified Professional) and MCTS (Microsoft Certified Technology Specialist) on Web Development. He has good knowledge of Object Oriented Programming, 3-Tier Architecture and Design Patterns as well as good command over IIS (IIS 5.1,IIS 6.0, IIS 7.0) and deployment of Application on Live Production Environment . His hobbies, listing to music and Developing Own small Tools Utilities and Knowledge sharing.


Awards
CodeProject MVP 2010
CodeProject MVP 2009
Prize winner "Best ASP.NET article of Sep 2009
Prize winner "Best ASP.NET article of July 2009
Prize winner "Best ASP.NET article of June 2009"
Prize winner "Best ASP.NET article of January 2009"
Prize winner "Best ASP.NET article of November 2008"

Prize winner "Best ASP.NET article of October 2008"

Abhijit's CodeProject Guru :
Sacha
Occupation: Software Developer (Senior)
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralMy vote of 1 Pinmemberrobvon20:16 6 Jul '09  
GeneralMy vote of zero. Pinmemberrobvon20:15 6 Jul '09  
GeneralRe: My vote of zero. PinmvpAbhijit Jana20:26 6 Jul '09  
GeneralMy vote of 1 PinmemberKamran Shahid20:51 5 Jul '09  
GeneralRe: My vote of 1 PinmvpAbhijit Jana21:25 5 Jul '09  
GeneralMy vote of 1 PinmemberTarabanko Yury12:40 5 Jul '09  
GeneralRe: My vote of 1 PinmvpAbhijit Jana19:50 5 Jul '09  
GeneralRe: My vote of 1 PinmemberTarabanko Yury22:37 5 Jul '09  
GeneralRe: My vote of 1 PinmvpAbhijit Jana0:12 6 Jul '09  
General5? Self-rating is to humiliate oneself PinmemberImHere2Learn5:22 5 Jul '09  
GeneralRe: 5? Self-rating is to humiliate oneself PinmvpAbhijit Jana19:57 5 Jul '09  
GeneralRe: 5? Self-rating is to humiliate oneself PinmemberKamran Shahid20:52 5 Jul '09  
GeneralRe: 5? Self-rating is to humiliate oneself PinmvpAbhijit Jana21:29 5 Jul '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jul 2009
Editor: Sean Ewington
Copyright 2009 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2010
Web11 | Advertise on the Code Project