Developer

XSL-FO makes XML documents flexible and multipurpose

XSL formatting objects (XSL-FO) can provide versatility in formatting for Web and print. Find out how this emerging dialect can ease formatting tasks.


Extensible Stylesheet Language Formatting Objects (XSL-FO) is an XML application for describing the page layout of an XML document. In other words, it can make the XML document both pretty and readable to nontechnical people. XSL-FO also provides the ability to display the same XML document in different formats for different purposes.

For example, consider an article for the adventure-related magazine Popular Spelunker. When the article is initially displayed, it is formatted for the Web, with navigational information along the top and side. For convenience, it could even be broken up into multiple Web pages. Although this is great for online reading, it leaves something to be desired when the folks at Popular Spelunker want to produce a paper edition of their magazine. Instead of creating two or more documents with the same content for each article, it's a lot more efficient to use XSL-FO to format the article’s XML document any way you want.

Imagine an article for the online edition of Popular Spelunker. The page would have a variety of links and ads. The article itself would be an XML document like that in Listing A, formatted for the Web using Extensible Stylesheet Language Transformations (XSLT).

Although this is of great use to the people who administer the online magazine, those who deal with paper would be out of luck. This is where XSL-FO comes into play. Its purpose is to format XML for printing in much the same way that IBM’s AFP formatted mainframe files for printing. XSL-FO formatted text produces the kind of output that looks nice printed on an 8.5-by-11 piece of paper. An XSL document, like the one in Listing B, would be used to reformat the XML document in Listing A.

How it works
The XML document in Listing A is used as input, along with the XSL document in Listing B, to an XSLT engine. The XSLT engine produces the XSL-FO document shown in Listing C. This XSL-FO document is then used as input by an XSL-FO formatter to produce a printable document.

A fly in the ointment
As cool as this approach sounds, it is not without some problems. Most notably, to most browsers, XSL-FO is just another XML document. If it weren't for some of the tools available on the Web, the entire process of producing an XSL-FO document would be an exercise in futility. For Popular Spelunker to get its articles to paper format, it needs a FOP. FOP stands for Formatting Object Processor or Formatting Objects to PDF—I’ve seen it called both. The Apache Project produces a FOP that fits nicely into my price range: It’s free.

With the FOP, the XSL-FO from Listing C produces a PDF file that can be both viewed online and printed. I recommend thoroughly examining it online before printing, because there is something enjoyable about tweaking a document to get it just right.

Conclusion
Although W3C requirements for XSL-FO haven’t quite firmed up yet, the possibilities of this technology warrant kicking the tires and taking it for a spin. When used with XSLT, it may even remove the drudgery of producing reports from a SQL database.

Editor's Picks