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!
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 where you want a new line. Show activity on this post. if you write this in file e.g.
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.
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"?>
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.
You could also replace <br/>
with 

and add a linefeed-treatment="preserve"
attribute to your <fo:block>
.
Something like:
<fo:block linefeed-treatment="preserve">This is an example Description.
List item 1
List item 2
List item 3
List item 4</fo:block>
Edit
Some users may need to use \n
instead of 

depending on how they are creating the XML. See Retain the 
 during xml marshalling for more details.
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="'
'"/> </xsl:template>
See https://en.wikipedia.org/wiki/Newline#Unicode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With