Abbey Workshop | ||||||||||
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.
Download source for: tempest.xml
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.
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.
Next, take a look at the .xsl file that generates our XSL-FO file.
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.
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.
Download source for: tempest.fo
Here is an XSL style sheet that generates an HTML. Just for compare and contrast purposes.
Download source for: bard_to_html.xsl
Copyright © Abbey Workshop 2006