using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.XML;
using System.XML.Xsl;
public class XSLTTransform
{
public static void Transform()
{
SqlConnection
nwindConn = new SqlConnection("Data
Source=INMUMIS123;database=northwind;uid=sa;pwd=;");
nwindConn.Open();
DataSet
custDS = new DataSet("CustomerDataSet");
SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers",
nwindConn);
custDA.Fill(custDS, "Customers");
SqlDataAdapter ordersDA = new SqlDataAdapter("SELECT * FROM Orders",
nwindConn);
ordersDA.Fill(custDS, "Orders");
nwindConn.Close();
custDS.Relations.Add("CustOrders",
custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;
XMLDataDocument XMLDoc = new XMLDataDocument(custDS);
XSLTransform
XSLTran = new XSLTransform();
XSLTran.Load("transform.xsl");
// This is
for generating the output in new html
XMLTextWriter
writer = new XMLTextWriter("XSLT_output.html",
System.Text.Encoding.UTF8);
writer.Close();
// This is
for writing in the current page
XSLTran.Transform(XMLDoc, null, Response.OutputStream);
}
}
<%@ Page language="C#"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<meta name="GENERATOR"
Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE"
Content="C#">
<meta name="vs_defaultClientScript"
content="JavaScript (ECMAScript)">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<script>
public void Page_Load(object sender, System.EventArgs e)
{
Transform();
}
public void Transform()
{
SqlConnection nwindConn = new
SqlConnection("Data
Source=INMUMIS123;database=northwind;uid=sa;pwd=;");
nwindConn.Open();
DataSet custDS = new
DataSet("CustomerDataSet");
SqlDataAdapter custDA = new
SqlDataAdapter("SELECT * FROM Customers",
nwindConn);
custDA.Fill(custDS, "Customers");
SqlDataAdapter ordersDA = new
SqlDataAdapter("SELECT * FROM Orders",
nwindConn);
ordersDA.Fill(custDS, "Orders");
nwindConn.Close();
custDS.Relations.Add("CustOrders",
custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;
XMLDataDocument XMLDoc = new
XMLDataDocument(custDS);
XSLTransform XSLTran = new
XSLTransform();
XSLTran.Load("transform.xsl");
// XMLTextWriter writer = new
XMLTextWriter("XSLT_output.html",
System.Text.Encoding.UTF8);
XSLTran.Transform(XMLDoc, null,
Response.OutputStream);
// writer.Close();
}
</script>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>
<xsl:stylesheet XMLns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="CustomerOrders">
<STYLE>
BODY
TD
</STYLE>
<TABLE BORDER="1">
<xsl:apply-templates
select="Customers"/>
</TABLE>
</xsl:template>
<xsl:template match="Customers">
<TR><TD>
<xsl:value-of select="ContactName"/>,
<xsl:value-of select="Phone"/><BR/>
</TD></TR>
<xsl:apply-templates
select="Orders"/>
</xsl:template>
<xsl:template match="Orders">
<TABLE BORDER="1">
<TR><TD valign="top"><B>Order:</B></TD><TD
valign="top"><xsl:value-of select="OrderID"/></TD></TR>
<TR><TD valign="top"><B>Date:</B></TD><TD
valign="top"><xsl:value-of select="OrderDate"/></TD></TR>
<TR><TD valign="top"><B>Ship
To:</B></TD>
<TD valign="top"><xsl:value-of
select="ShipName"/><BR/>
<xsl:value-of
select="ShipAddress"/><BR/>
<xsl:value-of
select="ShipCity"/>, <xsl:value-of select="ShipRegion"/> <xsl:value-of select="ShipPostalCode"/><BR/>
<xsl:value-of select="ShipCountry"/></TD></TR>
</TABLE>
</xsl:template>
</xsl:stylesheet>