如何使用XSL-FO在PDF中添加换行?

11

尝试基于XML和文件创建PDF文件,使用XMLSpy。

我想根据字段内容将其拆分为两行。

例如,如果我的变量=“John Doe AKA Johnny D”,我希望将其视为:

John Doe

Johnny D

我的问题是,即使使用网络上的所有示例,我仍然无法使其正常工作。

这是我的代码:

     <xsl:value-of disable-output-escaping="yes" select="concat(substring-before(//MyField,'AKA'),$newline,substring-after(//MyField,'AKA'))" /> 
  </xsl:when>

基本上,每当我找到“AKA”字符串时,我想把字段分成两行。 所以我的代码找到了这个字符串,创建了一个新变量,但仍然在一行中显示。 我尝试过创建一个带有空白行的变量,使用各种技术,但仍然在一行中显示。

有什么想法吗?

2个回答

17

参见我的答案,了解如何使用十六进制实体引用和linefeed-treatment来插入换行符。


编辑

我将您在评论中的代码放入模板中,生成了一个示例XSLT样式表的样本。 我唯一更改的是:

  1. 我将您的newline变量更改为&#xA;
  2. 我在您的fo:block中添加了linefeed-treatment="preserve"

使用虚拟的XML文件和XSLT样式表,我生成了一个XSL-FO文档,当使用FOP呈现时,会将"John Doe"和"Johnny D"分别显示在不同的行上。

以下是XML文件:

<doc>
  <MyField>John Doe AKA Johnny D</MyField>
</doc>

这是XSLT样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
          <fo:region-body margin="1in" margin-top="1.5in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="doc">
    <xsl:variable name="newline" select="'&#xA;'"/>        
    <xsl:variable name="MyVar">
      <xsl:choose>
        <xsl:when test="contains(//MyField,'AKA')">
          <xsl:value-of select="concat(substring-before(//MyField,'AKA'),$newline,substring-after(//MyField,'AKA'))"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="//MyField"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <fo:block linefeed-treatment="preserve">
      <xsl:value-of select="$MyVar"/>
    </fo:block>
  </xsl:template>

</xsl:stylesheet>

以下是生成的XSL-FO代码:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
         <fo:region-body margin="1in" margin-top="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:root>
            <fo:layout-master-set>
               <fo:simple-page-master page-height="11in" page-width="8.5in" master-name="my-page">
                  <fo:region-body margin-top="1.5in" margin="1in"/>
               </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="my-page">
               <fo:flow flow-name="xsl-region-body">
                  <fo:block linefeed-treatment="preserve">John Doe 
 Johnny D</fo:block>
               </fo:flow>
            </fo:page-sequence>
         </fo:root>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

这是一个单页PDF,尺寸为8.5" x 11",页面上只有这个内容:

John Doe
Johnny D

它对我来说就是无法工作。我尝试使用了许多在这里找到的示例,但似乎没有任何一个能够正常运行。 - Elkucho
你的链接没有传送过来。另外,你是否在使用Apache FOP作为你的处理器? - Daniel Haley
抱歉,当我说“found here”时,我是指在这个论坛中找到的。是的,我正在使用Apache FOP。 - Elkucho
你能否发布更多的代码,这样我/我们就可以尝试重现这个问题吗? - Daniel Haley
3
我知道这是一篇较旧的帖子,但天啊,它在我搜索了一个小时后救了我 :) - Jonathan
显示剩余2条评论

0

@daniel-haley的答案仍然在源代码为以下内容时生成单个名称对:

<doc>
    <MyField>John Doe AKA Johnny D</MyField>
    <MyField>Johnny D AKA John Doe</MyField>
    <MyField>John Smith</MyField>
</doc>

(在XPath 1.0中,将节点集转换为字符串仅返回节点集中按文档顺序排列的第一个节点的字符串值。请参见https://www.w3.org/TR/xpath/#function-string。)
下面的样式表将分割包含“AKA”的任何文本节点。由于封闭的fo:block来自MyField的,因此此版本会生成一个空的fo:block以引起换行。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
                <fo:region-body margin="1in" margin-top="1.5in" />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="my-page">
            <fo:flow flow-name="xsl-region-body">
                <xsl:apply-templates />
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>

<!-- Could change to match on 'MyField/text()[contains(., ' AKA ')]'
     if necessary. -->
<xsl:template match="text()[contains(., ' AKA ')]">
    <xsl:value-of select="substring-before(., ' AKA ')" />
    <fo:block />
    <xsl:value-of select="substring-after(., ' AKA ')" />
</xsl:template>

<xsl:template match="MyField">
    <fo:block>
        <xsl:apply-templates />
    </fo:block>
</xsl:template>

</xsl:stylesheet>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接