Constructing FO from XML documents

So far we have learnt some basic FO elements. As with HTML we typically generate FO code from other sources rather than crafting it by hand. The general picture is:

Figure 919. Different target formats from common source. Create comment in forum

(Animated PDF Version)

Different target formats from common source.

We may generate both online and printed documentation from a common source. This requires style sheets for the desired destination formats in question.


We discussed the FO standard as an input format for printable output production by a renderer. In this way a FO document is similar to HTML being a format to be rendered by a web browser for visual (screen oriented) output production. The transformation from a XML source (e.g. a memo document) to FO is still missing. As for HTML we may use XSL as a transformation means. We generate the sender's surname from a memo document instance:

Figure 920. Generating a sender's surname for printing. Create comment in forum
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simplePageLayout"
          page-width="294mm" page-height="210mm" margin="5mm">
          <fo:region-body margin="15mm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simplePageLayout">
        <fo:flow flow-name="xsl-region-body">
          <fo:block font-size="20pt">
            <xsl:text>Sender:</xsl:text>
            <fo:inline font-weight='bold'>
              <xsl:value-of select="memo/from/surname"/>
            </fo:inline>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

A suitable XML document instance reads:

Figure 921. A memo document instance. Create comment in forum
<memo ...="memo.xsd">
  <from>
    <name>Martin</name>
    <surname>Goik</surname>
  </from>
  <to>
    <name>Adam</name>
    <surname>Hacker</surname>
  </to>
  <to>
    <name>Eve</name>
    <surname>Intruder</surname>
  </to>
  <date year="2005" month="1" day="6"/>
  <subject>Firewall problems</subject>
  <content>
    <para>Thanks for your excellent work.</para>
    <para>Our firewall is definitely broken!</para>
  </content>
</memo>

Some remarks:

  1. The xsl_stylesheet element contains a namespace definition for the target FO document's namespace, namely:

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    This is required to use elements like fo:block belonging to the FO namespace.

  2. The option value indent="yes" in xsl_output is usually set to "no" in a production environment to avoid whitespace related problems.

  3. The generation of a print format like PDF is actually a two step process. To generate message.pdf from message.xml by a stylesheet memo2fo.xsl we need the following calls:

    XML document instance to FO
    xml2xml message.xml memo2fo.xsl -o message.fo
    FO to PDF
    fo2pdf -fo message.fo -pdf message.pdf

    When debugging of the intermediate FO file is not required both steps may be combined into a single call:

    fo2pdf -xml message.xml -xsl memo2fo.xsl -pdf message.pdf