Article
Build an XML/XSLT driven Website with .NET
Interested in .NET? Don't miss SitePoint's .NET Feature Guide -- it's an excellent resource!
Today, we'll build a simple dynamic Website using ASP.NET, C#, XML and XSLT. I'll assume you have prior working knowledge of ASP.NET, C#, XML and XSLT. The site source code provided here shows all details of the implementation, including error handling.
Why XML and XSLT?
There are a couple of different methods by which you can dynamically generate HTML, and they usually allow the writing of the presentation and the business logic to occur together. While writing small sites using these methods is fast, with the growth of the site, the lack of separation makes the maintenance and reuse of existing code difficult.
The XML/XSLT approach naturally separates the presentation from the underlying business logic. There are numerous benefits to this method:
- Better reuse of the business logic code in fast and reliable manner
- The creation of multiple alternative UI, so that serving clients with different capabilities or needs is easy and fast
- User customizations like co-branding, custom look and feel are possible
- Bigger development teams have specialized UI designers and core developers. The separation of the UI reduces the dependencies between the developers, thus improving productivity
- Even the UI unit testing is easier, as an XML document is all that's needed to reproduce particular tests
But if the UI/business logic separation is the stem of all these benefits, why use XML and XSLT for it?
Think of XSLT as HTML, because most of the code in XSLT is HTML. Given, there couple of rules to observe and the HTML has to be well-formed, but it's nothing dramatically different. There are free tools to help you convert HTML into XSLT. HTML Tidy, available at the W3C site, is one of them.
What about XML? Well, XML can represent any kind of data structure, and is used as input for the XSLT transformation. Most of the modern databases (SQL Server, Oracle, etc.) can return their query's result in XML format. In addition, database APIs (ADO) also can convert their results into XML format.
The Site Architecture
The architecture of the site we'll build is simple. For each HTTP request/response, the business logic and data management layers provide the appropriate data as XML. The presentation layer provides the XSLT and the different parameters for the transformation. The outcome of the XSLT transformation is HTML, which returns to the user.
The reusable presentation layer supports skins and globalization. The XSLT files are stored in a local directory called "UI". Each "skin" set of XSLT files is stored in a sub directory that bears the skin name. Each globalization set of XSLT files is stored in a subdirectory with the locale id, under the skin directory. The default skin has no name, and the same goes for the globalization set.
Here is how the directory tree looks:
UI
skin1
lcid1
lcid2
skin2
The UI directories contain all the UI files required for the final rendering on the client side, including images, CSS, java script, and more.
The application-independent and reusable functionality is isolated in class UIEngine in the UIEngine.cs file.
The class exposes three properties: ApplicationBase,Skin and Lcid, as well as the methods Transform, ExtractUIParameters and ExtractAction. ApplicationBase contains the relative path of the UI directory, Skin contains the current skin for the request, and Lcid contains the locale of the request.
The Transform method converts the XML data using the specified style sheet, and writes the resulting HTML to the response object.
Transform(HttpContext ctx, XPathDocument xmldoc,
string stylesheet, int lcid, XsltArgumentList arg)
Victor is the principle engineer for