在XSL的文本内容中保留换行等,但不在标签本身中

Seb*_*oek 5 xsl-fo apache-fop

在构建XSL文档时,我遇到了以下问题.我想在原始文本中保留换行符,所以我设置了linefeed-treatment="preserve".但是,这显然也意味着它保留了文本内容之外和实际xml元素中的换行符.一个例子:

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>";
Run Code Online (Sandbox Code Playgroud)

这是我用作输入的文本.它被转换为Java中的xml文档.请注意在指示新行的内容之前的\n.这将导致FO文档中的以下输出:

<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>
Run Code Online (Sandbox Code Playgroud)

所以它确实在文本内容之前显示了换行符,这很好.

我使用Apache FOP将其转换为PDF文件以及另一个第三方库,将其转换为DocX文件.在这两种情况下,内容都将显示如下:

这是一些
习惯

内容

当我手动更改我的XSL并使其像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

然后我的输出很好,就像我期望的那样:

这是一些自定义
内容

显然,我不希望这些额外的换行符来自元素本身,但我确实希望保留文本内容中的换行符.有没有办法做到这一点?或者是否有其他解决方案可以对我的换行符进行排序?

Tom*_*lak 5

一个相当粗糙的解决方案,直到你找到更好的东西.

<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>
Run Code Online (Sandbox Code Playgroud)

这样您至少可以保留一些源代码格式.