XSL Transformations using ASP.NET - Jumping higher
(Page 4 of 7 )
So far, the examples that I have demonstrated used the ASP.NET "Xml" server control. While such ready-to-use ASP.NET server controls make the job at hand a breeze to implement, the real power of the .NET platform lies in the vast number of assemblies that it comes equipped with to handle XML and related technologies, and this includes XSL Transformations.
The XML data in the previous examples can be at best considered to belong to the novice category. When you're working on a real-world project, you are bound to come across something more complicated. Now it is time to approach the task of XSL Transformation in a different - and more complex - manner.
For starters, let me update the XML file so that it contains information about a list of articles rather than a single article - this is more likely in a real-world project:
<?xml version="1.0"?>
<articles>
<article id="110">
<title>XSL Transformations with ASP.NET</title>
<author>Harish R. Kamath</author>
<category>ASP.NET</category>
<abstract>Leverage on the built-in functionality available in ASP.NET to transform your XML documents using XSL Transformations!</abstract>
</article>
<article id="109">
<title>Input Validation in ASP.NET</title>
<author>Harish R. Kamath</author>
<category>ASP.NET</category>
<abstract>Prevent corrupt data from entering into your database - use built-in ASP.NET validation controls in your scripts.</abstract>
</article>
<article id="108">
<title>Master the ASP.NET DataGrid Server Control</title>
<author>Harish R. Kamath</author>
<category>ASP.NET</category>
<abstract>Learn how to use the powerful ASP.NET DataGrid server control to list data from a database table.</abstract>
</article>
</articles>
Next, I have listed the updated XSLT style sheet to transform the above XML data into browser-compatible HTML format.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/article">
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="author"/>
<xsl:apply-templates select="category"/>
<xsl:apply-templates select="abstract"/>
</xsl:template>
<xsl:template match="title">
<H3>Article Title: <xsl:value-of select="." /></H3>
</xsl:template>
<xsl:template match="author">
<H4>Author: <xsl:value-of select="." /></H4>
</xsl:template>
<xsl:template match="category">
<H4>Category: <xsl:value-of select="." /></H4>
</xsl:template>
<xsl:template match="abstract">
<H5>Abstract:</H5>
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
Finally, I have the re-vamped ASP.NET script that brings about the XSL Transformation.
<%@ Page Language="C#" %>
<%@ import Namespace="System.Xml.Xsl" %>
<%@ import Namespace="System.Xml.XPath" %>
<%@ import Namespace="System.IO" %>
<SCRIPT runat="server">
void Page_Load() {
// path to the XML and XSLT files
string strXMLFile = Server.MapPath("articles.xml");
string strXSLTFile = Server.MapPath("articles.xsl");
// create an instance of XPathDocument object
// to load the XML file
XPathDocument objXmlFile = new XPathDocument(strXMLFile);
// create instance of XstTransform object
XslTransform objXSLTransform = new XslTransform();
// load the XSLT style sheet
objXSLTransform.Load(strXSLTFile);
// create an instance of StringWriter object
// to store result of transform
StringWriter objSWriter = new StringWriter();
// transform the XML file using the XSLT file
// store the output in a StringWriter object
objXSLTransform.Transform(objXmlFile, null, objSWriter, null);
// display the output in the browser
output.Text = objSWriter.ToString();
// clean up memory
objXmlFile = null;
objXSLTransform = null;
}
</SCRIPT>
<HTML>
<HEAD>
<TITLE>ASPFree.com</TITLE>
<BASEFONT face="Arial">
</HEAD>
<BODY>
<asp:Label id="output" runat="server" />
</BODY>
</HTML>
Load this ASP.NET script in your browser to view the following output:

With the formalities behind me, let me start dissecting the above code listing line-by-line.
Next: Code dissection >>
More XML Articles
More By Harish Kamath