Using XSL Formatting Objects, Part 2
by J. David Eisenberg
January 24, 2001
Part one of this series
Introduction
Lists
Definition Lists
Tables
Summary
This article is the second part of our series on using XSL
Formatting Objects. You should read the first article
before proceeding with this one.
Having tackled the cover and contents page in the previous
article, we're now ready to put the main content into the Spanish
handbook. Let's start off with:
Introduction
This handbook covers the major topics in Spanish but is by no
means complete.
Accents
When we pronounce English words, one syllable is usually emphasized
(stressed, in linguistic terms). The stressed syllable is underlined
in the following words: computer, language,
development, succeeds. Spanish words also have a stressed
syllable, and there are rules for determining which syllable carries the
emphasis.
The headings and paragraph will be <fo:block> elements,
and the bold and underlined words will be <fo:inline>
elements. Let's start with a description of the first heading.
<fo:block
font-size="14pt" font-family="sans-serif"
font-weight="bold" color="green"
space-before="6pt" space-after="6pt">
Introduction
<fo:block>
the space-before and space-after are two of the
many properties that you may set for a block. Many of them are exactly
the same as the properties you can use in Cascading Style Sheets (CSS)
- Font Properties
-
font-family, font-weight,
font-style (italic), font-size,
font-stretch, font-variant (small-caps)
- Background Properties
-
background-color, background-image,
background-repeat, background-attachment (scroll
or fixed)
- Border Properties
-
border-location-info where:
location is one of before, after,
start, end, top, bottom,
left, or right
info is one of style, width, or
color
- Padding Properties
-
padding-location where:
location is one of before, after,
start, end, top, bottom,
left, or right
- Margin Properties
-
margin-location where:
location is one of
top, bottom,
left, or right
- Text Alignment Properties
-
text-align and text-align-last
(for last line of text in block );
values can be start, end,
left, right, or center
- Indentation Properties
-
text-indent (first line),
start-indent, end-indent
- Miscellaneous Properties
-
wrap-option (no-wrap or wrap);
widows and orphans (determining
how many lines must be left at top or
bottom of a page)
break-after and break-before (when
to do page or column breaks);
reference-orientation (rotated text in
90-degree increments)
Given the plethora of options, we can have a fairly complicated
block definition for the paragraph. The definition below uses the
dot-notation “compound datatype” to allow the page layout
mechanism some flexibility in paragraph spacing:
<fo:block
text-indent="1em"
font-family="sans-serif" font-size="12pt"
space-before.minimum="2pt"
space-before.maximum="6pt"
space-before.optimum="4pt"
space-after.minimum="2pt"
space-after.maximum="6pt"
space-after.optimum="4pt">
This handbook covers the major topics in Spanish, but is by
no means complete.
<fo:block>
If your document has twenty or thirty headings and seventy or
eighty paragraphs, you don't want to type (or copy and paste) all of
these Formatting Objects elements. This is where XSLT comes in. We
will write our document in HTML and then use an XSLT to transform it
to the far more verbose XSL:FO version. Here's the HTML so far:
<h3>Introduction</h3>
<p>
This handbook covers the major topics in Spanish, but is by
no means complete.
</p>
<h3>Accents</h3>
<p>
When we pronounce English words, one syllable is usually
emphasized (<b>stressed</b>, in linguistic terms).
The stressed syllable is underlined in the following
words: com<u>pu</u>ter, <u>lan</u>guage, de<u>vel</u>opment,
suc<u>ceeds</u>. Spanish
words also have a stressed syllable, and there are rules for
determining which syllable carries the emphasis.
</p>
And here are the templates you'll need to do the headings and
paragraphs:
<xsl:template match="h3">
<fo:block font-size="14pt" font-family="sans-serif"
font-weight="bold" color="green"
space-before="6pt" space-after="6pt">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="p">
<fo:block
text-indent="1em"
font-family="sans-serif" font-size="12pt"
space-before.minimum="2pt"
space-before.maximum="6pt"
space-before.optimum="4pt"
space-after.minimum="2pt"
space-after.maximum="6pt"
space-after.optimum="4pt">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
What happens to all of the page-initialization code from the past
article? It goes into templates that handle the <html>
and <body> tags. We won't repeat it here, but you can
peruse it in another
browser window.
That leaves the <b> and <u>
tags. Those are inline elements handled via <fo:inline>
(with <i> thrown in as a bonus).
<xsl:template match="b">
<fo:inline font-weight="bold"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="u">
<fo:inline text-decoration="underline"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="i">
<fo:inline font-style="italic"><xsl:apply-templates/></fo:inline>
</xsl:template>
Once we set up the HTML file and run it through XSLT and FOP, we come
up with:
Lists
Next, we will add lists to the document. Here's the content
to be added:
- If a syllable has an accent mark, that syllable
always gets the stress: acción (action),
teléfono.
- If the word ends with a vowel, n, or s,
the next-to-last syllable gets the stress: amigo,
hablan (they talk), animales.
- All other words are accented on the last syllable:
hotel, similar,
español.
Four elements are used to set up a list. An
<fo:list-block> contains individual
<fo:list-items>. Each list item is composed of a
<fo:list-item-label> and a
<fo:list-item-body>. You set the spacing by setting the
attributes shown in the diagram below:
- provisional-distance-between-starts
- provisional-label-separation
- start-indent for list-item-label
- start-indent for list-item-body
- end-indent for list-item-label
- end-indent for list-item-body
Now we can create an XSLT template to handle an ordered list.
We'll set the start indent of the list item label, and leave the
rest up to FOP. By using the relative
em spacing, lists will give reasonable spacing with any size
font.
<xsl:template match="ol">
<fo:list-block
space-before="0.25em" space-after="0.25em">
<xsl:apply-templates/>
</fo:list-block>
</xsl:template>
<xsl:template match="ol/li">
<fo:list-item space-after="0.5ex">
<fo:list-item-label start-indent="1em">
<fo:block>
<xsl:number/>.
</fo:block>
</fo:list-item-label>
<fo:list-item-body>
<fo:block>
<xsl:apply-templates/>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
Unordered lists are set up similarly except that you use a bullet
instead of a number. The relevant part of the definition for list
items in an unordered list is
<xsl:template match="ul/li">
<fo:list-item>
<fo:list-item-label start-indent="1em">
<fo:block>
•
</fo:block>
<!-- etc. -->
[1] [2] Next