Stylish XML
Part two: using XSL to transform your XML documents
Short URL: http://fsmsh.com/1257
- 2006-06-30
- Server side | Easy
-
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.
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.
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.
Write a full post in response to this!
Download the files attached with this article.
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.
Best voted contents
Buzz authors
Free Software news
- #freesoftware #fashion lo que puedas hacer hoy Al vivo la hogaza y al muerto la mortaja #youtube http://tinyurl.com/26vqjyy
- RT @turicas: The #Arduino #HackNBeer yesterday with @maddoghall at #UFF (in Niterói/RJ - Brazil) was amazing! \o/ #freesoftware #FTW
- Second Sole of Ohio | marymoome: http://tinyurl.com/25y6nzv #coolest #freesoftware #freesoftware Amor no respeta l... http://bit.ly/azeueY
- http://tinyurl.com/25y6nzv #coolest #freesoftware #freesoftware Amor no respeta ley ni obedece a rey A diario una manzana es cosa sana
- via @Developpez A new font for easier code writing: http://bit.ly/9AADsE under #OpenFontLicense #freesoftware
Other sites
- The Top 10 Everything (Dave). The good, the bad and the ugly.
- Free Software news (Dave & Bridget). All about free software -- free as in freedom!
- Book Reviews: Illiterarty (Bridget). Book reviews, blogs, and short stories.
Hot topics - last 60 days
-
10 years on: free software wins, but you have nowhere to install it
Tony Mobily, 2010-07-29 -
Tales From the Front: in Search of APT-GET UNDO
Rosalyn Hunter, 2010-08-13 -
Finding Free Music for a Free Film with Jamendo, VLC, and K3B
Terry Hancock, 2010-07-13 -
The Jargon of Freedom: 60 Words and Phrases with Context
Terry Hancock, 2010-07-24 -
MediaWiki and Script Translation for the Morevna Project
Terry Hancock, 2010-07-07
Hot topics - last 21 days
-
Net Neutrality: what does the Google Verizon proposal mean for GNU Linux?
Gary Richmond, 2010-08-16 -
The Bizarre Cathedral - 78
Ryan Cartwright, 2010-08-16 -
The Bizarre Cathedral - 79
Ryan Cartwright, 2010-08-24 -
Flip: A Simple Camera Done Right
Terry Hancock, 2010-08-31
Free Software Magazine uses Apollo project management and CRM for its everyday activities!








CSS
Submitted by Anonymous visitor on Sun, 2007-02-11 20:52.
Vote!So this will make Cascading Style sheets irrevalant?
- eric gawaters blog
[edited by Admin, comment spam removed]
Nice try...
Submitted by admin on Mon, 2007-02-12 01:01.
Vote!Hi,
Nice try.
To other comment spammers: we read _every_ single comment before posting. Beware.
Admin