XML
  Home arrow XML arrow Page 6 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: 4 stars4 stars4 stars4 stars4 stars / 22
    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 - 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:

    XSLT Transformations using ASP NET

    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.

    More XML Articles
    More By Harish Kamath


       · 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-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway