XML
  Home arrow XML arrow Page 4 - 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 - Jumping higher


    (Page 4 of 7 )

    So far, the examples that I have demonstrated used the ASP.NET "Xml" server control. While such ready-to-use ASP.NET server controls make the job at hand a breeze to implement, the real power of the .NET platform lies in the vast number of assemblies that it comes equipped with to handle XML and related technologies, and this includes XSL Transformations.

    The XML data in the previous examples can be at best considered to belong to the novice category. When you're working on a real-world project, you are bound to come across something more complicated. Now it is time to approach the task of XSL Transformation in a different - and more complex - manner.

    For starters, let me update the XML file so that it contains information about a list of articles rather than a single article - this is more likely in a real-world project:

    <?xml version="1.0"?>
    <articles>
        <article id="110">
            <title>XSL Transformations with ASP.NET</title>
            <author>Harish R. Kamath</author>
            <category>ASP.NET</category>
            <abstract>Leverage on the built-in functionality available in ASP.NET to transform your XML documents using XSL Transformations!</abstract>
        </article>
        <article id="109">
            <title>Input Validation in ASP.NET</title>
            <author>Harish R. Kamath</author>
            <category>ASP.NET</category>
            <abstract>Prevent corrupt data from entering into your database - use built-in ASP.NET validation controls in your scripts.</abstract>
        </article>
        <article id="108">
            <title>Master the ASP.NET DataGrid Server Control</title>
            <author>Harish R. Kamath</author>
            <category>ASP.NET</category>
            <abstract>Learn how to use the powerful ASP.NET DataGrid server control to list data from a database table.</abstract>
        </article>
    </articles>

    Next, I have listed the updated XSLT style sheet to transform the above XML data into browser-compatible HTML format.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <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>

    Finally, I have the re-vamped ASP.NET script that brings about the XSL Transformation.

    <%@ Page Language="C#" %>
    <%@ import Namespace="System.Xml.Xsl" %>
    <%@ import Namespace="System.Xml.XPath" %>
    <%@ import Namespace="System.IO" %>
    <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();
       
    // 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>
    <HTML>
    <HEAD>
    <TITLE>ASPFree.com</TITLE>
    <BASEFONT face="Arial">   
    </HEAD>
    <BODY>
    <asp:Label id="output" runat="server" />
    </BODY>
    </HTML>

    Load this ASP.NET script in your browser to view the following output:

    XSLT Transformations using ASP NET

    With the formalities behind me, let me start dissecting the above code listing line-by-line.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek