Developing XSLT-based Applications in ASP.NET 2.0
(Page 1 of 7 )
This is the first article in a series focused on developing XSLT-oriented applications using ASP.NET 2.0. Even though the title includes ASP.NET 2.0, I use it only as a transformation engine. The entire focus will be on working with XSLT. You can use any transformation engine according to your requirements (such as Java or others).
A
downloadable zip file is available for this article.
The entire solution (source code) for this article is available as a free download in the form of a zip file. All the applications in this series have been developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Standard Edition together with Microsoft SQL Server 2005 Developer Edition as the database. I didn’t really test any of the code in any of the other tools/IDEs/servers/editions/versions. If you have any problems, please feel free to post in the discussion area.
How to transform XML to XSLT using ASP.NET 2.0
Transforming XML to XSLT using ASP.NET 2.0 is quite easy. Working with the XML control (available in the toolbox) makes it easy to transform any XML document to the specified XSLT.
As it is a bit tedious to always author XML documents, I simply converted the output of a result set (or dataset) to an XML document by using the following method:
Private Function getXMLContent(ByVal SQL As String) As XmlDocument
Dim ds As New DataSet("SQLData")
Dim da As New SqlDataAdapter(SQL, "data source=laptop2k3sql2k5;initial catalog=AdventureWorks;user id=sa;password=eXpress2005")
da.Fill(ds, "Rows")
da.Dispose()
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
ds.Dispose()
Dim docXML As New XmlDocument
docXML.LoadXml(sw.ToString())
sw.Close()
Return docXML
End Function
The above method accepts a SELECT query and returns XML in the form of an object of type “XmlDocument.” You may need to modify the above method to suit your production requirements.
Now, let us see how to transform the XML document to the specified XSLT. The following is the code used to achieve the same:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim docXML As XmlDocument = getXMLContent("select EmployeeID, FirstName, JobTitle from HumanResources.vEmployee")
Me.txtXML.Text = docXML.InnerXml
Dim xp As New XPath.XPathDocument(New XmlTextReader(New IO.StringReader(docXML.InnerXml)))
Xml1.XPathNavigator = xp.CreateNavigator
Xml1.TransformSource = Server.MapPath("XSLTFile01.xsl")
End Sub
To work with the above code, you need to drag and drop an XML control from the toolbox onto the web page. You may have to modify the name of the XSLT file according to your design or theme.
Next: Testing XSLT using ASP.NET 2.0 >>
More ASP.NET Articles
More By Jagadish Chaterjee