Previously, we discussed exporting your data (or Access object) to an XML file with the properties and characteristics it had while in Access. Suppose you wanted to "transform" your data (or object) into another format; for display in a Web browser, for example. To accomplish this, you might want to restructure the data by filtering or sorting, and by adding HTML tags to tell the browser how to display the data. You could do this manually by adding HTML tags to the .xml file and rearranging the data, which could be a rather tedious task, or you could use another type of XML-based file to do the transformation: an XSL Transformation (XSLT) file.
XSLT files (XSLT commands are a subset of XSL commands) are designed primarily for transforming one XML document into another. While this may sound limiting, XSLT is also well-suited for transforming XML to HTML or to other text-based formats.
What's the difference between XSL and XSLT? An XSL file defines the formatting and presentation of XML documents to a screen, printer, or other display device. An XSLT file actually performs the transformation.
The process of transforming a document is accomplished as follows:
- Create an XSLT file (with an .xsl file extension just like XSL), outside Access, by using a text editor such as Microsoft Notepad. In this file, you add processing instructions to convert and restructure your data, as necessary. You need a processing instruction for each element or group (branch) of elements that you want in your output document.
- Add a reference tag that points to the XSLT file from within the XML data file (.xml file).
- Use an XSLT processor to apply the XSLT style sheet to the XML data file. This will produce the necessary output file (a .htm file).
There are several XSLT processors available for use including the Microsoft MSXML3 processor which can be downloaded without charge from the Microsoft Developer Network (MSDN) Web site. To run the following examples, you will need the MSXML3 processor installed on your hard disk.
How does XSLT work?
Like XSL, XSLT uses templates and instructions triggered when a particular element or branch of elements in the XML data file are encountered. Template instructions locate elements with the data file and specify what output to process. Much of the template body consists of HTML tags and text for output. Instruction commands tell the processor what part of the data to output. Instructions handing particular elements or branches of elements are placed in the order in which the data is to be output, regardless of the actual order of the elements in the XML data file. This provides the means to restructure the data to fit your needs. Conditional programming instructions allow you to filter and sort the data. The combination of these techniques provides powerful tools for transforming your XML data into virtually any format.
To get your first look at a default .xsl style sheet in action, we'll use Microsoft Internet Explorer to look at a file exported from the Access Northwind Traders sample database.
- Open the Northwind sample database. The default location of the Northwind.mdb database is C:\Program Files\Microsoft Office\Office11\Samples, for Access 2003 — C:\Program Files\Microsoft Office\Office10\Samples, for Access 2002.
- Export the Employees table as an XML document. For more information on exporting as XML, see Access Help.
- Locate the Employees XML file (*.xml) and double-click the icon.
- Assuming your default browser is Internet Explorer, the file will be displayed as a tree structure with expandable branches of elements. This tree structure is created from an XSLT file used by Internet Explorer to transform the data.
Default XML output from Internet Explorer illustrating the expanded data sections:
<?xml version="1.0" encoding="UTF-8" ?>
- <dataroot>
- <Employees>
<EmployeeID>1</EmployeeID>
<LastName>Davolio</LastName>
<FirstName>Nancy</FirstName>
<Title>Sales Representative</Title>
<TitleOfCourtesy>Ms.</TitleOfCourtesy>
<BirthDate>1968-12-08T00:00:00</BirthDate>
<HireDate>1992-05-01T00:00:00</HireDate>
<Address>507 - 20th Ave. E. Apt. 2A</Address>
<City>Seattle</City>
<Region>WA</Region>
<PostalCode>98122</PostalCode>
<Country>USA</Country>
<HomePhone>(206) 555-9857</HomePhone> ....
This is a portion of the exported Employees XML data file:
<?xml version="1.0"
encoding="UTF-8"?>
<dataroot>
<Employees>
<EmployeeID>1</EmployeeID>
<LastName>Davolio</LastName>
<FirstName>Nancy</FirstName>
<Title>Sales Representative</Title>
<TitleOfCourtesy>Ms.</TitleOfCourtesy>
<BirthDate>1968-12-08T00:00:00</BirthDate>
<HireDate>1992-05-01T00:00:00</HireDate>
<Address>507 - 20th Ave. E. Apt.
2A</Address>
<City>Seattle</City>
<Region>WA</Region>
<PostalCode>98122</PostalCode>
<Country>USA</Country>
<HomePhone>(206) 555-9857</HomePhone>
...
<ReportsTo>5</ReportsTo>
</Employees>
</dataroot>
Now let's look at a simple XSLT file example. This file can be used to display select fields from the first record of the Employees XML data file created earlier. Later, you'll see the steps to test it yourself.
XSLT file designed to display the first record in the Employees table
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<BODY>
<h4>First record of the Employees table</h4>
<p><xsl:value-of select="//EmployeeID"/></p>
<p><xsl:value-of select="//TitleOfCourtesy"/></p>
<p><xsl:value-of select="//FirstName"/></p>
<p><xsl:value-of select="//LastName"/></p>
<p><i><xsl:value-of select="//Title"/></i></p>
<p><xsl:value-of select="//HomePhone"/></p>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
First two lines contain required XML processing instructions.
First template rule tells the processor to start processing at the start of the XML data file. The "/" indicates that this rule is to be triggered as soon as the document starts to be processed.
Start of the HTML tags that we want included in our output .htm document.
HTML heading that we want to appear at the top of our output document.
Start of a series of template rules that find the element tags indicated in the select statement and output the value of those tags to our output document. The "//" in front of the values are navigating symbols that indicate to the processor (MSXML3) that it will need to go down two levels (in our Employees XML data file, this would be the Document element "dataroot", down through the "Employees" element, to the desired element "EmployeeID", for example) to reach the desired tag.
Start of the corresponding close tags. Each beginning tag must have a closing tag.
To test this file, you will need to:
Output from the first sample .xsl file:
First record of the Employees table
1
Ms.
Nancy
Davolio
Sales Representative
(206) 555-9857
Notice from the output that we limited the number of fields displayed and changed their order from the Employees table (first name then last name). We included additional information in the output display which wasn't in the original document (document title). We also added additional formatting such as the PARAGRAPH (<p>) tags and the ITALICIZED (<i>) tags to the Title element.
Now we will now look at another sample .xsl file that will display certain fields for all of the records in the Employee XML file.
XSLT file to display all of the records in the Employees table

<HTML xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
<BODY>
<h4>All records of the Employees table</h4>
<xsl:for-each select="//Employees">
<br><xsl:value-of select="EmployeeID"/></br>
<br><xsl:value-of select="TitleOfCourtesy"/></br>
<br><xsl:value-of select="LastName"/></br>
<br><xsl:value-of select="FirstName"/></br>
</xsl:for-each>
</BODY>
</HTML>
Contains the required XML processing instruction, this time combined to show that you can integrate them with HTML instructions.
Start of the HTML tags to include in the output .htm document.
First instruction, which tells the processor to find the Employees tag. Notice that this tells the processor that it will need to go down two levels to find the tag (root element and the Document element "dataroot") .
Start of a series of instructions that locate the tags displayed in the select statement and output the value of those tags to the output document.
Start of the corresponding close tags. Remember each beginning tag must have a corresponding closing tag.
To run this sample, you will need to use the same steps as with the first example. Save the XSLT file with a different name from the first.xsl file you created. Be sure to change the reference in the Employees XML file to the new .xsl file. Note that you will need to delete the blue background callouts (
) from the file after pasting the text into the text editor and before running the file from the processor.
Double-clicking on the Employees XML file will display the following in your browser:
First record of the Employees table
1
Ms.
Davolio
Nancy
2
Dr.
Fuller
Andrew
3
Ms.
Leverling
Janet
...
9
Ms.
Dodsworth
Anne
In this output, we changed the spacing of the element data by using BREAK <br> HTML tags instead of PARAGRAPH <p> tags between the elements. We chose to output fewer elements than we did in the first example. We also reversed the order of the first name and the last name. These are just a few of the techniques available to transform your data. Hopefully, you will continue to experiment with these and other techniques to discover how XSL and XSLT can be used to create the types of data files that you need.