by Niklas Waller on June 30, 2011
in XML
In a former post I shortly showed you how to dedicate height easily in XSL when using the document for printing for example a PDF document. I will use the same code here to show you how to accomplish vertical align. Also an easy thing when you know about it, but it required quite some Googling and trying before getting it right.
<fo:table table-layout="fixed">
<fo:table-column column-width="5cm"/>
<fo:table-body>
<fo:table-row height="0.5cm">
<fo:table-cell display-align="center">
<fo:block font-size="8pt"><xsl:value-of select="$somevalue"/></fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row height="0.6cm">
<fo:table-cell display-align="center">
<fo:block font-size="8pt">Some hardcoded value</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row height="0.5cm">
<fo:table-cell display-align="before">
<fo:block font-size="8pt">Third textstring</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table
As you can see I have added an attribute to the fo:table-cell tag called display-align. In this example it can have three values which corresponds to the following in HTML.
display-align="before" - align="top"
display-align="before" - align="middle"
display-align="before" - align="bottom"
Tagged as:
vertical-align,
xsl:fo,
xslt
by Niklas Waller on June 23, 2011
in XML
If you have the need to dedicate height in a printout regardless of if there is a value to print or not, you can proceed this way. There are probably many other ways as well.
I have an XSL:FO document here meant to be outputted as PDF. In one place there should be the same height of a block regardless of if there is a value or not to display. If you use a simple block, it will shrink if there is no value in it. You can create a small table and set the height on the row though to get the desired behavior. Simple solution but not that obvious in the first place perhaps.
<fo:table table-layout="fixed">
<fo:table-column column-width="5cm"/>
<fo:table-body>
<fo:table-row height="0.5cm">
<fo:table-cell>
<fo:block font-size="8pt"><xsl:value-of select="$somevalue"/></fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row height="0.6cm">
<fo:table-cell>
<fo:block font-size="8pt">Some hardcoded value</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table
Tagged as:
pdf,
xsl-fo,
xslt
by Niklas Waller on May 26, 2011
in XML
In a recent post I explained how a table of contents can be accomplished using XSL. In my case I am using it in the BigMachines proposal editor.
One thing that were not that good with this solution was that I had to specify the length of the leader. This lead to undesired line breaks or too much space. Here is an alternative to the table of contents creation, which is the ‘tocThirdTemplate’ template if you’re looking at the other post for comparison.
Notice that the blocks are slightly different and I am also using ‘inline’ to get the text and number on the same row as the dots.
<xsl:template name="tocThirdTemplate">
<!-- Define parameters to decide whether to display certain lines or not -->
<xsl:param name="firstParam" />
<xsl:param name="secondParam" />
<!--
<fo:block><xsl:value-of select="$firstParam"/></fo:block>
<fo:block><xsl:value-of select="$secondParam"/></fo:block>
-->
<fo:block xsl:use-attribute-sets="toc_block" text-align="left">First row
<fo:inline space-after=".1in">
<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="anchorName1"/>
</fo:inline>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent" text-align="left">Second row
<fo:inline space-after=".1in">
<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="anchorName2"/>
</fo:inline>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent" text-align="left">Third row
<fo:inline space-after=".1in">
<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="anchorName3"/>
</fo:inline>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent" text-align="left">Fourth row
<fo:inline space-after=".1in">
<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="anchorName4"/>
</fo:inline>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent" text-align="left">Fifth row
<fo:inline space-after=".1in">
<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="anchorName5"/>
</fo:inline>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent" text-align="left">Sixth row (<xsl:value-of select="$firstParam" />)
<fo:inline space-after=".1in">
<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="anchorName6"/>
</fo:inline>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent" text-align="left">Seventh row
<fo:inline space-after=".1in">
<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="anchorName7"/>
</fo:inline>
</fo:block>
<xsl:if test="$secondParam = 'true'">
<fo:block xsl:use-attribute-sets="toc_block_indent" text-align="left">Eight row
<fo:inline space-after=".1in">
<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="anchorName8"/>
</fo:inline>
</fo:block>
</xsl:if>
</xsl:template>
Furthermore, the attribute sets ‘toc_block’ and ‘toc_block_indent’ contained the attribute <xsl:attribute name=’text-align-last’>justify</xsl:attribute> but this should be removed in addition for this to work.
The result is a table of contents with actually always looks like the example in the former post, regardless of how many letters.
Tagged as:
bigmachines,
table of contents,
xml,
xsl
by Niklas Waller on May 12, 2011
in XML
Table of contents can be done more or less complex. This solution is a rather simple one and can successfully be used with BigMachines for example. Especially if you are using the the old XSL printout functionality.

This is the code that prints the actual table of contents. It uses templates to print the rows. The last called template below takes parameters to decide what content to print. The page-sequence tag that surronds the table of contents assures that it will be printed on its own page.
<!-- ################## Table of Contents ############################ -->
<fo:page-sequence master-reference="aPageSequence" force-page-count="no-force">
<fo:flow flow-name="xsl-region-body" font-family="sans-serif" font-size="10pt">
<fo:block line-height="40pt"></fo:block>
<xsl:if test="$var1='value1'">
<xsl:call-template name="tocFirstTemplate" />
</xsl:if>
<xsl:if test="$var2='value2'">
<xsl:call-template name="tocSecondTemplate" />
</xsl:if>
<xsl:if test="$var3='value3'">
<xsl:variable name="firstParam" select="$paramVar1" />
<xsl:variable name="secondParam" select="$paramVar2" />
<xsl:call-template name="tocThirdTemplate">
<xsl:with-param name="firstParam" select="$firstParam" />
<xsl:with-param name="secondParam" select="$firstParam" />
</xsl:call-template>
</xsl:if>
</fo:flow>
</fo:page-sequence>
Below is the third template that prints the last part of the table of contents. The first two templates work the same way. If they don’t have any arguments (parameters) you just skip the first rows. This is just a way of passing information to the template to use as conditions to decide what to display.
In this template, all rows except the first one are indented.
Styling are used for the fo:blocks and the fo:leader (which prints the dots) using attribute sets. Read this post for more information of styling with attribute sets. At the bottom of this article you will also find the attribute set for the leader used in this example.
There is a tag in the code called ‘<fo:page-number-citation ref-id=”anchorName”/>’. Placing tags like this in the table of contents (toc) and the actual anchors at the place in the document where the toc row refers to makes the page number appear here (magic).
The anchor looks like this (<fo:block id=”anchorName” />). There can only be one in the document for each corresponding referral in the toc.
<xsl:template name="tocThirdTemplate">
<!-- Define parameters to decide whether to display certain lines or not -->
<xsl:param name="firstParam" />
<xsl:param name="secondParam" />
<!--
<fo:block><xsl:value-of select="$firstParam"/></fo:block>
<fo:block><xsl:value-of select="$secondParam"/></fo:block>
-->
<fo:block xsl:use-attribute-sets="toc_block">First row
<fo:leader xsl:use-attribute-sets="toc_leader" leader-length='86%' />
<fo:page-number-citation ref-id="anchorName1/>
<fo:block xsl:use-attribute-sets="toc_block_indent">Second row
<fo:leader xsl:use-attribute-sets="toc_leader" leader-length='84%' />
<fo:page-number-citation ref-id="anchorName2"/>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent">Third row
<fo:leader xsl:use-attribute-sets="toc_leader" leader-length='53%' />
<fo:page-number-citation ref-id="anchorName3"/>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent">Fourth row
<fo:leader xsl:use-attribute-sets="toc_leader" leader-length='77%' />
<fo:page-number-citation ref-id="anchorName4"/>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent">Fifth row
<fo:leader xsl:use-attribute-sets="toc_leader" leader-length='78%' />
<fo:page-number-citation ref-id="anchorName5"/>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent">Sixth row (<xsl:value-of select="$firstParam" />)
<fo:leader xsl:use-attribute-sets="toc_leader" leader-length='60%' />
<fo:page-number-citation ref-id="anchorName"/>
</fo:block>
<fo:block xsl:use-attribute-sets="toc_block_indent">Seventh row
<fo:leader xsl:use-attribute-sets="toc_leader" leader-length='64%' />
<fo:page-number-citation ref-id="anchorName7"/>
</fo:block>
<xsl:if test="$secondParam = 'true'">
<fo:block xsl:use-attribute-sets="toc_block_indent">Eight row
<fo:leader xsl:use-attribute-sets="toc_leader" leader-length='58%' />
<fo:page-number-citation ref-id="anchorName8"/>
</fo:block>
</xsl:if>
</fo:block>
</xsl:template>
The attribute set styling for the leader looks like this:
<xsl:attribute-set name='toc_leader'>
<xsl:attribute name='rule-thickness'>.1pt</xsl:attribute>
<xsl:attribute name='leader-pattern'>dots</xsl:attribute>
<xsl:attribute name='leader-alignment'>reference-area</xsl:attribute>
<xsl:attribute name='font-size'>10pt</xsl:attribute>
</xsl:attribute-set>
And one final note. The example at the top of this article starts with page count 6 because there are other things in the document that is not included in this table of contents.
Tagged as:
bigmachines,
table of contents,
xml,
xsl
by Niklas Waller on May 5, 2011
in XML
I am currently working on a table of contents for an agreement template using XSL:FO. Some data are hard-coded in the template and some are fetched from the underlying XML generated by the framework being used.
HTML tags can be styled with CSS by specifying a class. In a similar way XSL tags can be styled with attribute sets. For example if I want to style an fo:block tag I can define an attribute set defining how a specific fo:block should be rendered.
Here’s the code for the fo:block having a specific style. The attribute ‘xsl:use-attribute-sets’ specifies an attribute set to be used.
<fo:block xsl:use-attribute-sets="toc_block">Some text</fo:block>
And the code for the attribute set defining the style being used by the fo:block tag.
<xsl:attribute-set name='toc_block'>
<xsl:attribute name='line-height'>1.8</xsl:attribute>
<xsl:attribute name='text-align-last'>justify</xsl:attribute>
<xsl:attribute name='font-size'>10pt</xsl:attribute>
<xsl:attribute name='text-indent'>1em</xsl:attribute>
</xsl:attribute-set>
Obviously, this save space and makes it easier to make changes in the code.
Tagged as:
xsl
by Niklas Waller on November 12, 2009
in XML
Todays blog entry will be an open question to any XSL-developer.
Is there a way to use the HTML equivalent ‘rowspan’ in XSL-FO? I want to display a text with a bigger font in the leftmost column. The colums to the right however should consist of a couple of rows with smaller text.
In HTML this would be written something like this:
<table border=”1″ cellpadding=”4″ cellspacing=”0″> <tr> <td rowspan=”3″ style=”font-weight:bold;font-size:14pt”>New Text</td> <td>Value 1</td> <td>Value 2</td> <td></td> </tr> <tr> <td>{someAttribute}</td> <td>{someOtherAttribute}</td> <td></td> </tr> <tr> <td>Value 3</td> <td>Value 4</td> <td>Value 5</td> </tr> </table> |
and look like this:

Here is the code for XSL-FO without the new leftmost column.
<xsl:template name=”printAddress”> <xsl:param name=”subDocument”/> <fo:table table-layout=”fixed” background-color=”#e0e0e0″ keep-with-next.within-page=”always”>
<fo:table-column column-width=”7.0cm”/> <fo:table-column column-width=”7.0cm”/> <fo:table-column column-width=”2.0cm”/>
<fo:table-body>
<!– Begin Row 1 –> <fo:table-row keep-with-previous=”always”> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block>Value 1</fo:block> </fo:table-cell> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block>Value 2</fo:block> </fo:table-cell> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block/> </fo:table-cell> </fo:table-row> <!– Begin Row 2 –> <fo:table-row keep-with-previous=”always”> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block> <xsl:value-of select=”$subDocument/someAttribute”/> </fo:block> </fo:table-cell> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block> <xsl:value-of select=”$subDocument/someOtherAttribute”/> </fo:block> </fo:table-cell> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block/> </fo:table-cell> </fo:table-row> <!– Begin Row 3 –> <fo:table-row keep-with-previous=”always”> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block>value 3</fo:block> </fo:table-cell> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block>Value 4</fo:block> </fo:table-cell> <fo:table-cell margin-left=”0.2cm” padding-before=”0.1cm” padding-after=”0.1cm”> <fo:block>Value 5</fo:block> </fo:table-cell> </fo:table-row> </fo:table-body> </fo:table> </xsl:template> |
The HTML-equivalent for the above code would be to only remove this row from the provided HTML code at the top:
| <td rowspan=”3″ style=”font-weight:bold;font-size:14pt”>New Text</td> |
How would the XSL-FO code look like to provide the same look as the figure at the top?
Tagged as:
rowspan,
xml,
xsl,
xsl-fo