Herong's Tutorial Notes On XSL-FO and XHTML
Dr. Herong Yang, Version 2.00

XSL Formatting Objects (XSL-FO)

Part:   1   2 

This tutorial helps you to understand:

  • What is XSL Formatting Objects (XSL-FO)
  • "Hello world!" Example of XSL-FO
  • Installing Formatting Objects Processor (FOP)
  • Formatting an XSL-FO File to a .txt File
  • Formatting an XSL-FO File to a .pdf File
  • Formatting XML and XSLT Files to a .pdf File
  • Transforming XML and XSLT Files

What is XSL Formatting Objects (XSL-FO)

XSL-FO: An XML based markup language that defines how the embedded information should be formatted for viewing or printing. XSL-FO is very similar to TeX, which was a very popular language 15 years ago for writing well formatted scientific documents.

Main features of XSL-FO:

  • XSL-FO is XML based.
  • XSL-FO can be used inside XSL transformation stylesheet files. In this case, the source XML will be processed twice: 1. The source XML will be transformed according to the stylesheet - the result will contain the transformed information with XSL-FO statements; 2. The transformed information will be formatted according to the XSL-FO statements.

"Hello world!" Example of XSL-FO

Here is my first stand alone XSL-FO file, hello.fo:

<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <fo:layout-master-set>
  <fo:simple-page-master master-name="my_page" margin="0.5in">
   <fo:region-body/>
  </fo:simple-page-master>
 </fo:layout-master-set>
 <fo:page-sequence master-reference="my_page">
  <fo:flow flow-name="xsl-region-body">
   <fo:block>Hello world!</fo:block>
  </fo:flow>
 </fo:page-sequence>
</fo:root>

Note that:

  • The root element is named as "fo:root".
  • All the element names are prefixed with "fo:".
  • Prefix "fo:" is associated with the XML name space: "http://www.w3.org/1999/XSL/Format". This is done by the attribute, "xmlns:xsl" in the root element.

The next question is then do we have an application that can process this XSL-FO file to format the embedded information? I tried the following:

  • I don't know how to make IE to work with XSL-FO.
  • I don't know how to make XML Spy to work with XSL-FO. It can be used to edit XSL-FO files. But how to preview the formatted output?
  • I searched the Internet and located FOP from Apache. It can be used to format XSL-FO file into PDF files.

Installing Formatting Objects Processor (FOP)

After a quick research on the Internet, FOP (Formatting Objects Processor) seems to be the most popular XSL-FO processor. So here is how to download and install FOP.

1. Go to http://xml.apache.org/dist/fop/ and download fop-0.20.5rc2-bin.tar.gz

2. Open fop-0.20.5rc2-bin.tar.gz with WinZIP and extract everything to directory, like \local\fop-0.20.5rc2.

3. Create a batch file, run_fop.bat, with following command:

   \local\j2sdk1.4.1_01\bin\java -cp \local\fop-0.20.5rc2\build\fop.jar;
   \local\fop-0.20.5rc2\lib\xercesImpl-2.2.1.jar;
   \local\fop-0.20.5rc2\lib\xalan-2.4.1.jar;
   \local\fop-0.20.5rc2\lib\avalon-framework-cvs-20020806.jar;
   \local\fop-0.20.5rc2\lib\batik.jar org.apache.fop.apps.Fop
   %1 %2 %3 %4 %5 %6

Make sure that you enter everything into one line, and leave no space between the .jar file names. Because of the screen limitation, I have to split the command into multiple lines.

Now run the batch file in a command window. If you are getting the following:

USAGE
Fop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-ps
   |-txt|-at|-print] <outfile>
 [OPTIONS]
  -d          debug mode
  -x          dump configuration settings
  -q          quiet mode
  -c cfg.xml  use additional configuration file cfg.xml
  -l lang     the language to use for user information
  -s          for area tree XML, down to block areas only
...

the installation is ok.

According to the documentation, FOP can be used to:

  • Format an XSL-FO file to a .txt, .pdf, or .ps file.
  • Transform and format an XML file and an XSLT file to a .txt, .pdf, or .ps file.

".pdf" is called Portable Document Format (PDF), and ".ps" is called PostScript (PS).

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XSL-FO and XHTML - XSL Formatting Objects (XSL-FO)