不保留 XSLT 新行 [英] XSLT new lines not being preserved

查看:25
本文介绍了不保留 XSLT 新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我的空格在 xslt 之后没有保留在我的最终 PDF 中.我想要的输出是:

For some reason my spaces aren't being preserved in my final PDF after xslt. My desired output is:

静态

文本粗体.

这是我的 xslt 模板:

Here's my xslt template:

<xsl:preserve-space elements="*" />
<xsl:strip-space elements="" />

<xsl:template match="coverPage">
    <fo:block font-size="12pt" color="black" text-align="center">
        <xsl:text>
            Static
            &#xA;
            text
        </xsl:text>
    </fo:block>
    <fo:block font-size="12pt" color="black" text-align="center" font-weight="bold">
        <xsl:text>
            bold.
        </xsl:text>
    </fo:block>
</xsl:template>

推荐答案

我认为您的 XSLT 存在一些问题:

I think there are a few issues with your XSLT:

  1. 无需将文本包含在 xsl:text 元素中,因为这些文本节点不仅仅由空白字符组成,因此永远不会被剥离(请参阅空白剥离了解更多详情)
  2. 出于同样的原因,没有必要使用 xsl:preserve-spacexsl:strip-space,除非当然您出于其他原因需要它们
  3. 在从 XML 到 XSL-FO 的转换中保留换行符只是(必需的)第一步,但随后您必须在处理 XSL-FO 文件期间保留它们;为此,您必须使用linefeed-treatment 属性:linefeed-treatment="preserve"
  4. 文字换行相当于 &#xA; 实体,因此在您的输入中,您在静态"和文本"之间有 3 个换行,这将保存时产生两个空行;如果这不是您想要的,您必须删除其中一些
  5. 单词text"和bold"位于两个不同的fo:block元素中,因此这意味着它们将始终位于不同的行上;如果你想让它们一个挨着一个放置,那些词必须在 fo:inline 元素 内(并且必须有一个外部的 fo:block 包含它们)
  1. there is no need to enclose the text inside xsl:text elements, as those text nodes are not composed only of whitespace characters and therefore will never be stripped (see Whitespace stripping for more details)
  2. for the same reason, there is no need to use xsl:preserve-space and xsl:strip-space, unless of course you need them for other reasons
  3. preserving linefeeds in the transformation from XML to XSL-FO is just the (required) first step, but then you must preserve them during the processing of the XSL-FO file; in order to do this, you must use the linefeed-treatment property: linefeed-treatment="preserve"
  4. a literal linefeed is equivalent to a &#xA; entity, so in your input you have 3 linefeeds between "Static" and "text", which will produce two empty lines when preserved; if that's not what you want, you have to remove some of them
  5. the words "text" and "bold" are inside two different fo:block elements, so this means they will always be on different lines; if you want them to be placed one beside the other, those words must be inside fo:inline elements instead (and there must be an outer fo:block to contain them)

<小时>

最后一句警告

在查看 FO 文件时保留的换行符和被忽略的换行符之间的区别并不是很明显,因为它归结为换行处理的存在祖先元素中的属性(可能离文本节点本身很远).


A final word of warning

While looking at an FO file the difference between a preserved linefeed and an ignored one is not immediately apparent, as it boils down to the presence of the linefeed-treatment attribute in an ancestor element (which could be quite far from the text node itself).

在特定位置强制换行的更清晰方法包括:

Clearer ways to force a line break in a specific position include:

  • 使用不同的 fo:block 元素,每个元素都包含应该创建一行(或几行)的文本
  • using different fo:block elements, each one containing the text that should create a line (or several ones)
    <fo:block>Static</fo:block>
    <fo:block>text <fo:inline font-weight="bold">bold.</fo:inline></fo:block>

  • 使用空的 fo:block 来换行
  •     <fo:block>
            Static
            <fo:block/>
            text <fo:inline font-weight="bold">bold.</fo:inline>
        </fo:block>
    

    这篇关于不保留 XSLT 新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆