5

在构建 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>

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

这是一些自定义
内容

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

4

2 回答 2

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>

这样你至少可以保留一些源代码格式。

于 2011-02-07T13:06:47.793 回答
1

<xsl:output method="xml" indent="no"/>在您的 XSLT 中应该从您的输出中删除所有标记缩进。

于 2011-02-07T11:43:57.410 回答