When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Wednesday, January 16, 2002

Simplifying ConvertRStoXML() and Transforming With XSL, Part 2

By David O'Neill

In Part 1 we examined improving the ConvertRStoXML() function by making the XML more readable. Just this change improved the performance. In this final part we'll look at how to convert the XML produced by the ConvertRStoXML() function to HTML via XSL!

- continued -

Converting the XML Output to HTML Using XSL
André Dantas Rocha wrote: "I'm using your ConvertRStoXML() function and really love it! Actually I'm trying to modify your code (unsuccessfully) so it returns a HTML code (it returns XML now). I'm using Netscape 4.7 (it not supports XML) and want to do the processing on server. Is there a way to use a XSL (inside your code) to modify the generated XML and returns HTML to the browser? (I can't save the XML file to server, I need to do this on server memory)"

Great idea André! Lets look at how to do that. There are two things we have to do to convert the XML output to HTML using an XSL on the server.

  1. Create the XSL
  2. Use the XSL to transform the generated XML document to HTML

I have created a very simple, no-frills, no-gimmicks, XSL to demonstrate how this is done. I have designed all this to run against the Orders table of the Northwind database that is shipped with SQL Server, Visual Studio, and Minesweeper. Create a file called ConvertRStoXML.xsl and add the following piece of cookie-cutter code.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>

This defines the content of the file as being XML, points to its namespace, and sets the output. I'm not going to go into detail as to what all of this means since it's XSL 101 and is explained in about every second page on the Internet these days. (See the XSL Page at XML101.com for more information!) The next piece to be copied to your ConvertRStoXML.xsl file is the "business logic" in the XSL template.

<xsl:template match="/">
<xsl:for-each select="orders/order">
    <td><xsl:value-of select="OrderID"/></td>
    <td><xsl:value-of select="CustomerID"/></td>
    <td><xsl:value-of select="EmployeeID"/></td>
    <td><xsl:value-of select="OrderDate"/></td>
    <td><xsl:value-of select="RequiredDate"/></td>
    <td><xsl:value-of select="ShippedDate"/></td>
    <td><xsl:value-of select="ShipVia"/></td>
    <td><xsl:value-of select="Freight"/></td>

The line <xsl:template match="/"> defines a XSL template to match a certain pattern in our XML. In this case, it is matching the root so this template will apply to our whole XML document. The next fourteen lines deal with how we want our page and table formatted. Nothing complicated here. The <xsl:for-each select="orders/order"> block will loop through each "order" in the "orders" collection of data in our XML document. The next bunch of <xsl:value-of select="MyFieldName"/> lines simply extracts the values from the field MyFieldName and plunks it into that table cell. Really, the only thing easier than this is accidentally putting Caps Lock on while trying to type your Windows password! Last and least is closing the XSL style sheet, </xsl:stylesheet>.

Save this XSL file in your Web directory and we'll look at how to use it to convert the XML to HTML through an ASP page. All we have to do is define another XMLDOM object for our XSL and apply it to the XMLDOM object created by the ConvertRStoXML function.

Dim objXSL
Set objXSL = Server.CreateObject("Microsoft.XMLDOM")
objXSL.Load Server.MapPath("ConvertRStoXML.xsl")

Response.Write objXML.transformNode(objXSL)

Note the Server.MapPath(). You seem to have to pass a full file path to the Load() method and not just a relative path (i.e. just the file name). (See Using Server.MapPath for more information!) The next line applies the transformation to the XML document and outputs to the browser. Tada!

If you have SQL Server and the Northwind sample database installed, the source files should run without any modification. Just copy ConvertRStoXML.xsl (the StyleSheet), inc_ConverRStoXML.asp (the updated function), and driver.asp (a wrapper for testing) to your favourite virtual directory. Three new files. See how they run. See how they run. Happy Programming!

  • By David O'Neill


  • Download the source files (in ZIP format)

    David O'Neill is an MCSD and has been doing Web application development for the past five years.

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article