SIGN UP MEMBER LOGIN:    
ARTICLE

Transforming XML Document Using XSLT with XML Control

Posted by Sandeep Shekhawat Articles | XML in C# June 09, 2011
I am explaining the concept of transforming an XML Document using a well-defined XSLT incorporated with an Asp.Net XML Control.
Reader Level:
Download Files:
 


Introduction:

I am explaining the concept of transforming an XML Document using a well-defined XSLT incorporated with an Asp.Net XML Control.

W3C recommend two different style languages:

  1. CSS
  2. XLST

XSLT stands for EXtensible Stylesheet Language Transformations. The XSLT language transforms XML into a format interpreted by an internet browser. Generally we use XSLT when we want to do a transformation from an XML document into another XML document or convert an XML document into a HTML web page to create dynamic web pages.

Illustrate with an Example:

In our website we create an XML document. Here we will create a CollegeXML.xml document. It contains Student->Name and Branch like this:

<?xml version="1.0" encoding="utf-8" ?>
<College>
  <
Student>
    <
Name>Sandeep</Name>
    <Branch>MCA</Branch>
  </Student>
  <
Student>
    <
Name>Raviendra</Name>
    <Branch>B.Tech</Branch>
  </Student>
 </
College>


Now we create another document that is an XSLT document that transforms an XML document to a HTML document.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="College">
      <h1>Student Detail</h1>
      <table border="1" bgcolor="Green" width="250">
        <xsl:for-each select="Student">
          <tr>
            <
td> <xsl:value-of select="Name"/> </td>
            <td><xsl:value-of select="Branch"/> </td>
          </tr>
        </
xsl:for-each>
      </
table>
            </
xsl:template>
</xsl:stylesheet>

Elements in XSLT:

Element

Description

<xsl:stylesheet>

 

It is define root element of style sheet.

<xsl:output>

It is define format of output created by style sheet. It is top level element.

<xsl:template>

It contains rules to apply when a specified node is matched.

<xsl:for-each>

It allows us to loop through multiple nodes that match the selection criteria.

<xsl:value-of>

It can be used to select the value of an XML element and add it to the output.


Table 1: Elements in XLST

Now we use an ASP.Net XML control to display this XLST information.

<asp:Xml ID="college" runat="server" DocumentSource="~/CollegeXML.xml" TransformSource="~/CollegeXLST.xslt"></asp:Xml>

Property of XML Control:

Property

Description

DocumentSource

Source of XML Document

TransformSource

Source of XSLT Document


Table 2: Property of XML Control

OUTPUT:

Trans.gif

Note: Use CSS when you can, use XSL when you must.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor