Microsoft Office Online
Sign in to My Office Online (What's this?) | Sign in

 
 
Microsoft Office Access
Search
Search
 
Check for updates: (c) Microsoft
Office downloads
 
 
 
Warning: You are viewing this page with an unsupported Web browser. This Web site works best with Microsoft Internet Explorer 6.0 or later, Firefox 1.5, or Netscape Navigator 8.0 or later. Learn more about supported browsers.

Email this linkEmail this link Printer-Friendly VersionPrinter-Friendly Version Bookmark and ShareShare
Transforming XML files with XSLT in Access
 
Applies to
Microsoft Office Access 2003
Microsoft Access 2002

One of the principal advantages of using Extensible Markup Language (XML) as a data interchange standard between applications is the ease of transforming XML data documents into other formats and structures, such as HyperText Markup Language (HTML) files. The "vehicle" used to define these transmutations is an XML-based language called Extensible Stylesheet Language: Transformations (XSLT).

XSLT is a subset of another XML-based language: the Extensible Stylesheet Language (XSL). XSL is used to define the formatting of XML documents, and XSLT contains templates and commands to select and manipulate the structure of the data.

To understand how XSLT works, we need to look at how browsers, such as Microsoft Internet Explorer, handle XML data. When an XML data file is opened in a browser, the browser uses a built-in parser to parse the XML data into a tree-like structure. If a reference to an XSLT document is found in the XML data file, the browser then opens the style sheet file and applies it to the input tree. The browser cycles through the source file, attempting to match patterns within the XSLT file to specific elements (sometimes called nodes) in the XML data file. When it finds a match, the browser applies any matching templates to the elements and outputs the data according to rules in the template. The template may contain commands to manipulate the data, or HTML tags to apply formatting, or both. The result is an output tree reflecting any changes made by the templates and HTML tags added to change the formatting of the file. The browser then parses the output tree and displays it based on the HTML instructions. Thus at its lowest level, XSLT is used to convert one tree structure into another tree structure.

ShowGeneral transformation process

The process of transforming an existing XML data file into another format can be summarized as follows:

  1. First, create an XSLT file (with an .xsl file extension), outside Microsoft Access, by using a text editor such as Notepad. In this file, add processing instructions to convert and restructure your data as necessary. You need a processing instruction for each element or group (branch) of elements you want in your output document.

    For more information about creating XSLT files, visit the Microsoft Developer Network (MSDN).

  2. Insert a processing tag in the XML data file (.xml file) which points to the XSLT file.
  3. Use an XSLT processor to apply the XSLT style sheet to the XML data file to produce the resultant output file (an .htm file).

There are several XSLT processors available, including the Microsoft MSXML3 processor which can be downloaded free from the Microsoft Developer Network (MSDN ) Web site. The MSXML3 processor can also installed with Microsoft Office XP and Microsoft Windows XP. To run the examples appearing later in this article, you will need to have the MSXML3 processor installed on your system.

In addition to creating an XSLT file manually by entering instructions into a text processor, the Access Export XML dialog box also provides options to save the format of your data to an XSL file. The XSL (.xsl) file produced by Access can be used as a starting point for creating your own XSLT file.

ShowStructure of XSLT and XML files

To understand the examples that follow, we need to briefly review how XML identifies data. Like HTML, XML and XML-based files (such as XSL and XSLT) use element tags and attribute assignments to provide information about the data and its structure. A typical XML element consists of a start-tag, the element data, and an end-tag. An attribute is normally contained in the element start-tag as a name-value pair and provides additional information about the element. The general form of an element tag and attribute is:

<START-TAG attribute="value">element data<END-TAG>.

All XML and XML-based files have a similar structure. Similar to the Unix and DOS operating systems, an XML data file has a hierarchic structure which begins with a root node and continues down into parent and child elements (an inverted tree structure with the root at the top proceeding down to branches and leaves). The root node of an XML data file is considered to be the outermost node of the document (i.e., containing the entire document).

After the root node, the next element encountered in an XML data file is the outmost element tag called the Document element. This element contains all of the other elements, attributes, and text (designated as children of this element) which make up the document.

ShowRendering XML in a browser

So how is the XSLT (.xsl) file used? The input XML data file (.xml) contains a tag, typically starting with "<xsl:stylesheet", that references the XSLT file. When you open the XML data file in a Web browser, such as Microsoft Internet Explorer, the browser parses the input data into a tree-like structure and then calls an XML processor (such as the MSXML3 processor). The processor processes the XSLT file against this data tree to produce an output data tree. This output structure is then parsed to produce an .htm file which is displayed in the browser. If no XSLT (or other style sheet) reference exists in the XML data file, the data is displayed as raw XML data or in a default structure, depending on the browser used. As you'll see, Internet Explorer has a default XSL style sheet that displays XML files as an expandable tree-like structure.

An XSLT file consists of a series of templates containing navigating instructions for traversing the input XML data tree. When the processor encounters a template instruction in the XSLT file, it then scans the input XML data tree until it finds a "node" (an element) that matches the template. That element or series of elements (branches) are processed and written out to a result tree along with any HTML formatting that is included within the template. Conditional processing commands (such as if-then and sort structures) can also be included within the template to modify the structure of the output data, as needed. The resulting document is an .htm file that is then read by the browser.

ShowAn example of an XSLT style sheet

The following is an example of an XSLT file that we will be working with later in this article:

1<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" version="1.0">
2<xsl:template match="/">
3<HTML>
<HEAD>
<TITLE>Employees</TITLE>
</HEAD>
<BODY>
<h4>Sorted list of employee names</h4>
4<xsl:for-each select="//Employees">
<xsl:sort select="LastName" order="ascending"/>
<xsl:value-of select="LastName"/>,
<xsl:value-of select="FirstName"/>
<br/>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

1 Standard XSLT file heading consisting of an element start tag identifying this as a style sheet. The xmlns:xsl section defines a namespace prefix. For more information on namespaces, see the Microsoft Developer Network (MSDN) Web site.
2 XSL template rule which is triggered when a particular part of the source document is encountered.
3 HTML tags for defining formatting information.
4 XSL conditional programming instruction and processing body.

This style sheet is designed to process an XML file consisting of a group of employee records and output two of the fields from those records as LastName, FirstName. The XML data file can be seen further down in this article. We will create this file later. For now, lets look at the style sheet in a little more detail.

The first XSL template encountered is

<xsl:template match="/">
. This template instruction, demonstrating the standard form of most templates, tells the processor to find the node (the names "nodes" and "elements" are used interchangeably) matching the root node ("/") in the input XML data file and start processing all the child elements of that node. In essence, this template instruction tells the processor to start processing the input document. This template may or may not appear in all XSLT files. In many cases, if a template is missing from an XSLT file and the processor doesn't know how to proceed, a default template is invoked which tells the processor to "process all of the children of the current node."

The "/" is actually an XPath expression. XPath is used in conjunction with XSLT to navigate the elements and attributes of an XML data structure. A discussion of XPath is beyond the scope of this article but additional information on XPath can be found on the Microsoft Developers Network (MSDN ) Web site.

After including some HTML formatting tags for the output document, the next XSLT instruction

<xsl:for-each select="//Employees">
tells the processor to move (denoted by "//") relative to the current position (in this case, from the root node) to the child elements of the Employees element tag and start iterating through the child elements of that tag. The
<xsl:sort select="LastName" order="ascending"/>
instruction sorts the LastName element in ascending order. The
<xsl:value-of select="LastName"/>, and <xsl:value-of select="FirstName"/>
instructions select the LastName and FirstName elements, respectively, and output the value of those two elements. The remaining tags provide more formatting information or provide closing tags for all of the open tags. XML is much stricter than HTML in that XML requires a closing tag for every opening tag. Overlapping nested tags aren't allowed in XML.

To summarize the process, XSLT uses templates and instructions that are triggered when a particular element or branch of elements in the XML data file are encountered. Template instructions locate elements within 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 that the data is to be output, regardless of the actual order of the elements in the input 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.

ShowConverting an XML data file

To see how a transformation is done, let's follow an example of converting an XML data file consisting of data from a Microsoft Access database into a formatted .htm file for display in a Web browser. This will highlight some of the techniques we have already discussed.

We'll create the Employees.xml file and then look at a default .xsl style sheet in action by using Microsoft Internet Explorer with no explicit style sheet specified.

  1. Open the Northwind sample database. The Northwind database is typically installed with Microsoft Office and is usually located at C:\Program Files\Microsoft Office\Office10\Samples.
  2. Export the Employees table as an XML document by pointing to Export from the File menu and selecting XML Documents from the Save as type drop-down list. Keep the default file name "Employees.xml". For more information on exporting as XML, see Access Help.
  3. Locate the Employees.xml file and double-click to open.
  4. 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 the default XSLT style sheet used by Internet Explorer to transform the data.

Default XML output from Internet Explorer:


<?xml version="1.0" encoding="UTF-8" ?> 
- <dataroot>
-   <Employees>
      <EmployeeID>1</EmployeeID> 
      <LastName>Smith</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>      ....


Now let's apply the XSLT style sheet that we saw earlier to the Employees.xml data file. Remember, to get the results described in this article, you will need to have the latest version of the MSXML3 processor installed on your system. Internet Explorer 4 and Internet Explorer 5 shipped with an older version of MSXML which is based on XSL specifications that are now outdated. MSXML3 should be installed in Replace mode (as described in the MSXML3 Readme file) to overwrite the existing processor.

To create and test your XSLT file:

  1. Open a text editor such as Microsoft Notepad.
  2. Copy and paste the following XSLT file into a blank document. Note that this is same file that we described earlier with the graphic callouts removed.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
    <HTML>
    <HEAD>
    <TITLE>Employees</TITLE>
    </HEAD>
    <BODY>
    <h4>Sorted list of employees</h4>
    <xsl:for-each select="//Employees">
    <xsl:sort select="LastName" order="ascending"/>
    <xsl:value-of select="LastName"/>,
    <xsl:value-of select="FirstName"/>
    <br/>
    </xsl:for-each>
    </BODY>
    </HTML>
    </xsl:template>
    </xsl:stylesheet>

  3. Save the file to the same location as the Employees.xml file. Name the file "SortNames.xsl". Be sure to include the .xsl extension.
  4. Open the Employees.xml data file with a text editor such as Notepad.
  5. Place your cursor at the end of the first line in the document and press ENTER.
  6. Copy and paste the following line of text into the blank line you created:

    <?xml-stylesheet type="text/xsl" href="SortNames.xsl"?>

    This line tells the browser to apply the SortNames.xsl style sheet when you open Employees.xml.

  7. Save your changes to the Employee XML data file.
Now double-click the Employee.xml data file to open it. It should open in your browser and display a list of employee names sorted by last name.

Output from the SortNames.xsl file:

Sorted list of employees

Buchanan, Steven
Callahan, Laura
Davolio, Nancy
Dodsworth, Anne
Fuller, Andrew
King, Robert
Leverling, Janet
Peacock, Margaret
Suyama, Michael

Now let's look at another XSLT style sheet than can also be used with the Employees.xml file. This example returns a table of employees.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="1.0">
 <xsl:template match="dataroot">
    <html><body>
    <h1>A list of employees</h1>
    <table width="640">
    <xsl:apply-templates/>
    </table>
    </body></html>
 </xsl:template>

 <xsl:template match="Employees">
    <tr>
    <td><xsl:number/></td>
    <td><xsl:value-of select="LastName"/></td>
    <td><xsl:value-of select="FirstName"/></td>
    <td><i><xsl:value-of select="Title"/></i></td>
    </tr>
 </xsl:template>

 </xsl:stylesheet>


In this example, we see the standard XSLT heading, followed by the

<xsl:template match="dataroot">
template rule. Notice that we didn't include the root node template so the default processing all of the children of the current node will be invoked. Since we are at the beginning of the document, the root node is the current node.

The

<xsl:template match="dataroot">
instruction tells the processor to locate the "dataroot" element and then start processing its children. Some HTML tags, including a table definition, are then output to the result tree. Then we see the
<xsl:apply-templates/>
instruction. This is a frequently used instruction that says "select all of the children of the current node ("dataroot") in the input, and for each one, find its matching template rule and process it." In this case, the processor finds the rule relating to the child of the "dataroot" element which is the
<xsl:template match="Employees">
rule and processes it. In this template, we again output some the HTML tags and then the
<td><xsl:number/></td>
instruction which outputs a sequential number for each row in our table. The
<td><xsl:value-of select="LastName"/></td>
selects the LastName element and sends its value to the output. The same occurs for the remaining elements. Had these elements had children, we could have used the
<xsl:apply-templates/>
here and inserted matching template rules in the document to handle these elements also. And finally, we provide closing tags for all open tags.

You should now process this style sheet from the Employees.xml file using the instructions from the earlier example. Remember to change the reference in the Employees.xml file to point to this style sheet.

The output of the Employees.xml file and this style sheet is as follows:

A list of employees

1 Davolio Nancy Sales Representative
2 Fuller Andrew Vice President, Sales
3 Leverling Janet Sales Representative
4 Peacock Margaret Sales Representative
5 Buchanan Steven Sales Manager
6 Suyama Michael Sales Representative
7 King Robert Sales Representative
8 Callahan Laura Inside Sales Coordinator
9 Dodsworth Anne Sales Representative

ShowConclusion

In this article, we introduced the XSLT and XSL languages and described their file structures and a few processing instructions. We discussed their use in transforming XML data files from one format to another. We also looked at output of the default XSLT style sheet used in Internet Explorer and two of our own custom XSLT files. Hopefully, these concepts will provide a starting point for building your own XSLT and XSL files and using them in your own applications. For more detailed information on any of the concepts presented in this article, search the Microsoft Developers Network (MSDN ) Web site.

For more information about programming in Access, visit the Office Developer Center on the Microsoft Developer Network (MSDN).

Get Office 2007
Get Office 2007
advertisement