The XSL Pattern (which I discuss in "XSL to XSLT: Terminology and Background") in the select attribute of the xsl:value-of element in Listing 1 uses context(-2) to select the row that the template is processing. Using context(-2) seems peculiar because we're trying to access the previous context, not the second previous one. But the syntax is correct because context(-1) selects the current context. The XSL Pattern then selects all attributes in the row element by using @* and a predicate to select the one attribute that matches the desired column name for the table. The result is the HTML table that Figure 1 shows.
Converting the XSL style sheet that Listing 1 shows to XSLT is straightforward, even though XSLT has no context() operator. Instead of the context() operator, XSLT supports variables. An XSLT variable is a named storage location much like the variables that many programming languages use. The only difference is that XSLT variables are immutable; that is, after you declare a variable and set its value, you can't change that value. An XSLT variable can hold one value or a nodeset, which is a collection of nodes. You can use an XSLT variable to store the context node (as a nodeset with one node) before you execute a construct that changes the context, such as an xslt:for-each element. You can then use the variable to refer to the previously stored node within the new context.
Web Listing 2 shows an XSLT style sheet converted from the XSL style sheet in Listing 1. The XSL style sheet requires a number of updates to make it compatible with the XSLT standard. These updates include converting the order-by attributes to the xslt:sort element and adding namespace declarations for the prefixes referenced in the XML Path Language (XPath) select attributes. Callout A in Web Listing 2 shows the revised template for processing row elements.
The revised template for row elements begins by declaring the variable current_row, which the template sets to the row element being processed. The XPath expression '.' selects the current context node (the node being processed by the template). The XSLT style sheet iterates through the AttributeType elements in the XDR schema to output the table columns in the correct order. Directly after the xslt:for-each element, the template declares the attrName variable to hold the name of the AttributeType being processed. Together, the current_row and attrName variables are used with xslt:value-of to obtain the value of the attribute on the row element being processed. This produces the desired HTML table, except for formatting the columns that contain dates.
Formatting the date columns is easy if you use a Microsoft XPath extension. The format-date() function lets you use a pattern string to specify the date's format. The format-date() function takes two parameters: an expression that resolves to date and a format string. To use the extension function, you need to use a namespace to qualify the function. XPath uses namespaces to ensure that names of vendor-supplied extension functions won't collide with the names of future XPath built-in functions. The code at Callout B in Web Listing 2 uses the ms prefix, which the xslt:stylesheet element declares. An xslt:choose element performs a check similar to an if-then-else construct, common to programming languages, to determine whether the value of the attribute being processed is a dateTime type. If so, the style sheet formats the date by using the format-date() function. Otherwise, it outputs the value as is.
To run the example, you have to save the XSLT style sheet that Web Listing 2 shows to a file called RStoTable.xslt in a directory of your choice on your computer. Also, you have to save the Visual Basic (VB) script that Web Listing 3 shows to a file named FormatTable.vbs in the same directory. The script contains comments explaining the steps you need to take to execute the XSLT style sheet from Web Listing 2. You might need to update the script's connection string to access your database. To execute the script, enter
cscript FormatTable.vbs
at a command prompt. The script creates a file called debug.html in the directory you choose that contains the table output from the XSLT style sheet. You also need Microsoft XML Core Services (MSXML) 4.0 to run the example. (To download MSXML 4.0, go to http://msdn.microsoft.com/xml.)