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 987. 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 988. Generating a sender's surname for printing. Create comment in forum
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"

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

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

A suitable XML document instance reads:

Figure 989. A memo document instance. Create comment in forum
<memo ...="memo.xsd">
  <date year="2005" month="1" day="6"/>
  <subject>Firewall problems</subject>
    <para>Thanks for your excellent work.</para>
    <para>Our firewall is definitely broken!</para>

Some remarks:

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


    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
    FO to PDF
    fo2pdf -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