Abbey Workshop

XSLT: XSL-FO basics with FOP

There is a lot of information about XSLT on the Net, and it is getting more popular every day. But XSL is defined in two parts: eXtensible Style Language Transformations (XSLT) and eXtensible Style Language Formatting Objects (XSL-FO). XSL-FO allows you to transform XML into into print. Using an XSL-FO processor like FOP you can output XML to PDF, PostScript, PCL, or SVG.

This howto is by no means an all inclusive tutorial on XSL-FO (because there really is a lot to it, see the book below for more information), but it is meant to be a starting point. To use the source code in this example, you will need Ant 1.6.5 or later, the Apache Formatting Objects Processor (FOP) 0.20.5, and of course Java 1.5 to install the preceding two.

Let's get started. First, we need XML source to format. How about some Shakespeare? Below is the XML source for the Tempest.

tempest.xml

Download source for: tempest.xml

Using FOP from Ant

To use FOP from Ant, a task for FOP must be defined in the build.xml file. The Ant documentation on this is a bit confusing. You need to include a number of library .jar files in the classpath for the FOP task. However, the Ant documentation does not have the correct names for the files. The following example shows the correct settings for the versions identified earlier (FOP 0.20.5). If you are using a different version, the names of the library files may change.

build1.xml

Download source for: build1.xml

Generating a pdf is a two step process. First, you must generate an XSL-FO file with a .fo file extension. The .fo files contain the XML markup that describes how the output text will look. Once this file is generated with XSLT, then the FOP processor takes this file as input and generates a PDF file. The tasks create-fo and create-pdf represent these steps.

Generating a .fo File

Next, take a look at the .xsl file that generates our XSL-FO file.

bard_to_fo.xsl

Download source for: bard_to_fo.xsl

The first thing you will notice about the file is that XSL-FO is quite a verbose language. If you think about it, page layout is complex stuff so you need quite a bit of detail to describe things.

Take a look at the root template. If you have worked with a publishing tool like Framemaker, the markup will look pretty familiar to you. If you haven't, don't panic. This markup merely sets up the layout for the document as a whole. First you setup things like the width, height and margin of each page. In this example, I have setup master pages for odd numbered and even numbered pages. Further down, you will see that headers and footers are defined for each page type. The reason for this is you want to the text to appear in different parts of the page. For example, on an even numbered page you want the header left justified, on an odd number page, the header is right justified.

Further down you will see more typical templates for elements like ACT or SCENE. Most XSL-FO markup consists of blocks that describes the formatting for that piece of text. Typically, a block is a paragraph. If you need more granular formatting for words you can use inline elements for that purpose.

Summary

Well that's an overview. In each aspect of this howto there is a lot more detail that can be covered in each area. But if you donwnload the source, you should have a place to get started. Below I have included a few more files for additional information. In addition, click here to download the PDF file for The Tempest.

Here is the .fo file for The Tempest.

tempest.fo

Download source for: tempest.fo

Here is an XSL style sheet that generates an HTML. Just for compare and contrast purposes.

bard_to_html.xsl

Download source for: bard_to_html.xsl

For more information, see the following books

Google
  Web abbeyworkshop.com   

Copyright © Abbey Workshop 2006

Music Only Search
The Black MP3 Album
The MP3 Album Lyrics
Wprld Music Plus
Mp3 Songs Blog
Music Search Store
First Music Search
Mega MP3 Blog
E MP3 BLog
My MP3 Diary
Fast MP3 Finder
My Blog MP3
Mp3 Songs Mart
My MP3 Songs Album
Top Music Group
Mp3 Tune Records
Song Album Guide
Music DataDite
MP3 Buying Guide
Top MP3 Downloads
MP3 Tune Festival
FAST MP3 Downloads
MP3 Mart Online
Upper MP3
Premium MP3 Site
Music Data online
Fine MP3 Audio
Cheap Music Albums
Fine mp3 downloads
World MP3 Music Records
Mp3 Music Data Direct
The Fine Mp3
MP3 Files World
Home Music Store
DatMP3
Fat MP3 Audio
MP3 ZigZag
MP3 Songs Direct
MP3 Tune World
E MP3 Log
Boom MP3 Music
Speedy MP3
Top 20 MP3 music
Deep MP3 World
Deep MP3 Download
Best-20 MP3
Top Twenty MP3
Deep MP3
Big MP3 Guide
Deep MP3 Audio
Mp3 R.O.B.
MP3-Pop Online
Greate-20-MP3
MP3 Appear
3T-MP3