XML
  Home arrow XML arrow Page 7 - XSL Transformations using ASP.NET
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XSL Transformations using ASP.NET
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    2004-11-30

    Table of Contents:
  • XSL Transformations using ASP.NET
  • Start it up
  • Dynamic transformation
  • Jumping higher
  • Code dissection
  • Publish or you will perish
  • To err is human

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    XSLT Transformations using ASP NET

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

    XSLT Transformations using ASP NET

    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:

    XSLT Transformations using ASP NET

    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.

       · pls post the tested sample files also.thnks pankajsrivastava@vsnl.net
     

    XML ARTICLES

    - More on Triggers and Styles and Control Temp...
    - Looking at Triggers with Styles and Control ...
    - A Closer Look at Styles and Control Templates
    - Styles and Control Templates
    - Properties and More in XAML
    - Elements and Attributes in XAML
    - XAML in a Nutshell
    - Importing XML Files into Access 2007
    - Using MSXML3.0 with VB 6.0
    - MSXML, concluded
    - MSXML, continued
    - MSXML Tutorial
    - Generating XML Schema Dynamically Using VB.N...
    - XSL Transformations using ASP.NET
    - Applying XSLT to XML Using ASP.NET





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek