XML
  Home arrow XML arrow Page 2 - Applying XSLT to XML 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 
Mobile Linux 
App Generation ROI 
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

Applying XSLT to XML Using ASP.NET
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 62
    2004-09-13

    Table of Contents:
  • Applying XSLT to XML Using ASP.NET
  • A Simple Example on XSLT
  • XSLT with ASP.NET
  • Query a Database and Transform through XSLT using ASP.NET
  • How to Transform More than One Row?

  • 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


    Applying XSLT to XML Using ASP.NET - A Simple Example on XSLT


    (Page 2 of 5 )

    Just to understand the structure of XSLT, let us test a small example on XSLT using a simple XML document without using any server-side (ASP.NET) code. This example can be directly executed by opening the XML document in any XSLT supported browser (currently I tested with IE6).

    So, let us start by creating a plain and simple XML document (sample1.xml) as follows.

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
    <Employees>
      <Employee>
        <Name>Stuart</Name>
        <Age>28</Age>
      </Employee>
    </Employees>

    Now we create an XSL style sheet (sample1.xslt) for the above XML document as follows.

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html>
      <body>
        <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/>
        <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/>
      </body>
      </html>
    </xsl:template>
    </xsl:stylesheet>

    Remember that the family of XML technologies are case sensitive. By this time, if you open “sample1.xml” directly in IE6 or NN6, you should be able to see the result something like the following:

    Name of Employee: Stuart
    Age: 28

    If you observe the HTML source at browser, it would be something like the following, which is nothing but the transformation applied through XSLT to the XML document:

    <html>
      <body>
        <b>Name of Employee : </b>Stuart<br>
        <b>Age:</b>28<br>
      </body>
    </html>

    Let us try to understand the above two files in detail now. “sample1.xml” is just a simple XML document. “sample1.xslt” is our style sheet definition to transform “sample1.xml”. If we carefully observe the sample1.xml file, we can spot an interesting line:

    <?xml-stylesheet type="text/xsl" href="sample1.xslt"?>

    This is the only magic needed in any XML file to be transformed to any XSLT. The above statement instructs browser to link the current XML document with “sample1.xslt” and present (transform) it accordingly. So, in general we will not see the XML document as a tree structure anymore. It will be presented (transformed) in a different way as specified in “sample1.xslt”.

    Now, let us study the most important lines in sample1.xslt.

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

    The above line defines that it is basically an XSLT document used for certain transformation.

    <xsl:template match="/">

    The above line specifies the format (template) applicable to the root of XML document. We can also specify individual templates for each and every XML element as well. The attribute ‘match’ can be provided with any XPath expression.

    <xsl:value-of select="Employees/Employee/Name" />

    And finally, the above line states that it has to get the value (text) present in that path (Employees/Employee/Name) of XML document (tree) starting from root. In this case it starts from root (/), then searches for Employees element (first element), within that Employee child element (first child element) and finally the child element Name (first child element) of Employee element. This path could be replaced with any XPath expression.

    The rest of the content should be easily understandable, as it mostly contains XHTML elements to be transformed. Make sure that this example always takes only the first elements into consideration. There is no use of placing more than one employee information in sample1.xml file. We will overcome this drawback in the later part of this article.

    More XML Articles
    More By Jagadish Chaterjee


       · this article help intermediates to know about the power of xml using xls.can help...
     

    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 6 hosted by Hostway
    Stay green...Green IT