Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction to XSL-FO
XSL-FO is about formatting XML data for output.
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
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.
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>
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.
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 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 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 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.
XSL-FO Pages
XSL-FO uses page templates called "Page Masters" to define the layout of pages.
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.
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>
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
Block Background
• background-color
• background-image
• background-repeat
• background-attachment (scroll or fixed)
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.
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)
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.
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>
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.
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>
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.
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