簡體   English   中英

在帶有 XSL-FO 的 PDF 中添加換行符?

[英]Adding line break in a PDF with XSL-FO?

嘗試基於 XML 和使用 XMLSpy 的文件創建 PDF 文件。

我正在嘗試根據字段內容將字段拆分為兩行。

例如,如果我的 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”字符串時,我想將該字段分成兩行。 所以我的代碼找到了字符串,創建了新變量,但仍然顯示在一行中。 我嘗試使用各種技術創建一個帶有空行的變量,但仍顯示在一行中。

有什么想法嗎?

請參閱我關於使用十六進制實體引用和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

當來源是:@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 -字符串。)

下面的樣式表拆分任何包含“ AKA ”的文本節點。 由於封閉的fo:block來自MyFieldxsl:template ,因此此版本會生成一個空的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM