Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving linefeeds etc. in text content of XSL but not in tags itself

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
custom

content

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?

like image 914
Sebastiaan van den Broek Avatar asked Feb 07 '11 11:02

Sebastiaan van den Broek


2 Answers

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.

like image 54
Tomalak Avatar answered Sep 18 '22 01:09

Tomalak


<xsl:output method="xml" indent="no"/> in your XSLT should remove all the tag indentation fromyour output.

like image 22
biziclop Avatar answered Sep 18 '22 01:09

biziclop