Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xslt

xsl-fo

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!

like image 700
Zkoh Avatar asked Sep 07 '10 18:09

Zkoh


People also ask

How do you break a line in XSLT?

Include the attribute Method="text" on the xsl:output tag and include newlines in your literal content in the XSL at the appropriate points. If you prefer to keep the source code of your XSL tidy use the entity &#10; where you want a new line. Show activity on this post. if you write this in file e.g.

What is XSL FO used for?

XSL-FO (XSL Formatting Objects) is a markup language for XML document formatting that is most often used to generate PDF files. XSL-FO is part of XSL (Extensible Stylesheet Language), a set of W3C technologies designed for the transformation and formatting of XML data.

What does the purpose of the xsl value of block what does xsl refer to?

An XPath expression that specifies which node/attribute to extract the value from. It works like navigating a file system where a forward slash <? xml version="1.0" encoding="UTF-8"?>

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


2 Answers

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.

like image 136
Daniel Haley Avatar answered Sep 19 '22 20:09

Daniel Haley


This helped me and should be simplest solution (working with Apache FOP 1.1):

Why not replace your <br/> with Unicode character called line separator.

   <xsl:template match="br">       <xsl:value-of select="'&#x2028;'"/>    </xsl:template> 

See https://en.wikipedia.org/wiki/Newline#Unicode

like image 25
j.per Avatar answered Sep 20 '22 20:09

j.per