Table of ContentsThis document gives a quick, learn-by-example introduction to XSL Formatting Objects. I don't discuss subtle details of implementation, but rather provide a series of examples of how to perform routine tasks with XEP — an XSL formatter developed by RenderX, Inc. It is not a manual of XSL FO (XSLFO) in general, and some examples given here may not work in other XSL FO (XSLFO) formatters, or give different results. This tutorial was conceived as a means to facilitate reading of XSL 1.0 Recommendation of October 15, 2001. The normative text is available from W3C site: http://www.w3.org/TR/2001/REC-xsl-20011015/. You should obtain a copy of XSL 1.0 Recommendation, and refer to it for a complete description of objects and properties mentioned here. XEP 4.0 also implements several extensions to the XSL 1.0 Recommendation: they add support for useful functionality that cannot be expressed by standard XSL Formatting Objects. The last chapters of this document discuss available extensions in more detail. Needless to say, the extensions are proprietary and incompatible with other XSL formatters; please avoid their use whenever possible. Readers who aren't XEP users can download an evaluation copy of XEP from http://www.renderx.com/download/index.html, and run the proposed examples to see how the formatter works. This manual is also available in XML format with an associated XSL FO (XSLFO) stylesheet; browsing its source code may also be interesting (and instructive). XEP covers more of the spec than was needed for this manual. Please refer to XEP product documentation for a full list of supported elements and properties. Let us create the shortest XSL FO (XSLFO) document. <?xml version="1.0" encoding="iso-8859-1"?> Now we can save this document into a file and compile it using XEP 4.0. to produce a PDF file. Open it with Acrobat Reader, and enjoy :-). Let us now enrich the text with character-level formatting. Several properties control font styles — family, size, color, weight, etc. Let's look at some examples: <fo:block font-family="Times" font-size="14pt">
Hello, world!
</fo:block>
Font family is Times, and font size is 14 points. <fo:block font-family="Times" font-size="14pt" font-style="italic">
<fo:inline color="red">H</fo:inline>ello,
<fo:inline font-weight="bold">world!</fo:inline>
</fo:block>
Same as above, plus:
Note a new formatting object —
Font properties are inheritable. It means that, once defined for
a formatting object, they apply to all formatting objects inside it.
That's why the first inline sequence affects only the color of the
font, leaving its family, size, and slant unmodified. Inheritable
properties can be put almost everywhere on the formatting objects
tree; as a rule, you specify default font for a document by applying these
properties to To reduce typing, you can use a shorthand notation for setting font attributes as a group. For example, the above example can be rewritten as follows: <fo:block font="italic 14pt Times">
<fo:inline color="red">H</fo:inline>ello,
<fo:inline font-weight="bold">world!</fo:inline>
</fo:block>
The [<style, weight, and/or variant>] <size>[/<line height>] <family>
It sets all mentioned attributes to specified values, and resets
all other font-related attributes to their default values, overriding
inherited values. Be careful when using this feature: Let's now build a full XSL FO (XSLFO) example with font attributes introduced above, and some new ones: <?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="my-page">
<fo:region-body margin="1in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body" font="12pt Times"Let's consider this piece: <fo:block line-height="1.5" text-align="justify">
This is an example of double-justified text.
The space between lines is 1.5 of the nominal font height.
</fo:block>
The
Let's look into a more complicated example: <fo:block text-align="justify" text-indent="1in"
text-align-last="end" last-line-end-indent="1in">
This is an example of double-justified text with an indented first line.
The last line of the text is aligned to the right, and indented
by 1 inch from the right.
</fo:block>
This fragment should be formatted as follows:
By specifying a negative value for
<fo:block text-align="start" text-indent="-1in"
text-align-last="end" last-line-end-indent="-1in"
start-indent="1in" end-indent="1in">
This is an example of left-aligned text with an outdented first line.
The last line of the text is aligned to the right, and outdented
by 1 inch from the right.
</fo:block>
The names of properties
To complete this chapter, let's introduce attributes to position
blocks vertically with respect to each other: Spaces are more complicated than indents: they aren't specified as a single value, but rather as a vector of several components — minimum, optimum, and maximum value for the space. Components can be assigned separately: an attribute name will consist of a property name followed by a component name, separated by a dot. You can also assign all numeric components the same value by using the attribute name without a component qualifier; this is what you normally do in most cases.
An important property of spaces is that they aren't additive:
if there are several space specifiers between two blocks (e.g.
The fragment below illustrates the use of spaces: <fo:flow flow-name="xsl-region-body" font="14pt Times">
<fo:block font-size="24pt"
text-align="center"
space-before="30pt"
space-before.conditionality="retain"Blocks may have borders from either side. Sides can be addressed either in an absolute orientation scheme (left, right, top, and bottom), or in a writing-mode relative scheme (resp. start, end, before, and after). Every border has the following properties, that may get the following values in XSL FO (XSLFO):
You can specify each property for each border separately by writing several attributes
of the form <fo:block border-top-color="black"
border-top-style="solid"
border-top-width="thick"
text-align="center">
Thick black border at the top
</fo:block>
You can also specify properties for all the four sides as a whole,
using a shorthand notation <fo:block border-color="gray"
border-style="groove"
border-width="medium"
text-align="center">
Medium gray groove around the whole block
</fo:block>
You can also group properties that refer to one side into a single
shorthand attribute <fo:block text-align="center"
border-top="dashed 1pt #C00000"
Finally, a single <fo:block border="thin silver ridge"
text-align="center">
Thin silver ridge around the whole block
</fo:block>
When printed, a block may be split by a page break or
a column break. What happens to the borders adjacent to
the breakline? For some block types, you may want to box
every part of the original block separately, i.e. draw
a border line where the break occurs; this is the default
behaviour in XSL FO (XSLFO). For some other blocks, you may prefer to
“keep the box open” suppressing borders at
column/page breaks. This behavior is controlled by a special
component of the border's width —
<fo:block border="thin blue groove"
border-before-width.conditionality="discard"
border-after-width.conditionality="discard">
If this block happens to be split by a page break,
no line will be drawn on either side of the break.
</fo:block>
Only writing-mode oriented sides (before, after, start, and end) are permitted in the conditional border expressions.
Once you have set a border around an object, you normally
want to specify a padding between the text and
the border. This is done by <fo:block border="thin solid navy"
text-align="center"
padding-before="18pt"
padding-bottom="18pt">
<fo:block border="thin solid maroon">
The outer block has a 18 pt padding from top and bottom
</fo:block>
</fo:block>
There also exists a shorthand <fo:block border="thin solid navy"
text-align="center"
padding="2cm">
<fo:block border="thin solid maroon">
The outer block has a 2 cm padding from all sides
</fo:block>
</fo:block>
You can also specify several numbers as
a value for
Example: <fo:block border="thin solid navy"
text-align="center"
padding="1cm 3cm">
<fo:block border="thin solid maroon">
The outer block has 1 cm padding from top and bottom,
and 3 cm padding from right and left.
</fo:block>
</fo:block>
Like borders, padding may be conditional at page breaks:
specifying To complete the picture, let's learn how to set backgrounds for blocks.
Blocks may have different backgrounds — colored or even
decorated with a graphic. To specify a color for the
background, use <fo:block color="yellow"
background-color="red"
padding="12pt"
text-align="center">
Yellow on red
</fo:block>
To add a background image to the block, you should
The NoteUnder Win32, absolute pathnames may contain a colon after the disk letter. These colons confuse Java URI resolver: the disk letter is treated as protocol name. Use an unabridged file URI: file:/C:/XEP/myimage.jpg Example: <fo:block border="0.5pt solid silver"
background-image="url('spots.jpg')"
padding="18pt"
text-align="center">
The whole background tiled with colored spots
</fo:block>
By default, an image specified in <fo:block border="0.5pt solid silver"
background-image="url('spots.jpg')"
background-repeat="no-repeat"
background-position-horizontal="center"
background-position-vertical="center"
padding="18pt"
text-align="center">
A single image placed 18pt to the right
and 6pt below the upper left corner.
</fo:block>
The position of the image within the block is controlled by two properties,
<fo:block border="0.5pt solid silver"
background-image="url('spots.jpg')"
background-repeat="repeat-y"
background-position="left center"
background-color="silver"
padding="18pt"
text-align="center">
A stripe made of coloured spots along the left edge of the block;
the rest of the block has a silver background.
</fo:block>
XSL Recommendation defines no method to scale the background image. To do it,
you have to recur to RenderX add-ons; see description of
So far, I have used only single page masters in examples. In this section, more complex cases will be analyzed. To start, let's design a page sequence with two page masters: one for the first page, the other one for the rest of the document. <?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="first-page">
<fo:region-body margin="1in" border="thin silver ridge"
You can also specify different page masters for odd and even pages,
blank pages, first/last pages, etc. This is achieved by using a more
complex sequence specifier —
In the example below, the first page will have a thin silver border around it, and all other pages will have a border along the inside edge of the body area (left for even pages, right for odd ones). <fo:layout-master-set>
<fo:simple-page-master master-name="first-page">
<fo:region-body margin="1in" border="thin solid silver"
padding="6pt"/>
</fo:simple-page-master>
<fo:simple-page-master master-name="odd-page">
<fo:region-body margin="1in" border-right="medium gray ridge"
padding-right="6pt"/>
</fo:simple-page-master>
<fo:simple-page-master master-name="even-page">
<fo:region-body margin="1in" border-left="medium gray ridge"
padding-left="6pt"/>
</fo:simple-page-master>
<fo:page-sequence-master master-name="my-sequence">
<fo:repeatable-page-master-alternatives>
<fo:conditional-page-master-reference page-position="first"
master-reference="first-page"/>
<fo:conditional-page-master-reference odd-or-even="odd"
master-reference="odd-page"/>
<fo:conditional-page-master-reference odd-or-even="even"
master-reference="even-page"/>
</fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-sequence">
…
…
…
So far, I only placed the contents into the body region of the page master. There are other regions on the page, used to display static elements of page layout — headers, footers, and sidebars. Inside page master, there may be up to five region specifications:
The dimensions of the regions are calculated by the following algorithm:
All regions may have borders, padding and background. However, the
XSL 1.0 Recommendation is contradictory with respect to borders and padding
on all region areas but
Regions are named using a special
To put contents into side regions, a special formatting object —
Let us now look at the examples. The simplest example just adds a header to “Hello, world!”: <fo:layout-master-set>
<fo:simple-page-master master-name="my-page">
<fo:region-body margin="1.5in 1in"Another example uses sidebars. It draws a right sidebar on odd pages, and a left sidebar on even pages: <fo:layout-master-set>
<fo:simple-page-master master-name="even-page">
<fo:region-body margin="1in 1.5in"
column-count="2"Lists in XSL FO (XSLFO) are much more than just a bulleted sequence of paragraphs: it is a general-purpose mechanism to align two blocks adjacent to each other. It may be used to format ordinary lists, footnotes, image lists, and even to produce some table-like layout patterns.
A list is created by a Let's start with an ordinary bulleted list: <fo:list-block provisional-distance-between-starts="18pt"
Another example — using <fo:static-content flow-name="xsl-region-before"> <fo:list-block border-bottom="1pt gray ridge" Tables in XSL FO (XSLFO) resemble HTML ones: they are made of cells grouped into rows; rows are further grouped into row groups — table header, table footer, and table bodies (one or more). There are also column descriptors. Table model is rather evolved: please refer to the spec for further details. Here, I limit myself to a couple of core examples. A basic 2x2 table: <fo:table border="0.5pt solid black" text-align="center">
<fo:table-body>
<fo:table-row>
<fo:table-cell padding="6pt" border="0.5pt solid black">
A more complicated example includes explicit column width assignment and cells spanning multiple grid units: <fo:table border="0.5pt solid black"
text-align="center"
border-spacing="3pt"
There is a special inline element for including graphics
into XSL FO (XSLFO) — Here's an example: <fo:block>
This text includes a picture:
<fo:external-graphic src="url('smile.gif')"
If you need a block-level graphic, you should wrap the element in
a | |||||