PART-2
======
XSLT IN
DOTNET PLATFORM
(
DESKTOP-C# & ASP.net-C#)
We will now see three methods by which we can implement
such xsl transformation in ASP.net.
a) asp style modified as
aspx
b) using asp.net's asp:xml
tag
c) by using standard XML
classes in DotNet Framework.
--
a) asp style modified for asp.net
---------------------------------
The following aspx
file is a slight modification of familiar asp files.In aspx we have to
declare variables before defining. Also , there is no 'set' or 'get' methods.
----------------------------------------
//
d:\inetpub\wwwroot\xsldemo.aspx
<%
dim xml as object
xml=server.createobject("microsoft.xmldom")
xml.load(Server.MapPath("students.xml"))
dim xsl as
object
xsl=server.createobject("microsoft.xmldom")
xsl.load(Server.MapPath("xsl1.xsl"))
response.write(xml.transformNode(xsl))
%>
----------------------------------------------------------
We can start the IIS-5 server( normally it will be running).We
should remember to place the xml &
xsl file in the same folder.If we type the URL as "http://localhost/xsldemo.aspx",
we get correct result ( ie) the xml is transformed as per xsl1.xsl
and the result is displayed as a table with yellow background.
**********************************************************
It may be noticed that the above code is in VB.net!
Actually, it is asp code retrofitted for asp.net!The default language for
ASP.net is VB.net and that is why we are able to be indifferent to uppercase & lowercase.We can display the
result in any browser.
------------
b) using asp:xml tag in
asp.net
=================================
There is a far simpler
and elegant method in ASP.net by using asp:xml tag , as illustrated below.
//
d:\inetpub\wwwroot\xsldemo1.aspx
<%@ page language="c#"
debug="true" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Xml.Xsl" %>
<%@ import Namespace="System.Xml.XPath" %>
<%@ import Namespace="System.IO" %>
<script runat=server>
void job1(Object o,EventArgs e)
{
xmldso.DocumentSource="students.xml";
xmldso.TransformSource="xsl1.xsl";
}
</script>
<html>
<body>
<form
runat=server>
<asp:xml id=xmldso runat=server />
<asp:button text="click" onclick=job1
runat=server />
</form>
</body>
</html>
----------------------------------------------------------
Before taking up the code
for classical xml-transform in asp.net, we can see how it is done in console-mode c#.
The same packages are referenced in both .
The console-mode c# program follows:
// trax.cs
using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;
using System.Text;
class trax
{
public static void
{
StreamReader reader
=
new
StreamReader("students.xml");
XmlTextReader xmlreader
=
new
XmlTextReader(reader);
XPathDocument doc =
new
XPathDocument(xmlreader);
XslTransform xsldoc=
new
XslTransform();
xsldoc.Load("xsl1.xsl");
StreamWriter writer =
new
StreamWriter("result1.htm");
xsldoc.Transform(doc,null,writer);
Console.WriteLine("result sent to file!");
reader.Close();
xmlreader.Close();
}
}
==========================================================
As we have installed DotNet Framework SDK1.1 in our system, we get
default path to DotNet binaries. Therefore we can compile the above file from
any folder.
We will assume that we have created the above file
in g:\c#demo as trax.cs.
g:\c#demo>csc trax.cs
This will compile correctly.
Ensure that students.xml and xsl1.xsl are available in current
folder.
g:\c#demo>trax
(This will run the program and we get the message result sent to
file).We can now easily write similar code to asp.Net as shown below. Carefully
note how the 'using'
in DeskTop-C# is written as 'import Namespace' in ASP.Net.)
//d:/inetpub/wwwroot/trax.aspx
<%@ page language="c#" debug="true" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.IO" %>
<script runat=server>
public void Page_Load()
{
StreamReader
reader=
new
StreamReader(Server.MapPath("students.xml"));
XmlTextReader xmlreader=
new
XmlTextReader(reader);
XPathDocument doc=
new
XPathDocument(xmlreader);
XslTransform xsldoc=
new
XslTransform();
xsldoc.Load(Server.MapPath("xsl1.xsl"));
StreamWriter writer =
new
StreamWriter(Server.MapPath("result.htm"));
xsldoc.Transform(doc,null,Response.Output);
reader.Close();
xmlreader.Close();
}
</script>
<html>
<body>
<form runat=server>
</form>
</body>
</html>
So far, we have dealt with
the various methods of effecting transformation and sending it to the browser. If the
transformation is done in server-side, all browsers can display the html
correctly. But, in a tutorial supposedly on j2ee, we have not mentioned
java atall,so far. This, we take up in
part3