XML
  Home arrow XML arrow Page 5 - 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 - Code dissection


    (Page 5 of 7 )

    <%@ Page Language="C#" %>
    <%@ import Namespace="System.Xml.Xsl" %>
    <%@ import Namespace="System.Xml.XPath" %>
    <%@ import Namespace="System.IO" %>
    <%

    // snip

    %>

    For starters, I have imported the "System.Xml.Xsl" and "System.Xml.XPath" assemblies in order to create instances of the required classes later in the script. Next, the "System.IO" assembly has to be imported in order to create an instance of the StringWriter() object, the purpose of which will be clear as I proceed with the explanations.

    <%

    // snip

    <SCRIPT runat="server">

    void Page_Load() {
       
    // path to the XML and XSLT files
    string strXMLFile = Server.MapPath("articles.xml");
    string strXSLTFile = Server.MapPath("articles.xsl");

    // create an instance of XPathDocument object
    // to load the XML file
    XPathDocument objXmlFile = new XPathDocument(strXMLFile);

    // create instance of XstTransform object
    XslTransform objXSLTransform = new XslTransform();
       
    // snip
       
    }
    </SCRIPT>

    // snip

    %>

    Next, I have the customary Page_Load() function - the function begins with the definitions of two variables i.e. strXMLFile and strXSLTFile, to store path to the XML and XSLT code files respectively. Next, I have created instances of the XPathDocument() and XslTransform() classes for use in my script.

    Note that I could have opted for the more-familiar XmlDocument() object instead of the XPathDocument() object. However, if you are familiar with the Document Object Model (a.k.a. the DOM) and its .NET implementation i.e. the aforementioned XmlDocument() object, you will know that this is memory intensive, since the object is required to keep the entire tree structure of the XML data in memory at all times. In contrast, the XPathDocument() is a much more lightweight object that uses the relatively simpler and easy-to-understand XPath syntax to access the required nodes directly without needing to keep any information in memory.

    <%

    // snip

    <SCRIPT runat="server">

    void Page_Load() {
       
     // snip

    // load the XSLT style sheet
    objXSLTransform.Load(strXSLTFile);
       
    // create an instance of StringWriter object
    // to store result of transform
    StringWriter objSWriter = new StringWriter();
       
    // transform the XML file using the XSLT file
    // store the output in a StringWriter object
    objXSLTransform.Transform(objXmlFile, null, objSWriter, null);
       
    // display the output in the browser
    output.Text = objSWriter.ToString();

    // clean up memory
    objXmlFile = null;
    objXSLTransform = null;
       
    }
    </SCRIPT>

    // snip

    %>

    Next, I invoke the Load() method of the XslTransform() object to load the XSLT style sheet file - the location to the file stored in the variable "strXSLTFile" is passed as an input parameter. This is followed by the creation of an instance of the StringWriter() object that is passed as an output parameter to the Transform() method of the XslTransform() object.

    As you might have guessed, the Transform() method does all the hard work of applying the XSLT rules (present in the style sheet file) to the XML data (loaded in the local instance XPathDocument() object). This method in turn returns the transformed HTML output as a StringWriter() object.

    Finally, I have used the ToString() method of the StringWriter() object to assign the transformed output to the "Text" property of an ASP.Net "Label" server control for display in the browser.

    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 2 hosted by Hostway