XSL Transformations using ASP.NET - Publish or you will perish
(Page 6 of 7 )
I have already shown you two different techniques to transform XML document using XSLT style sheets.
There is one common feature in all these examples - they transform the XML data using the style sheet in real-time. This mechanism works efficiently when I am working in a development environment and - in all probability - I am the only user accessing the resources of the Web server.
Cut to a real-world Web server, where thousands (if not millions) of users are constantly accessing a website. As the above examples transform the XML files every time, the run-time memory utilized by the processors for transforming the XML will be enormous - not to mention the high loads that the server will be subjected to. In such a scenario, any means to reduce the memory utilization as well as the load is definitely welcome.
One simple mechanism: develop an ASP.NET script that transforms an XML file using a XSLT style sheet and stores the output in a static HTML file. The Web server, then, will serve these static files to visitors of the website, thereby avoiding the need for run-time transformation for every visit. This, in turn, will definitely lead to drastic improvement in server memory utilization as well as reduction of load on the Web server, since requests will be served quickly.
Fortunately, this task is not very complex when it comes to ASP.NET. Take a look at the next example that saves the transformed XML data as a HTML file on the server.
First, the XSLT style sheet has to be updated in order to ensure that the HTML output is complete, i.e. it contains the required HTML elements such as <HTML>, <HEAD>, <BODY> etc.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/articles">
<HTML>
<HEAD>
<TITLE>ASPFree.com</TITLE>
<BASEFONT face="Arial" />
</HEAD>
<BODY>
<xsl:apply-templates select="article"/>
</BODY>
</HTML>
</xsl:template>
<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>
Next, I have updated the ASP.NET script in order to publish an HTML file on the server instead of merely displaying the output in the browser.
<%@ Page Language="C#" %>
<%@ import Namespace="System.Xml.Xsl" %>
<script runat="server">
void Page_Load(){
// path to the XML and XSLT files
string strXSLTFile = Server.MapPath("articles.xsl");
string strXMLFile = Server.MapPath("articles.xml");
string strHTMLFile = Server.MapPath("articles.htm");
// create instance of XstTransform object
XslTransform objXSLTransform = new XslTransform();
// load the XSLT style sheet
objXSLTransform.Load(strXSLTFile);
// transform the XML file using the XSLT file
// store the output in a HTML file
objXSLTransform.Transform(strXMLFile, strHTMLFile);
// output message
output.Text = strHTMLFile + " file published successfully.";
// clean up memory
objXSLTransform = null;
}
</SCRIPT>
<HTML>
<HEAD>
<TITLE>ASPFree.com</TITLE>
<BASEFONT face="Arial" />
</HEAD>
<BODY>
<asp:Label id="output" runat="server"></asp:Label>
</BODY>
</HTML>
If you load the example in browser, you will notice the following output:

Navigate to the location that appears in the output above and you will find that the script has created the required HTML file.
Now, let us focus on the ASP.NET script itself.
The first change you will notice that I have only imported one .NET assembly i.e. the "System.Xml.Xsl" assembly. The rest (imported earlier) are not required for this example. This is followed by definition of the strings variables to store the path to the XML and XSLT style sheet files. This time around, I have defined one more variable called "strHTMLFile" - this is used to store the name of the HTML file that will be created on the server.
Next, I created a local instance of XslTransform() object and invoked the Load() method in order to load the required XSLT style sheet.
Then, I passed the locations of the input XML file and output HTML file to Transform() method that - once again - bears the responsibility of loading the XML file from the specified location, applying the style sheet rules from XSLT file and storing the output as a HTML file at the specified location.
That was pretty simple, wasn’t it?
Just one caveat - ensure that the location on the server where the HTML file will be created has appropriate "write" permissions for the special ASP.NET user.
Next: To err is human >>
More XML Articles
More By Harish Kamath