在 XSL 的文本内容中保留换行符等,但不在标签本身中保留
在构建 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>
然后我的输出很好,就像我所期望的那样:
这是一些自定义
内容
显然,我不希望这些额外的换行符来自元素本身,但我确实想保留文本内容中的换行符。有办法做到这一点吗?或者是否有其他解决方案来对我的换行符进行排序?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个相当粗糙的解决方法,直到你找到更好的东西。
这样你至少可以保留一些源代码格式。
A rather crude work-around, until you find something better.
This way you can at least keep some of your source code format.
XSLT 中的
应从输出中删除所有标记缩进。<xsl:output method="xml" indent="no"/>
in your XSLT should remove all the tag indentation fromyour output.