Extensible Stylesheet Language Transformations (XSLT) specifies rules for transforming an XML document into another XML document or into another document type, such as an HTML file. To understand how this works, let’s start by creating an HTML select using the traditional, classic ASP method of looping through the recordset. Listing A shows the code.
Look familiar? Routines like the one in Listing A allow you to operate in your comfort zone. Listing B provides a look at how you can achieve the same results using XML and XSLT.
It seems kind of like magic, doesn’t it? No loop structure, no move next statement—it isn’t readily apparent how this code works, even if you are familiar with XML. So let’s break down what it does, line by line:
- · The first two lines create instances of the XML document object model, one for the recordset as XML and one for the XSLT.
- · The third line invokes ADO’s save method to save the entire recordset as an XML document.
- · The fourth line loads the XSLT from the server.
- · The fifth line transforms the XML recordset into an HTML select and loads it back into objXML.
- · The last line writes the XML, which is now really HTML.
This whole process almost seems too easy, doesn’t it? Well, I kind of glossed over the third and fourth lines, saving the recordset as XML and loading the XSLT. Saving the recordset as XML is pretty straightforward, but what the XML version of recordset looks like is much less clear.
When you save a recordset as XML, ADO includes namespaces and schemas for the recordset, along with the individual rows contained in the recordset. At first, all of this information can seem overwhelming. In addition, each row is saved as an XML element with the individual columns as XML attributes. While this isn’t a problem, remember it when writing the XSLT so that the document is well formed.
With this in mind, let’s tackle the fourth line. XSLT converts XML into another type of document. One of the big differences with XSLT is the addition of xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” for the XSL namespace, which identifies the parser tags beginning with xsl: as XSLT instructions, elements, and attributes. Since we’re working with an XML document that started out as an ADO recordset, the stylesheet will need a few more namespaces, so it winds like the code in Listing C.
This example serves as the starting point for anything that you may want to do with an ADO recordset. If you want to build an HTML select or a table, you start with this example. The more goes here is where the real magic takes place; this is where the logic would go to build an HTML select or table.
In Listing D, we’ve returned to Listing C’s <!– more goes here –> comment.
By changing what’s in the more goes here area, you can get different results. Instead of producing an HTML select object, you can produce an HTML table. For example, the code in Listing E can produce a table with a variable number of columns. If the recordset has two columns, the table will have two columns; if it has five, the table will have five. No matter how many columns the recordset has, the table will have the same number. The single drawback to this example is that it is not designed to handle null columns in the original recordset.
The use of ADO, XML, and XSLT allows a greater degree of flexibility in our code. My example code is written in VBScript for use on a server, but you aren’t limited to a particular platform. Once the ADO recordset has been saved as XML, it can, for example, be sent to the client’s browser where it can be transformed to HTML. It’s easy to see why XML, coupled with XSLT, is quickly becoming the lingua franca of online business.