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

Formatting XML Files to PDF Files with XSLT

This section provides an example of formatting an XML file to a PDF file with XSLT stylesheets.

In the previous two examples, the input file is an XSL-FO file which contains the source information mixed with the XSL formatting objects directly. In this example, the source information is stored in an normal XML file, and the XSL formatting objects are mixed with the XSL transformation templates in an XSL file.

So I modified the old hello.xsl into hello_fo.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="p">
<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>
   -<xsl:value-of select="."/>-
   </fo:block>
  </fo:flow>
 </fo:page-sequence>
</fo:root>
 </xsl:template>
</xsl:stylesheet>

Note that:

  • The formatting objects are embedded as output of the transformation templates.
  • There are two name spaces used in this file, "xsl:" and "fo:".
  • The transformation template puts two "-" characters around the information of the root elements in the source XML file.

Let's run FOP with the following command:

fop -xml hello_xsl.xml -xsl hello_fo.xsl -pdf hello.pdf

Now if you open the output file, hello.pdf, with Adobe Acrobat Reader, you should see the greeting message, "-Hello world!-", showing at the top left corner of the page. This is perfect.

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
Formatting XML Files to PDF Files with XSLT