Você está na página 1de 12

XSL-FO TUTORIAL

Introduction to XSL-FO
XSL-FO is about formatting XML data for output.

What You Should Already Know


Before you study XSL-FO you should have a basic understanding of XML and XML Namespaces.

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
• XSL-FO is About Formatting
• XSL-FO is an XML-based markup language describing the formatting of XML data for output to
screen, paper or other media.
• XSL-FO is Formally Named XSL

Why this confusion? Is XSL-FO and XSL the same thing?


Yes it is, but we will give you an explanation:
Styling is both about transforming and formatting information. When the World Wide Web Consortium (W3C)
made their first XSL Working Draft, it contained the language syntax for both transforming and formatting
XML documents.
Later, the XSL Working Group at W3C split the original draft into separate Recommendations:
• XSLT, a language for transforming XML documents
• XSL or XSL-FO, a language for formatting XML documents
• XPath, a language for navigating through elements and attributes in XML documents

The rest of this tutorial is about formatting XML documents: XSL-FO, also called XSL.
• XSL-FO is a Web Standard
• XSL-FO became a W3C Recommendation 15. October 2001. Formally named XSL.

XSL-FO Documents
XSL-FO documents are XML files with output information.

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 is also quite common to see XSL-
FO documents 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"?>

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>
Note: The master-reference "A4" does not actually describe a predefined page format. It is just a name. You
can use any name like "MyPage", "MyTemplate", etc.

XSL-FO Areas
XSL-FO uses rectangular boxes (areas) to display output.

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
• 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.


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.


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.

2
• XSL-FO Block areas can contain other Block areas, but most often they contain Line areas.
XSL-FO Line Areas
• XSL-FO Line areas define text lines inside Block areas.
• XSL-FO Line areas contain Inline areas.

XSL-FO Inline Areas


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

XSL-FO Output
• XSL-FO defines output inside <fo:flow> elements.
• XSL-FO Page, Flow, and Block

"Blocks" of content "Flows" into "Pages" and then to the output media.

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>

XSL-FO Example
It is time to look at a real 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>

Hello
Hello W3Schools
W3Schools

XSL-FO Flow
XSL-FO pages are filled with data from <fo:flow> elements.

XSL-FO Page Sequences


• XSL-FO uses <fo:page-sequence> elements to define output pages.
• Each output page refers to a page master which defines the layout.
• Each output page has a <fo:flow> element defining the output.
• Each output page is printed (or displayed) in sequence.

XSL-FO Flow
XSL-FO pages are filled with content from the <fo:flow> element.

3
The <fo:flow> element contains all the elements to be printed to the page.
When the page is full, the same page master will be used over (and over) again until all the text is printed.

Where To Flow?
The <fo:flow> element has a "flow-name" attribute.
The value of the flow-name attribute defines where the content of the <fo:flow> element will go.

The legal values are:


• xsl-region-body (into the region-body)
• xsl-region-before (into the region-before)
• xsl-region-after (into the region-after)
• xsl-region-start (into the region-start)
• xsl-region-end (into the region-end)

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:
<fo:simple-page-master master-name="intro">
<fo:region-body margin="5in" />
</fo:simple-page-master>
<fo:simple-page-master master-name="left">
<fo:region-body margin-left="2in" margin-right="3in" />
</fo:simple-page-master>
<fo:simple-page-master master-name="right">
<fo:region-body margin-left="3in" margin-right="2in" />
</fo:simple-page-master>

In the example above, three <fo:simple-page-master> elements, define three different templates. Each
template (page-master) has a different name.
The first template is called "intro". It could be used as a template for introduction pages.
The second and third templates are called "left" and "right". They could be used as templates for even and
odd page numbers.

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.

4
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.

Note that the regions (region-start and region-end) are not a part of the calculation. As described earlier,
these regions are parts of the body.

XSL-FO Blocks
• XSL-FO output goes into blocks.
• XSL-FO Pages, Flow, and Block
"Blocks" of content "Flow" into "Pages" of the output media.

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

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
Block Margin
• margin
• margin-top
• margin-bottom
• margin-left
• margin-right
• Block Border

Border style attributes:


• border-style
• border-before-style
• border-after-style
• border-start-style
• border-end-style
• border-top-style (same as border-before)
• border-bottom-style (same as border-after)
• border-left-style (same as border-start)
• border-right-style (same as border-end)

Border color attributes:


• border-color
• border-before-color
• border-after-color
• border-start-color
• border-end-color
• border-top-color (same as border-before)
• border-bottom-color (same as border-after)
• border-left-color (same as border-start)
• border-right-color (same as border-end)

Border width attributes:


• border-width
• border-before-width
• border-after-width
• border-start-width
• border-end-width
• border-top-width (same as border-before)
• border-bottom-width (same as border-after)
• border-left-width (same as border-start)
• border-right-width (same as border-end)
Block Padding
• padding
• padding-before
• padding-after
• padding-start
• padding-end
• padding-top (same as padding-before)
• padding-bottom (same as padding-after)
• padding-left (same as padding-start)
• padding-right (same as padding-end)

Block Background
• background-color
• background-image
• background-repeat
• background-attachment (scroll or fixed)

Block Styling Attributes

6
Blocks are sequences of output that can be styled individually:
<fo:block
font-size="12pt"
font-family="sans-serif">
This block of output will be written in a 12pt sans-serif font.
</fo:block>

Font attributes:
• font-family
• font-weight
• font-style
• font-size
• font-variant

Text attributes:
• text-align
• text-align-last
• text-indent
• start-indent
• end-indent
• wrap-option (defines word wrap)
• break-before (defines page breaks)
• break-after (defines page breaks)
• reference-orientation (defines text rotation in 90" increments)

Example
<fo:block
font-size="14pt" font-family="verdana" color="red"
space-before="5mm" space-after="5mm">
W3Schools
</fo:block>
<fo:block
text-indent="5mm"
font-family="verdana" font-size="12pt"
space-before="5mm" space-after="5mm">
At W3Schools you will find all the Web-building tutorials you
need, from basic HTML and XHTML to advanced XML, XSL, Multimedia
and WAP.
</fo:block>

Result:

W3Schools

At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to
advanced XML, XSL, Multimedia and WAP.

When you look at the example above, you can see that it will take a lot of code to produce a document with
many headers and paragraphs.
Normally XSL-FO document do not combine formatting information and content like we have done here.
With a little help from XSLT we can put the formatting information into templates and write a cleaner content.

XSL-FO Lists
XSL-FO uses List Blocks to define 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)

7
• 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)

An XSL-FO list example:


<fo:list-block>
<fo:list-item>
<fo:list-item-label>
<fo:block>*</fo:block>
</fo:list-item-label>
<fo:list-item-body>
<fo:block>Volvo</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label>
<fo:block>*</fo:block>
</fo:list-item-label>
<fo:list-item-body>
<fo:block>Saab</fo:block>
</fo:list-item-body>
</fo:list-item>
</fo:list-block>

The output from this code would be:


* Volvo
* Saab

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:
<fo:table-and-caption>
<fo:table>
<fo:table-column column-width="25mm"/>
<fo:table-column column-width="25mm"/>
<fo:table-header>
<fo:table-row>
<fo:table-cell>

8
<fo:block font-weight="bold">Car</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-weight="bold">Price</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block>Volvo</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>$50000</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>SAAB</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>$48000</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:table-and-caption>

The output from this code would something like this:


Car Price
Volvo $50000
SAAB $48000

XSL-FO and XSLT


XSL-FO and XSLT can help each other.
Remember this Example
?<fo:block
font-size="14pt" font-family="verdana" color="red"
space-before="5mm" space-after="5mm">
W3Schools
</fo:block>
<fo:block
text-indent="5mm"
font-family="verdana" font-size="12pt"
space-before="5mm" space-after="5mm">
At W3Schools you will find all the Web-building tutorials you
need, from basic HTML and XHTML to advanced XML, XSL, Multimedia
and WAP.
</fo:block>

Result:

W3Schools

At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to
advanced XML, XSL, Multimedia and WAP.

The example above is from the chapter about XSL-FO Blocks.

9
With a Little Help from XSLT
Remove the XSL-FO information from the document:
<header>
W3Schools
</header>
<paragraph>
At W3Schools you will find all the Web-building tutorials you
need, from basic HTML and XHTML to advanced XML, XSL, Multimedia
and WAP.
</paragraph>

Add an XSLT transformation:


<xsl:template match="header">
<fo:block
font-size="14pt" font-family="verdana" color="red"
space-before="5mm" space-after="5mm">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="paragraph">
<fo:block
text-indent="5mm"
font-family="verdana" font-size="12pt"
space-before="5mm" space-after="5mm">
<xsl:apply-templates/>
</fo:block>
</xsl:template>

And the result will be the same:

W3Schools

At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to
advanced XML, XSL, Multimedia and WAP.

XSL-FO Software
XSL-FO needs formatting software to produce output.

XSL-FO Processors
An XSL-FO processor is a software program for formatting XSL documents for output.
Most XSL-FO processors can output PDF documents and quality print, as well as HTML and other formats.
Some well-known XSL-FO processors are described below.

XSL Formatter
XSL Formatter is a software to format XML documents for production-quality printing and output to PDF.
Antenna House has been providing version V2 of the same product since January, 2002 in the global
market, and XSL Formatter was rated as one of the best quality product at the XML 2002, XML 2003
conferences held in Europe.
Building on over 4 years of experience developing XSL-FO software, Antenna House has completely written
from scratch an entirely new Formatter that offers significant enhancements and provides a solid foundation
on which to continue to move forward.

Xinc Beta Release


Xinc is an XSL-FO processor by Lunasil LTD.
Xinc is designed to be fast, multithreaded and memory efficient. A Swing based XSL-FO viewer allows you
to view and print XSL-FO files as well as generate PDF files with the click of a button. Xinc can be used as a
server component via its Java API. Xinc can also be used in a Microsoft server environment by using its
COM interface. New features include hyphenation, basic-link, PDF output, memory/speed optimizations and
a simple COM interface.

10
Scriptura
Inventive Designers Scriptura is a cross-platform document design and generation solution based on XSLT
and XSL-FO.
Scriptura has a WYSIWYG design tool and engine. The XSL-FO formatter used in the engine is no longer
based on Apache FOP, but is written from scratch by Inventive Designers. The new features in this release
are: support for bulleted and numbered lists, 'break-before' and 'break-after' properties, extended bar code
options and improved number and currency formatting. A free trial version is available for download.

11
By: DataIntegratedEntity22592
Source: http://w3schools.com/xslfo/default.asp

12

Você também pode gostar