While building an XSL document I ran into the following problem. I want to preserve linefeeds in the original text, so I set linefeed-treatment="preserve"
. However, this apparantly also means it preserves linefeeds outside of the text content and in the actual xml elements. An example:
String content = "<fo:block white-space-collapse=\"true\" ><fo:inline>this is some </fo:inline><fo:inline font-weight=\"bold\">custom</fo:inline><fo:inline> \ncontent</fo:inline></fo:block>";
This is the text I use as input. It's transformed to an xml document in Java. Note the \n right before content indicating a new line. This will result in the following output in the FO document:
<fo:block white-space-collapse="true" linefeed-treatment="preserve">
<fo:inline>this is some</fo:inline>
<fo:inline font-weight="bold">custom</fo:inline>
<fo:inline>
content</fo:inline>
</fo:block>
So it does show the linebreak right before the text content which is fine.
I use Apache FOP to transform this to a PDF file as well as another third party library to convert this into a DocX file. In both cases the content will then show up like this:
this is some
customcontent
When I change my XSL manually and make it like this:
<fo:block white-space-collapse="true" linefeed-treatment="preserve"><fo:inline>this is some </fo:inline><fo:inline font-weight="bold">custom</fo:inline><fo:inline>
content</fo:inline></fo:block>
Then my output is fine and like I would expect:
this is some custom
content
Obviously I don't want these additional linebreaks coming from the elements itself, but I really do want to preserve linefeeds from the text content. Is there a way to do this? Or is there an alternative solution to get my linebreaks sorted?
A rather crude work-around, until you find something better.
<fo:block white-space-collapse="true" linefeed-treatment="preserve"
><fo:inline>this is some</fo:inline
><fo:inline font-weight="bold">custom</fo:inline
><fo:inline>
content</fo:inline
></fo:block>
This way you can at least keep some of your source code format.
<xsl:output method="xml" indent="no"/>
in your XSLT should remove all the tag indentation fromyour output.
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