在构建 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>";
这是我用作输入的文本。它已转换为 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>
所以它确实在文本内容之前显示了换行符,这很好。
我使用 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>
然后我的输出很好,就像我期望的那样:
这是一些自定义
内容
显然我不希望这些额外的换行符来自元素本身,但我确实希望保留文本内容的换行符。有没有办法做到这一点?或者是否有其他解决方案可以让我的换行符排序?