XSL-FO (XSL formatting)

XSL-FO (Extensible Stylesheet Language Formatting Objects) is a language used to describe the layout and appearance of XML documents. It’s mainly used for transforming XML documents into high-quality print outputs, such as PDFs or other page-oriented formats. XSL-FO is part of the XSL family, which includes XSLT for transformations and XPath for querying XML data.

Structure of XSL-FO

An XSL-FO document defines how XML content should be formatted on a page. It involves creating objects that represent pages, paragraphs, tables, and other layout elements. These objects specify how the content should be styled and laid out.

Basic Structure of XSL-FO

				
					<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    
  </fo:layout-master-set>
  <fo:page-sequence master-reference="pageMaster">
    
  </fo:page-sequence>
</fo:root>

				
			

The core structure includes:

  • fo:root: The root element of an XSL-FO document.
  • fo:layout-master-set: Defines page layout and structure.
  • fo:page-sequence: Contains the content to be rendered on the pages.

Basic Example: Formatting an XML Document into a PDF

Here’s an example of how to create a simple PDF output using XSL-FO.

XML Input

				
					<bookstore>
  <book>
    <title>XML Developer's Guide</title>
    <author>Author Name</author>
    <price>44.95</price>
  </book>
  <book>
    <title>Learn XSL-FO</title>
    <author>Another Author</author>
    <price>39.95</price>
  </book>
</bookstore>

				
			

XSL-FO Code:

				
					<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  
  <fo:layout-master-set>
    <fo:simple-page-master master-name="pageMaster" page-height="29.7cm" page-width="21cm" margin="2cm">
      <fo:region-body margin="2cm"/>
    </fo:simple-page-master>
  </fo:layout-master-set>

  
  <fo:page-sequence master-reference="pageMaster">
    <fo:flow flow-name="xsl-region-body">
      <fo:block font-family="Arial" font-size="14pt" font-weight="bold">
        Bookstore Inventory
      </fo:block>

      
      <fo:block>
        <fo:block font-size="12pt">
          <fo:inline font-weight="bold">Title: </fo:inline>
          XML Developer's Guide
        </fo:block>
        <fo:block>
          <fo:inline font-weight="bold">Author: </fo:inline>
          Author Name
        </fo:block>
        <fo:block>
          <fo:inline font-weight="bold">Price: </fo:inline>
          44.95
        </fo:block>

        <fo:block font-size="12pt" space-before="1cm">
          <fo:inline font-weight="bold">Title: </fo:inline>
          Learn XSL-FO
        </fo:block>
        <fo:block>
          <fo:inline font-weight="bold">Author: </fo:inline>
          Another Author
        </fo:block>
        <fo:block>
          <fo:inline font-weight="bold">Price: </fo:inline>
          39.95
        </fo:block>
      </fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>

				
			

Explanation

  1. fo:simple-page-master: Defines the page dimensions (A4 in this case) and margins.
  2. fo:block: Represents blocks of text, like paragraphs.
  3. fo:inline: Used to style inline elements like bold text for titles, authors, and prices.
  4. fo:page-sequence: Contains the content to be printed on the page. Here, it iterates over the books in the XML.

Output

When this XSL-FO is processed (using an XSL-FO processor like Apache FOP), it generates a PDF with the following content:

				
					Bookstore Inventory

Title: XML Developer's Guide
Author: Author Name
Price: 44.95

Title: Learn XSL-FO
Author: Another Author
Price: 39.95

				
			

Advanced Concepts in XSL-FO

Page Layout Control

XSL-FO allows you to control complex page layouts, such as multi-column pages or different headers/footers for odd and even pages.

  • Columns Example

				
					<fo:simple-page-master master-name="twoColumns" page-height="29.7cm" page-width="21cm" margin="2cm">
  <fo:region-body column-count="2" column-gap="1cm"/>
</fo:simple-page-master>

				
			

This defines a page with two columns.

Tables in XSL-FO

You can create tables to organize data in a grid structure.

  • Table Example

				
					<fo:table table-layout="fixed" width="100%">
  <fo:table-column column-width="5cm"/>
  <fo:table-column column-width="5cm"/>
  <fo:table-column column-width="3cm"/>
  
  <fo:table-header>
    <fo:table-row>
      <fo:table-cell><fo:block>Title</fo:block></fo:table-cell>
      <fo:table-cell><fo:block>Author</fo:block></fo:table-cell>
      <fo:table-cell><fo:block>Price</fo:block></fo:table-cell>
    </fo:table-row>
  </fo:table-header>
  
  <fo:table-body>
    <fo:table-row>
      <fo:table-cell><fo:block>XML Developer's Guide</fo:block></fo:table-cell>
      <fo:table-cell><fo:block>Author Name</fo:block></fo:table-cell>
      <fo:table-cell><fo:block>44.95</fo:block></fo:table-cell>
    </fo:table-row>
    
  </fo:table-body>
</fo:table>

				
			

This XSL-FO snippet creates a table with three columns for titles, authors, and prices.

Styling Text and Layout

XSL-FO supports a wide range of styling properties for text, such as:

  • Font properties (font-family, font-size, font-weight)
  • Text alignment (text-align, line-height)
  • Margins, padding, and borders.

Example: Styling Text

				
					<fo:block font-family="Arial" font-size="14pt" color="blue" text-align="center">
  Centered Blue Text
</fo:block>

				
			

This block styles the text to be centered, blue, and using a font size of 14 points.

XSL-FO Processors

To convert XSL-FO documents to PDF or other formats, you need an XSL-FO processor. Some common processors include:

  • Apache FOP (Formatting Objects Processor): Converts XSL-FO to PDF, PostScript, and other formats.
  • RenderX XEP: A commercial XSL-FO processor for high-quality print outputs.

XSL-FO is a powerful tool for formatting XML documents into print-ready outputs like PDFs. It offers a fine-grained level of control over page layout, typography, and content structure, making it ideal for applications where printed output is essential. With the examples provided, you now have a foundational understanding of XSL-FO, from basic formatting to advanced layout control, enabling you to efficiently design complex documents. Happy coding !❤️

Table of Contents