By David O'Neill
ConvertRStoXML() and Transforming With XSL, Part 2
In Part 1 we examined improving the
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
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.
- Create the XSL
- 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.
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="/"> 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
The next fourteen lines deal with how we want our page and table formatted. Nothing complicated here.
<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
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,
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
Server.MapPath(). You seem to have to pass a full file path to the
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),
(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!
David O'Neill is an MCSD and has been doing Web application development for the past five years.