首页 > 解决方案 > XSL 自动换行问题

问题描述

我有一个用 XSL 编码的表,用于在执行时生成 pdf。用户在 CountryGroup 表格单元格中输入数据时出现问题。如果数据中没有空格,它将破坏单元格的边界,并且数据将溢出到其他单元格中,从而使文档不可读。

我正在尝试找出一种使文本换行的方法。请看下面的代码片段:

 <fo:table-cell border="1pt solid black" display-align="after"><fo:block>Customer Seq Range</fo:block></fo:table-cell>
           <fo:table-cell border="1pt solid black" display-align="after"><fo:block>Label Code</fo:block></fo:table-cell>
           <fo:table-cell border="1pt solid black" display-align="after"><fo:block>Labelled Lot</fo:block></fo:table-cell>
           <fo:table-cell border="1pt solid black" display-align="after"><fo:block>Country Group</fo:block></fo:table-cell> <!--NCCRC200003  Country Group -->
           <fo:table-cell border="1pt solid black" display-align="after"><fo:block>Lot Expiry</fo:block></fo:table-cell>
           <fo:table-cell border="1pt solid black" display-align="after"><fo:block>QTY</fo:block></fo:table-cell>
     </fo:table-row>
    </fo:table-header>
    <fo:table-body>
    <fo:table-row font-size="9pt" font-weight="normal">
        <fo:table-cell border="1pt solid black" padding="3mm 0mm" display-align="center"><fo:block><xsl:value-of select="ItemNumber"/></fo:block></fo:table-cell>
        <fo:table-cell border="1pt solid black" padding="3mm 0mm" display-align="center"><fo:block><xsl:value-of select="RangeStart"/>-<xsl:value-of select="RangeEnd"/></fo:block></fo:table-cell>
        <fo:table-cell border="1pt solid black" padding="3mm 0mm" display-align="center"><fo:block><xsl:value-of select="CustRangeStart"/>-<xsl:value-of select="CustRangeEnd"/></fo:block></fo:table-cell>
        <fo:table-cell border="1pt solid black" padding="3mm 0mm" display-align="center"><fo:block><xsl:value-of select="LabelType"/></fo:block></fo:table-cell>
        <fo:table-cell border="1pt solid black" padding="3mm 0mm" display-align="center"><fo:block><xsl:value-of select="OtherLot"/></fo:block></fo:table-cell>
        <fo:table-cell border="1pt solid black" padding="3mm 0mm" display-align="center"><fo:block><xsl:value-of select="CountryGroup"/></fo:block></fo:table-cell> <!--NCCRC200003  Country Group. -->
        <fo:table-cell border="1pt solid black" padding="3mm 0mm" display-align="center"><fo:block><xsl:value-of select="ExpiryDate"/></fo:block></fo:table-cell>
        <fo:table-cell border="1pt solid black" padding="3mm 0mm" display-align="center"><fo:block><xsl:value-of select="Quantity"/></fo:block></fo:table-cell>
    </fo:table-row> 

标签: xsltxsl-fo

解决方案


如果您希望在文本中没有空格的情况下将一个块换成多行,请尝试将属性定义wrap-option"wrap".

如果我重新缩进你的代码,你有:

<fo:table-cell border="1pt solid black" padding="3mm 0mm" 
   display-align="center">
  <fo:block>
    <xsl:value-of select="CountryGroup"/>
  </fo:block>
</fo:table-cell>

我将通过以下方式更改上面的代码:

<fo:table-cell border="1pt solid black" padding="3mm 0mm" 
   display-align="center">
  <fo:block wrap-option="wrap"><!-- added wrap-option attribute here -->
    <xsl:value-of select="CountryGroup"/>
  </fo:block>
</fo:table-cell>

更多关于这里的信息:XSL-FO fop。长文本流入相邻的单元格/块,掩盖那里的东西

如果这不起作用,您可以在块中注入零宽度空格,希望这有助于将您的文本包装成几行:

<xsl:template name="string-replace-all">
  <xsl:param name="text" />
  <xsl:param name="replace" />
  <xsl:param name="by" />
  <xsl:choose>
    <xsl:when test="contains($text, $replace)">
      <xsl:value-of select="substring-before($text,$replace)" />
      <xsl:value-of select="$by" />
      <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text"
        select="substring-after($text,$replace)" />
        <xsl:with-param name="replace" select="$replace" />
        <xsl:with-param name="by" select="$by" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

这是我在这里找到的一段代码:XSLT 1.0:替换节点集中所有出现的字符串

代替

    <xsl:value-of select="CountryGroup"/>

您可以尝试(根据以下建议进行编辑):

    <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="CountryGroup"/>
      <xsl:with-param name="replace" select="','"/>
      <xsl:with-param name="by" select="',&#8203;'"/>
    </xsl:call-template>

对于零宽度空间,我使用了它的 Unicode &#8203;。如果您使用其他编码,则需要对此进行调整。


推荐阅读