且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XSLT新行未保留

更新时间:2023-12-03 21:34:40

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

I think there are a few issues with your XSLT:

  1. 无需将文本包含在xsl:text元素中,因为这些文本节点不仅由空格字符组成,因此也不会被剥离(请参见 linefeed-treatment 属性:linefeed-treatment="preserve"
  2. 文字换行等效于
实体,因此在您的输入中,静态"和文本"之间有 3个换行,当保留时将产生两个空行.如果这不是您想要的,则必须删除其中一些
  3. 单词"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 
 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文件时,保留的换行符和被忽略的换行符之间的区别不是立即显而易见的,因为它归结为祖先元素中存在linefeed-treatment属性(可能离文本节点本身很远.


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应该在其中换行的地方
    • using an empty fo:block where a line break should be
    <fo:block>
        Static
        <fo:block/>
        text <fo:inline font-weight="bold">bold.</fo:inline>
    </fo:block>