1. Home
  2. Computing & Technology
  3. Web Design / HTML

XSLT: Advanced
Transforming a Complex Document

By , About.com Guide

Last week we discussed the basics of XSLT. You saw that 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.

xsl:stylesheet
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">

xsl:output
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"/>

xsl:template
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.

<xsl:template match="/">

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>, and so on. Here's what it would look like:

 <xsl:template match="/">
  <html>
   <head>
    <title>Address Book</title>
   </head>
   <body>
    <h2>Address Book</h2>

There are many XSLT elements that you can use to convert your documents. For this XML document, we'll be using two:

  • xsl:for-each
  • xsl:value-of

xsl:for-each
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 commands 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:

Name
Street Address
City, State zip/postal code
Country

xsl:value-of
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 <xsl:value-of select="name/first"/>
  • 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.

Explore Web Design / HTML
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Web Design / HTML
  4. XML
  5. XSLT
  6. XSLT: Advanced>

©2009 About.com, a part of The New York Times Company.

All rights reserved.