How XSL-FO Works
Transforming XML for print is accomplished by transforming an XML document to a Formatting Objects (FO) document, which itself is XML-based, via XSLT. The formatting objects processor is able to read the FO document and transform it for different types of print output. The most common and best supported print output is currently Adobe PDF.
In a moment, we'll take a look at our first FO document, but first let's look at how FO sees a page.
Note that the XSL specification requires the border-width to be 0 on all regions. However, RenderX's XEP and Antenna House both support the ability to put borders on regions.
As you can see, the page is divided into regions. The main region is the region-body. The regions above and below the body are called region-before and region-after and the regions to the right and left of the body are called region-start and region-end. We will examine page layout more closely in the Page Layout lesson.
XSL-FO Document Parts
An XSL-FO document is divided into two main parts:
- One layout master set (layout-master-set), which specifies one or more page master templates (simple-page-master) to be used in the document.
- One or more page sequences, which contain the content of the document.
The following code sample shows how the page is broken up.
Let's now look at a complete, though simple, FO document.
The first thing to note is that this is an XML document with a root element of root with the namespace http://www.w3.org/1999/XSL/Format.
layout-master-set and simple-page-master
The layout-master-set formatting object (FO) holds simple-page-master FOs, which define page templates.
page-height="11in" page-width="8.5in" margin-top="1in"
margin-bottom="1in" margin-left="1in" margin-right="1in">
<fo:region-body margin-top="1in" margin-bottom=".5in"/>
In this case, there is only one template and it is named "hello". The page height, width and margins are all defined in the simple-page-master FO.
The simple-page-master FO contains region settings in the form of the following child FOs.
The locations of these regions are shown on the diagram under How XSL-FO Works. Our simple "Hello-World" document only uses the region-body.
The layout-master-set FO is followed by one or more page-sequence FOs, which must reference one of the simple-page-master templates by name. The page-sequence FOs contain the contents of the page or pages.
Notice that the value of the master-reference attribute matches the master-name defined in the simple-page-master FO.
flow and static-content
The contents of the page inside the page-sequence FO are either static, meaning they appear the same on each page or flowing, meaning they flow from one page to the next. Static content is contained in static-content FOs and flowing content is contained in flow FOs.
This example only has a flow FO. In most cases, the region-body has flowing content and the other regions have static content.
Paragraphs, lists and other elements that generally are vertically separated with whitespace are called blocks. The block FO is used for separating and formatting generic blocks, such as paragraphs. In our "Hello-World" example, there is only one block, which contains the text "Hello World!" and sets its font size, font weight and alignment via attributes.
You may notice that the formatting properties of the block FO are Cascading Style Sheets properties. Formatting Objects use CSS for display properties, so, if you use CSS in web design, youÃ¢â¬â¢ll find that most of your knowledge is transferrable to XSL-FO.
<fo:block font-size="18pt" text-align="center"
Note that blocks can contain nested blocks, which can contain nested blocks, and so on.