Publishing Books Using XML Technologies
Dr. Herong Yang, Version 4.01

Tranforming XML Files with XSLT Stylesheets

This section provides a simple XSL-FO (XSL Formatting Objects) document, hello.fo.

The FOP package also contains another XML tool called "xalan". It can be used to transform an XML file based the transformation templates defined in an XSL file. To run "xalan", create a batch file, xalan.bat, with the following command:

java -cp \local\fop-0.20.5rc2\lib\xercesImpl-2.2.1.jar;
   \local\fop-0.20.5rc2\lib\xalan-2.4.1.jar 
   org.apache.xalan.xslt.Process %1 %2 %3 %4 %5 %6

Now let's run "xalan" to transform hello_xsl.xml with hello_fo.xml:

xalan -in hello_xsl.xml -xsl hello_fo.xsl -out hello.out

Open hello.out with a text editor, you will see:

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"><fo:layout...
   -Hello world!-
   </fo:block></fo:flow></fo:page-sequence></fo:root>

This is exactly what I expected. The output generated from the transformation template contains the formatting objects and information from the source XML file. Note that the format is not well maintained during the transformation. The first line is too long, I have to truncate it.

So if you have some data organized in an XML file, you can write an XSL file with transformation templates to re-organize the data. In the same XSL file, you can also put some formatting objects to format the re-organized data into a pintable format.

You can finish the transformation and formatting process in two steps:

  • 1. Use "xalan" to transform the XML file with the XSL file into a FO file.
  • 2. Use "fop" to format the FO file into a PDF file.

Sections in This Chapter

What Is XSL-FO (XSL Formatting Objects)

Example of XSL-FO Document - hello.fo

Installing Apache FOP - XSL-FO Processor

Formatting XSL-FO Files to Text Files Using FOP

Formatting XSL-FO Files to PDF Files Using FOP

Formatting XML Files to PDF Files with XSLT

Tranforming XML Files with XSLT Stylesheets

Dr. Herong Yang, updated in 2007
Tranforming XML Files with XSLT Stylesheets