Search content within the blog

Friday, April 24, 2009

Using XML, XSLT in Asp.net

XSLT is the W3C specification for formatting XML documents and displaying the contents in the required format.

XML File --books.xml

<?xml version="1.0" encoding="utf-8" ?>
<books>
<book>
<bookid> B001 </bookid>
<title> Understanding XML </title>
<Price> $30 </Price>
<Author>
<FirstName> Lily </FirstName>
<LastName> Hicks </LastName>
</Author>
</book>
<book>
<bookid> B002 </bookid>
<title> .NET Framework </title>
<Price> $45 </Price>
<Author>
<FirstName> Jasmine </FirstName>
<LastName> Williams </LastName>
</Author>
</book>
<book>
<bookid>B003</bookid>
<title>ASP.NET</title>
<Price>$35</Price>
</book>
</books>

XSLT file--- style_sheet.xslt

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Author">
<xsl:value-of select="FirstName"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="LastName"/>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>BOOKS AT WEBSHOPPE</TITLE>
</HEAD>
<BODY>
<H1>BOOKS AT WEBSHOPPE</H1>
<TABLE BORDER="3" CELLSPACING="2" CELLPADDING="6">
<THEAD ALIGN="CENTER" BGCOLOR="LightBlue">
<TH> BOOK ID </TH>
<TH> TITLE </TH>
<TH> PRICE </TH>
<TH> AUTHOR(S) </TH>
</THEAD>
<TBODY>
<xsl:for-each select="books/book">
<TR>
<TD>
<font color="green">
<xsl:value-of select="bookid"/>
</font>
</TD>
<TD>
<i>
<xsl:value-of select="title"/>
</i>
</TD>
<TD>
<u>
<xsl:value-of select="Price"/>
</u>
</TD>
<TD>
<b>
<xsl:apply-templates select="Author"/>
</b>
</TD>
</TR>
</xsl:for-each>
</TBODY>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>


ASPX Page Contents

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WithXML.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Xml id="Xml1" runat="server" DocumentSource="books.xml" TransformSource="style_sheet.xslt"></asp:Xml>
</form>
</body>
</HTML>

Output :

No comments:

Post a Comment