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.
An XSL-FO document is divided into two main parts:
The following code sample shows how the page is broken up.
<?xml version="1.0" encoding="utf-8"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <!-- Define page templates using simple-page-master formatting objects inside of a layout master set. These templates are used to specify the basic rules for content layout; for example, the page margin size. --> <!-- Define page sequences, which use the templates defined above. These page sequences contain the actual content to be output. --> </fo:root>
Let's now look at a complete, though simple, FO document.
<?xml version="1.0" encoding="utf-8"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="hello" 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"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="hello"> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="18pt" text-align="center" font-weight="bold"> Hello World! </fo:block> </fo:flow> </fo:page-sequence> </fo:root>
The output is shown below.
All this simply to output "Hello World!"??? Yup. With FO, you need to do a little work to get things started. Let's break this code down.
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.
The layout-master-set formatting object (FO) holds simple-page-master FOs, which define page templates.
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.
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.
Note that blocks can contain nested blocks, which can contain nested blocks, and so on.