XSL Transformations using ASP.NET - To err is human
(Page 7 of 7 )
To make errors is considered human; commit errors regularly, and you must be a programmer. Bitter as it may sound, programmers are prone to making errors, and trapping them in their scripts is a vital weapon in the armory of every ASP.NET programmer.
Take a look at the next example that I have cooked up.
<%@ Page Language="c#" %>
<%@ import Namespace="System.Xml.Xsl" %>
<script runat="server">
void Page_Load() {
// Start the "try" block
try {
// 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);
// transfor the XML file using the XSLT file
// store the output in a StringWriter object
objXSLTransform.Transform(strXMLFile, strHTMLFile);
// output message
output.Text = strHTMLFile + " file published successfully.";
} catch (XsltException e) {
output.Text = "An XSLT Exception occured: " + e.Message;
} catch (Exception e) {
output.Text = "A General Exception occured: " + e.Message;
} finally {
// clean up memory
if(objXSLTransform != null) {
objXSLTransform = null;
}
}
}
</SCRIPT>
<HTML>
<HEAD>
<TITLE>ASPFree.com</TITLE>
<BASEFONT face="Arial" />
</HEAD>
<BODY>
<asp:Label id="output" runat="server"></asp:Label>
</BODY>
</HTML>
The output of this example - if all goes well - is the same as the last one, i.e. a HTML file is generated on the server at the specified location. But Murphy’s Law states "if things can go wrong, they will."
Consider a situation where the "articles.xml" XML file has been accidentally deleted from the server. Now, load the above example in your browser. The updated ASP.NET script ensures that the .NET compiler takes a quiet detour into the "catch" block of the "try-catch-finally" mechanism and displays the following error message:

What would happen if there is no error handling present in the above example - take a look:

Now, that is definitely a sight for sore eyes!
That is not the end of the story. Let me assume that, in a hurry to get my work done and over with, I replace the "match" attribute of the <xsl:template> element in the XSLT style sheet with the word "matches." Not likely to happen, but then nobody’s perfect.
Should I worry? Not really. The error handling mechanism in my ASP.NET is quite adept at handling such unfortunate mishaps. Why? It takes the honorable route of displaying a polite error message as shown in the output below:

As usual, it is up to the resourceful "try-catch-finally" block to make amends for all my mistakes.
The general norm is to place all the code that can potentially throw errors within the try block - for the above example, this includes loading the XSLT style sheet using the Load() method and pulling off the XSL transformation by calling the Transform() method.
Next, I have the "catch" block. Since the "System.Xml.Xsl" assembly is equipped with it very own XsltException(), the catch block tries to trap any error in XSLT style sheet using this exception handler.
However, if things get worse, I have also provided for general Exception() handler - this can catch any mistake that XsltException() object is unable to handle.
Last but not least, I have the "finally" block. As you might be aware, this block contains code that will always execute at the end of the execution of the "try" block even if the script encounters an exception. I use this feature to carry out a clean-up job that includes closing any active XslTransform() object that might have been created during the execution of the script.
Round Up
That's about it for this article. If you're interested in learning more about XSLT on the .NET platform, take a look at the following links:
XSLT and XSL at the W3C: http://www.w3.org/Style/XSL/
XSL Transformations with the XslTransform Class on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconxslttransformationswithxsltransformclass.asp
The ASP.NET "Xml" server control on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsXmlClassTopic.asp
The .NET XslTransform() class on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxslxsltransformclasstopic.asp
The .NET XPathDocument() class on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathDocumentClassTopic.asp
Note: All examples in this article have been tested on Windows 2000 Server with ASP.NET version 1.1 . Examples are illustrative only, and are not meant for a production environment. YMMV!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |