XSL Transformations using ASP.NET - Start it up
(Page 2 of 7 )
Consider the following XML document (say articles.xml) that lists the details of an article (on a web site such ASPFree.com):
<?xml version="1.0"?>
<article id="110">
<title>Implementing 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>
While this looks fine and dandy to an XML-aware client, ordinary mortals (such as you and me) prefer an HTML version that looks more like this:

This is possible if I use the following XSLT style sheet (say articles.xsl) to transform the above XML file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<H3>Article Title: <xsl:value-of select="article/title" /></H3>
<H4>Author: <xsl:value-of select="article/author" /></H4>
<H4>Category: <xsl:value-of select="article/category" /></H4>
<H5>Abstract:</H5>
<xsl:value-of select="article/abstract" />
</xsl:template>
</xsl:stylesheet>
I have the XML content and I have the XSLT style sheet. Now, I need the glue to ensure that the two of them interact with each other and return the desired HTML output. This is where ASP.NET steps in - I can leverage on the .NET Framework, which is equipped with a wide array of .NET assemblies specifically defined for this purpose.
Take a peek at the following ASP.NET script (say articles.aspx) that uses the server-side .NET "Xml" control.
<%@ Page Language="C#" %>
<HTML>
<HEAD>
<TITLE>ASPFree.com</TITLE>
<BASEFONT face="Arial" size="2">
</HEAD>
<BODY>
<asp:Xml id="output" runat="server" DocumentSource="articles.xml" TransformSource="articles.xslt" />
</BODY>
</HTML>
Load the example in your browser in order to see the desired HTML output (as shown above).
After a quick review of the code listing, I can safely conclude that the task of transforming an XML document using an XSLT style sheet is pretty simple in ASP.NET. All that I need to do is to create an instance of the .NET "Xml" server control using the <asp:Xml> element. This "Xml" server control is associated with several useful attributes - I have used the following:
- DocumentSource: allows me to specify the name of the XML file that I wish to transform
- TransformSource: allows me to specify the name of the XSLT file that is used to transform the XML file specified in the "DocumentSource" property
Next, I have assigned the names of the XML and XSLT style sheet files to the corresponding attributes (listed above) of the <asp:Xml> element and the end result is the transformation of the XML data into the desired HTML output.
Next: Dynamic transformation >>
More XML Articles
More By Harish Kamath