tags:

views:

112

answers:

2

Hello,

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!

+2  A: 

You shouldn't use xsl:value-of instruction but xsl:apply-templates instead: for built-in rule for text node will just output their string value, and for empty br element you could declare a rule matching descriptionStr/br or descriptionStr//br (depending your input) in order to transform to empty fo:block.

Alejandro
Hi Alejandro, I think I understand what you mean... but in my case, I apologize for not making this clear in my initial question, the string I am actually processing is not from an XML file per se. It's from a node set that I build using the EXSLT str:split function (http://www.exslt.org/str/functions/split/str.split.html). So, each child in the node set is enclosed in a '<token>' element tag, therefore I am unable to match a template with the name of the element that contains my string. Do you think I need to change my implementation so I can use the apply-templates call? Thank you so much.
@user311811: if you have `<token>This is an example Description.<br/>List item 1</token>` you could match `token/br`. But from your question, this `xsl:value-of select="descriptionStr"` means string value of `descriptionStr` child element.
Alejandro
@user311811: You should be able to use `exslt:node-set()` to treat the result from `str:split()` as a node set. If you're able to use XSLT 2.0 this would be so much easier though.
Per T
+1  A: 

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>
DevNull