In a previous article, I wrote about the basics of XSLT. With a simple text document and an XSLT processor you could convert an XML document into HTML. But that was a really basic document. Most XML documents have much more than one entity. They often have multiple nested entities and attributes of those entities.
XSLT is equipped to process complex XML as quickly and easily as simple. There are several parts to an XSL document:
- xsl:stylesheet element
- xsl:output element
- xsl:template elements
For this article, we will be working on converting an XML document that contains many address entries into a web page address book. Here is the XML we will be converting.
This is the element that starts every XSL document. In this element, you should define the version of XSLT you are using and the namespace of the specification. To convert the addresses, we’ll use XSLT version 1.0 with the specification at the W3C.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
This element is not required in your XSL documents, but it’s a good idea. You can indicate the method you want to output (XML, HTML, text), indent your text (or not), indicate the doctype, and other things. For the address book, we just need to indent the HTML and set it up as HTML output.
<xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>
The templates are the meat of your XSL document. They are where you define your HTML tags and other elements to be on your web page. The first and often only template you need is one that matches the entire XML document. This tells the XSL processor what to do with the entire XML document.
Once you’ve matched the entire XML document, you need to write your HTML elements. This is what will be written as your HTML document. So, like all HTML, you need the HTML tag, the HEAD tag, and so on. Here’s what it would look like:
There are many XSLT elements that you can use to convert your documents. For this XML document, we’ll be using two:
If you are a programmer, you are probably familiar with for each functions. But if you’re not, a for each function tells the processor to do the enclosed ommands on every instance of the element. For example, if you have 10 addresses in your address book, your for each statement would operate one at a time over each of them.
For the address book, I want to display each name in bold and then the address below, in this format:
City, State zip/postal code
When you want your XML elements to display in your XSLT output, you need to find out what the value is of those elements, and print them to the output. This is the most common XSLT element you will probably use. You can use it to display both the element contents and the attributes.
- to display the element contents
- to display the element attributes
<xsl:value-of select="address/street/@number"/>(the @-sign indicates that it’s an attribute)
The final step in the XSLT document is to close all the open tags, such as the xsl:for-each, xsl:template, and xsl:stylesheet. Then you’ll be ready to run your XSLT processor to create your HTML. For this style sheet, I used the Windows executable of James Clark’s XT.