Inserting a line break in a PDF generated from XSL FO using <xsl:value-of>

Zkoh picture Zkoh · Sep 7, 2010 · Viewed 55k times · Source

I am using XSL FO to generate a PDF file containing a table with information. One of these columns is a "Description" column. An example of a string that I am populating one of these Description fields with is as follows:

This is an example Description.<br/>List item 1<br/>List item 2<br/>List item 3<br/>List item 4

Inside the table cell that corresponds to this Description, I would like the output to display as such:

This is an example Description.
List item 1
List item 2
List item 3
List item 4

I've learned from searching elsewhere that you can make line breaks in XSL FO using an <fo:block></fo:block> within another <fo:block> element. Therefore, even before I parse the XML with my XSL stylesheet, I replace all occurrences of <br/> with <fo:block/>, so that the literal value of the string now looks like:

This is an example Description.<fo:block/>List item 1<fo:block/>List item 2<fo:block/>List item 3<fo:block/>List item 4

The problem arises when the Description string I am using is obtained using <xsl:value-of>, example as follows:

<fo:block>
    <xsl:value-of select="descriptionStr"/>
</fo:block>

In which case, the value that gets output to my PDF document is the literal value, so it looks exactly like the previous example with all the <fo:block/> literals. I've tried manually hard-coding the <fo:block/> in the middle of another string, and it displays correctly. E.g. if I write inside my stylesheet:

<fo:block>Te<fo:block/>st</fo:block>

It will display correctly as:

Te
st

But this does not seem to happen when the <fo:block/> is inside the value of an <xsl:value-of select=""/> statement. I've tried searching for this on SO as well as Google, etc. to no avail. Any advice or help will be greatly appreciated. Thank you!

Answer

Daniel Haley picture Daniel Haley · Sep 8, 2010

You could also replace <br/> with &#xA; and add a linefeed-treatment="preserve" attribute to your <fo:block>.

Something like:

<fo:block linefeed-treatment="preserve">This is an example Description.&#xA;List item 1&#xA;List item 2&#xA;List item 3&#xA;List item 4</fo:block>

Edit

Some users may need to use \n instead of &#xA; depending on how they are creating the XML. See Retain the &#xA; during xml marshalling for more details.