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.

Table of Contents

 About This Book

 Ideas on Publishing Books using XML

 Overview of hyPub - A Simple XML Publishing Tool

 Writing Book Contents in XHTML Source Files

 Transforming Book Source Files with XSL Technology

 Designing Web Page Layouts with Tables

 Controlling Web Page Appearance with CSS

 Sharing HTML Codes with JavaScript Files

 Generating Web Pages with hyPub

Formatting Printable Documents with XSL-FO

 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

 Generating PDF Documents with hyPub

 Areas of Future Improvements

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2007
Formatting XML Files to PDF Files with XSLT