Stylish XML

Part two: using XSL to transform your XML documents

Download the whole article as PDF

Write a full post in response to this!


Part one of this article looked at how Cascading Style Sheets (CSS) can be used to make XML documents look good in a web browser. In part two, I’ll explore the more complex eXtensible Style sheet Language (XSL) and how it can be used to transform XML into HTML and PDF documents.

Limitations of XML/CSS

By the end of part one of this article I had a my tasty pico de gallo recipe marked up with XML tags and nicely styled using CSS. It looked great in my Firefox browser. Unfortunately, one of the major problems with using XML/CSS is that it doesn’t work for everyone. Older, proprietary browsers and text-only browsers can only understand HTML and get terribly confused when trying to interpret XML/CSS. If I want to share my delicious recipes with someone using Netscape 4 or Lynx, I’m going to have to convert my XML into a format that their browser can handle. This means leaving behind CSS and constructing a new style sheet using the eXtensible Style-sheet Language (XSL).

Unfortunately, one of the major problems with using XML/CSS is that it doesn’t work for everyone

As long as I’m in the mood for creating new things, I may as well create a new XML recipe to use for the examples in this article. You already have a recipe for a nice appetizer from part one, and now, for part two, you just need a drink to go with it. I can’t think of anything better to have with pico de gallo and tortilla chips than a cold margarita so that’s what I’ll use in the examples. By the way, the margarita recipe and all of the style sheets in this article are available as a compressed download.

Transforming with XSL

An XSL Transformation, or XSLT, is the process of transforming an XML document into another type of XML document. Now why on earth would someone want to transform one XML document into another XML document? I’ll give you a hint, Grasshopper: HTML is another type of XML document. I can build a custom XSL style sheet, apply it with a tool called an XSLT processor and presto, my XML is magically transformed into HTML.

In the examples, I will be using the XSLT processor called xsltproc to process the XSL style sheets. The xsltproc tool should be available as a package for most GNU/Linux distributions and Apple’s OS X, if you want to follow along. The basic syntax for the command is xsltproc -o [output-file] [style-sheet] [input-file]. Or, in the case of this article’s examples, xsltproc -o output.html recipe-style.xsl margarita.xml. If you don’t have access to xsltproc, or if you’re just feeling a little apathetic about typing all these commands, the output files from each of the examples are included alongside the other files in the compressed download.

An XSL Transformation is the process of transforming an XML document into another type of XML document

Simple beginnings

The most basic XSL style sheet is one that does nothing at all. Of course, there are a few headers that are required, but for the most part the style sheet is devoid of any processing instructions. When using a blank style sheet like this, it appears that xsltproc simply strips the tags from the XML recipe and dumps it as plain text to the output file. Close, but not quite. Take a look at the first line of the output and you will see a document-type declaration for HTML. This was added during processing, because the second line of the recipe-style.xsl specifies HTML as the output document. So I’m on the right track, but output.html displayed in a browser looks really bad. That’s because the only output I have produced so far is a plain-text file masquerading itself as HTML.

Output produced by an “empty” style sheet
Output produced by an “empty” style sheet

Introducing the XSL template

A real HTML document should have tags and currently output.html has none. To make a valid HTML file I’ll need to fix up the XSL style sheet to at least produce <html>, <head> and <body> tags in the appropriate places. This can be done by adding a single XSLT processing rule called a template. You can think of an XSLT template as being like a word processor’s find-and-replace feature. You use the “match” attribute to tell the template what XML element it should find. Everything between the template’s starting and ending tags is the replacement text. To convert my margarita recipe to HTML I need my style sheet to match the recipe element in the XML source file and replace it with appropriate HTML elements in the output document.

HTML tags are here, but the recipe’s gone!
HTML tags are here, but the recipe’s gone!

It wasn’t hard to add the template rule to my recipe-style.xsl style sheet and get the HTML tags I wanted to see. Unfortunately, although I am one step closer to a valid HMTL document, it seems that all of the recipe’s content has now disappeared. This is because I have started using processing rules in my style sheet, but I have not followed through by specifying where to place the recipe’s content. Adding a simple <xsl:apply-templates /> element between the HTML <body> tags will fix things up and get my recipe content back.

<apply-templates /> brings the recipe’s content back
<apply-templates /> brings the recipe’s content back
Don't miss out on the other pages!
12next ›last »

Write a full post in response to this!

Download the files attached with this article.

Similar articles

0

Do you like this post?
Vote for it!

Copyright information

This article is made available under the "Attribution-Sharealike" Creative Commons License 3.0 available from http://creativecommons.org/licenses/by-sa/3.0/.

Biography

David Horton: David Horton got started with GNU/Linux in 1996 when he needed a way to share a single dial-up internet connection with his college room-mates. He found the solution he needed with an early version of Slackware and a copy of the PPP-HOWTO from The Linux Documentation Project. More than ten years later he is older and wiser and still hooked on GNU/Linux. Many of Dave's interests and hobbies can be explored on his website.

Anonymous visitor's picture

CSS

Submitted by Anonymous visitor on Sun, 2007-02-11 20:52.

Vote!
0

So this will make Cascading Style sheets irrevalant?

- eric gawaters blog

[edited by Admin, comment spam removed]

admin's picture

Nice try...

Submitted by admin on Mon, 2007-02-12 01:01.

Vote!
0

Hi,

Nice try.
To other comment spammers: we read _every_ single comment before posting. Beware.

Admin