Vous êtes sur la page 1sur 7

XSL FO

XSL Formatting Objects, or XSL-FO, is a markup language for XML document formatting that
is most often used to generate PDF files. XSL-FO is part of XSL (Extensible Stylesheet
Language), a set of W3C technologies designed for the transformation and formatting of XML
data.

What is XSL-FO?
XSL-FO is a language for formatting XML data
XSL-FO stands for Extensible Stylesheet Language Formatting Objects
XSL-FO is a W3C Recommendation
XSL-FO is now formally named XSL

1.XSL-FO Documents

XSL-FO documents are XML files with output information. They contain information about the
output layout and output contents.
XSL-FO documents are stored in files with a .fo or a .fob file extension. It can also be stored
with an .xml extension because this makes them more accessible to XML editors.

XSL-FO Document Structure


XSL-FO documents have a structure like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

<fo:layout-master-set>
<fo:simple-page-master master-name="A4">
<!-- Page template goes here -->
</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="A4">
<!-- Page content goes here -->
</fo:page-sequence>
</fo:root>

Structure explained
XSL-FO documents are XML documents, and must always start with an XML declaration:
<?xml version="1.0" encoding="ISO-8859-1"?>
The <fo:root> element is the root element of XSL-FO documents. The root element also declares
the namespace for XSL-FO:
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<!-- The full XSL-FO document goes here -->
</fo:root>

The <fo:layout-master-set> element contains one or more page templates:


<fo:layout-master-set>
<!-- All page templates go here -->
</fo:layout-master-set>

Each <fo:simple-page-master> element contains a single page template. Each template must
have a unique name (master-name):
<fo:simple-page-master master-name="A4">
<!-- One page template goes here -->
</fo:simple-page-master>

One or more <fo:page-sequence> elements describe the page contents. The master-reference
attribute refers to the simple-page-master template with the same name:
<fo:page-sequence master-reference="A4">
<!-- Page content goes here -->
</fo:page-sequence>

2.XSL-FO Areas

The XSL formatting model defines a number of rectangular areas (boxes) to display output.
All output (text, pictures, etc.) will be formatted into these boxes and then displayed or printed to
a target media.
We will take a closer look at the following areas:
Pages
Regions
Block areas
Line areas
Inline areas

a)XSL-FO Pages

XSL-FO output is formatted into pages. Printed output will normally go into many separate
pages. Browser output will often go into one long page. XSL-FO Pages contain Regions.
b)XSL-FO Regions

Each XSL-FO Page contains a number of Regions:


region-body (the body of the page)
region-before (the header of the page)
region-after (the footer of the page)
region-start (the left sidebar)
region-end (the right sidebar)
XSL-FO Regions contain Block areas.

c) XSL-FO Block Areas

XSL-FO Block areas define small block elements (the ones that normally starts with a new line)
like paragraphs, tables and lists.
XSL-FO Block areas can contain other Block areas, but most often they contain Line areas.

d)XSL-FO Line Areas


XSL-FO Line areas define text lines inside Block areas.
XSL-FO Line areas contain Inline areas.

e) XSL-FO Inline Areas


XSL-FO Inline areas define text inside Lines (bullets, single character, graphics, and more).

XSL-FO Example

<?xml version="1.0" encoding="ISO-8859-1"?>


<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

<fo:layout-master-set>
<fo:simple-page-master master-name="A4">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="A4">
<fo:flow flow-name="xsl-region-body">
<fo:block>Hello W3Schools</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
The output from this code would be something like this:
Hello W3Schools

3.XSL-FO Pages

XSL-FO uses page templates called "Page Masters" to define the layout of pages.

XSL-FO Page Templates

XSL-FO uses page templates called "Page Masters" to define the layout of pages. Each template
must have a unique name:

XSL-FO Page Size


XSL-FO uses the following attributes to define the size of a page:
page-width defines the width of a page
page-height defines the height of a page

XSL-FO Page Margins


XSL-FO uses the following attributes to define the margins of a page:
margin-top defines the top margin
margin-bottom defines the bottom margin
margin-left defines the left margin
margin-right defines the right margin
margin defines all four margins

XSL-FO Page Regions


XSL-FO uses the following elements to define the regions of a page:
region-body defines the body region
region-before defines the top region (header)
region-after defines the bottom region (footer)
region-start defines the left region (left sidebar)
region-end defines the right region (right sidebar)

Note that the region-before, region-after, region-start, and region-end is a part of the body region.
To avoid text in the body region to overwrite text in these regions, the body region must have
margins at least the size of these regions.
Margin Top

REGION BEFORE

R M
M
E a
a R
G r
r E
I g
g G
O i
i I
N REGION BODY n
n O
L N
S R
e E
T i
f N
A g
t D
R h
T t

REGION AFTER

Margin Bottom

XSL-FO Example
This is an extract from an XSL-FO document:
<fo:simple-page-master master-name="A4"
page-width="297mm" page-height="210mm"
margin-top="1cm" margin-bottom="1cm"
margin-left="1cm" margin-right="1cm">
<fo:region-body margin="3cm"/>
<fo:region-before extent="2cm"/>
<fo:region-after extent="2cm"/>
<fo:region-start extent="2cm"/>
<fo:region-end extent="2cm"/>
</fo:simple-page-master>
The code above defines a "Simple Page Master Template" with the name "A4".
The width of the page is 297 millimeters and the height is 210 millimeters.
The top, bottom, left, and right margins of the page are all 1 centimeter.
The body has a 3 centimeter margin (on all sides).
The before, after, start, and end regions (of the body) are all 2 centimeters.
The width of the body in the example above can be calculated by subtracting the left and right
margins and the region-body margins from the width of the page itself:
297mm - (2 x 1cm) - (2 x 3cm) = 297mm - 20mm - 60mm = 217mm.

4.XSL-FO Blocks

XSL-FO output is normally nested inside <fo:block> elements, nested inside <fo:flow>
elements, nested inside <fo:page-sequence> elements:

<fo:page-sequence>
<fo:flow flow-name="xsl-region-body">
<fo:block>
<!-- Output goes here -->
</fo:block>
</fo:flow>
</fo:page-sequence>

Block Area Attributes


Blocks are sequences of output in rectangular boxes:
<fo:block
border-width="1mm">
This block of output will have a one millimeter border around it.
</fo:block>

Since block areas are rectangular boxes, they share many common area properties:
space before and space after
margin
border
padding
space before
margin
border
padding

content

space after
The space before and space after is the empty space separating the block from the other blocks.
The margin is the empty area on the outside of the block.
The border is the rectangle drawn around the external edge of the area. It can have different
widths on all four sides. It can also be filled with different colors and background images.
The padding is the area between the border and the content area.
The content area contains the actual content like text, pictures, graphics, or whatever.

5.XSL-FO Lists

XSL-FO List Blocks


There are four XSL-FO objects used to create lists:
fo:list-block (contains the whole list)
fo:list-item (contains each item in the list)
fo:list-item-label (contains the label for the list-item - typically an <fo:block> containing
a number, character, etc.)
fo:list-item-body (contains the content/body of the list-item - typically one or more
<fo:block> objects)

6.XSL-FO Tables

XSL-FO uses the <fo:table-and-caption> element to define tables.

XSL-FO Tables
The XSL-FO table model is not very different from the HTML table model.
There are nine XSL-FO objects used to create tables:
fo:table-and-caption
fo:table
fo:table-caption
fo:table-column
fo:table-header
fo:table-footer
fo:table-body
fo:table-row
fo:table-cell
XSL-FO uses the <fo:table-and-caption> element to define a table. It contains a <fo:table> and
an optional <fo:caption> element.
The <fo:table> element contains optional <fo:table-column> elements, an optional <fo:table-
header> element, a <fo:table-body> element, and an optional <fo:table-footer> element. Each
of these elements has one or more <fo:table-row> elements, with one or more <fo:table-cell>
elements.

Vous aimerez peut-être aussi