【问题标题】:Adding line break in a PDF with XSL-FO?使用 XSL-FO 在 PDF 中添加换行符?
【发布时间】:2011-10-09 11:55:43
【问题描述】:

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

我正在尝试根据字段内容将字段分成两行。

例如,如果我的 varialbe = "John Doe AKA Johnny D",我想这样查看:

约翰·多伊

约翰尼·D

我的问题是,即使使用网络上的所有样本,我也无法使其工作。

这是我的代码:

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

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

有什么想法吗?

【问题讨论】:

    标签: xml xslt xsl-fo


    【解决方案1】:

    @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 来自xsl:templateMyField,因此此版本会生成一个空的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>
    

    【讨论】:

      【解决方案2】:

      请参阅 my answer here 关于使用十六进制实体引用和 linefeed-treatment


      编辑

      我从 cmets 中获取了您的代码,并将其放在示例 XSLT 样式表中的模板中。我唯一改变的是:

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

      使用虚拟 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
      

      【讨论】:

      • 这对我不起作用。我使用了许多在这里找到的样本,但似乎没有任何效果。
      • 您的链接未通过。另外,您的处理器是否使用 Apache FOP?
      • 对不起,当我说,在这里找到,我的意思是在这个论坛。 ...是的,我正在使用 Apache FOP。
      • 您能否发布更多代码,以便我/我们可以尝试重现该问题?
      • 我知道这是一篇较旧的帖子,但我的天哪,在搜索了一个小时后它才救了我 :)
      猜你喜欢
      • 2010-09-18
      • 2016-09-06
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 2017-04-15
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多