FOP中长单词的表格单元格包裹问题

7
我有一个在FOP中的表格,在遇到非常长的单词时,表格就会出现单元格溢出的问题。我尝试在表格单元格或单元格块中使用wrap-option="wrap",但它并没有起作用。 **完整编辑** 因为我认为这里只展示一点是比较复杂的,所以这是完整的xsl文件:
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes" />
<xsl:param name="tableCell" select="'1.0'" />

<!-- Globale Variablen START -->

<xsl:param name="tabbgcolor" select="'#EEEEEE'" />



<!-- Globale Variablen ENDE -->

<xsl:template match="/datasheet">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fn="http://www.w3.org/2005/xpath-functions">
        <fo:layout-master-set>
            <fo:simple-page-master page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="1cm" margin-left="1.5cm" margin-right="1.5cm" master-name="first">
                <fo:region-body margin-top="20mm" margin-bottom="20mm" />
                <fo:region-before extent="15mm" />
                <fo:region-after extent="15mcm" />
            </fo:simple-page-master>
            <fo:simple-page-master master-name="new" margin-right="1.0cm" margin-left="1.0cm" margin-bottom="1cm" margin-top="1cm" page-height="21cm" page-width="29.7cm">
                <fo:region-body margin-top="30mm" margin-bottom="20mm" />
                <fo:region-before extent="30mm" />
                <fo:region-after extent="15mm" />
            </fo:simple-page-master>
        </fo:layout-master-set>



        <fo:page-sequence language="de" country="DE" master-reference="new" initial-page-number="1">

            <fo:static-content flow-name="xsl-region-before">
                <fo:block text-align="center" font-size="12pt" padding="5pt" font-weight="bold">
                    <xsl:value-of select="title" />
                </fo:block>
                <fo:block text-align="right" font-size="12pt" padding="5pt" font-weight="bold">
                    <xsl:value-of select="date" />
                </fo:block>
                <fo:block text-align="right" font-size="12pt" padding="0pt" font-weight="bold">
                    <xsl:value-of select="time" />
                </fo:block>
                <fo:block>
                    <fo:leader leader-length="100%" leader-pattern="rule" rule-thickness="2pt" color="black" />
                </fo:block>
            </fo:static-content>

            <fo:static-content flow-name="xsl-region-after">
                <fo:block text-align="center">
                    Seite
                    <fo:page-number />
                    von
                    <fo:page-number-citation ref-id="TheVeryLastPage" />
                </fo:block>
            </fo:static-content>





            <fo:flow flow-name="xsl-region-body">



                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>


                <fo:block font-weight="bold" padding="5pt" padding-top="25pt">
                    Jahre <xsl:value-of select="fromYear" /> bis <xsl:value-of select="toYear" />
                </fo:block>

                <fo:block text-align="center">
                    <xsl:choose>
                        <xsl:when test="dataList != ''">
                            <fo:table table-layout="fixed" width="100%" border-style="solide" border-width="1pt">
                                <fo:table-column column-width="25%" border-style="solid" border-width="1pt" />
                                <fo:table-column column-width="25%" border-style="solid" border-width="1pt" />
                                <fo:table-column column-width="25%" border-style="solid" border-width="1pt" />
                                <fo:table-column column-width="25%" border-style="solid" border-width="1pt" />



                                <fo:table-header>
                                    <fo:table-row>
                                        <fo:table-cell border-style="solid" font-weight="bold" border-width="0pt">
                                            <fo:block>
                                                Cell1
                                            </fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border-style="solid" font-weight="bold" border-width="0pt">
                                            <fo:block>
                                                Cell2
                                            </fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border-style="solid" font-weight="bold" border-width="0pt">
                                            <fo:block>
                                                Cell3
                                            </fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border-style="solid" font-weight="bold" border-width="0pt">
                                            <fo:block>
                                                Cell4
                                            </fo:block>
                                        </fo:table-cell>

                                    </fo:table-row>
                                </fo:table-header>

                                <fo:table-body>
                                    <xsl:for-each select="dataList"> 
                                        <fo:table-row>
                                            <fo:table-cell padding="5pt" border-style="solid" border-width="1pt">
                                                <fo:block text-align="left">
                                                    <xsl:value-of select="data1" />
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell padding="5pt" border-style="solid" border-width="1pt">
                                                <fo:block text-align="left">
                                                    <xsl:value-of select="data2" />
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell padding="5pt" border-style="solid" border-width="1pt">
                                                <fo:block text-align="left">
                                                    <xsl:value-of select="data3" />
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell padding="5pt" border-style="solid" border-width="1pt">
                                                <fo:block text-align="left">
                                                    <xsl:call-template name="intersperse-with-zero-spaces">
<xsl:with-param name="str" select="data4"/>
</xsl:call-template>
                                                </fo:block>
                                            </fo:table-cell>

                                        </fo:table-row>
                                    </xsl:for-each>  
                                </fo:table-body>

                            </fo:table>
                        </xsl:when>
                        <xsl:otherwise>
                            <fo:block padding="6cm" text-align="center" font-weight="bold" font-size="16pt">No data.
                            </fo:block>
                        </xsl:otherwise>
                    </xsl:choose>
                </fo:block>


                <fo:block id="TheVeryLastPage">
                </fo:block>
            </fo:flow>
        </fo:page-sequence>

        <!-- ___________________________________________________________________________________________________________ -->





    </fo:root>

</xsl:template>

<xsl:template name="intersperse-with-zero-spaces">
<xsl:param name="str"/>
<xsl:variable name="spacechars">
    &#x9;&#xA;
    &#x2000;&#x2001;&#x2002;&#x2003;&#x2004;&#x2005;
    &#x2006;&#x2007;&#x2008;&#x2009;&#x200A;&#x200B;
</xsl:variable>

<xsl:if test="string-length($str) &gt; 0">
    <xsl:variable name="c1" select="substring($str, 1, 1)"/>
    <xsl:variable name="c2" select="substring($str, 2, 1)"/>

    <xsl:value-of select="$c1"/>
    <xsl:if test="$c2 != '' and
        not(contains($spacechars, $c1) or
        contains($spacechars, $c2))">
        <xsl:text>&#x200B;</xsl:text>
    </xsl:if>

    <xsl:call-template name="intersperse-with-zero-spaces">
        <xsl:with-param name="str" select="substring($str, 2)"/>
    </xsl:call-template>
</xsl:if>
</xsl:template> 




</xsl:stylesheet>

数据单元格data4存在问题,例如一次输入255个数字或字符,没有连字符或空格。

输入来自数据库,但可能如下:

 <datasheet>
   <dataList>
     <data1>intro</data1>
     <data2>section</data2>
     <data3>cutters</data3>
     <data4>743576746876357467569384657654687465874638563465873487435767468763574675693846576546874658746385634658734874357674687635746756938465765468746587463856346587348743576746876357467569384657654687465874638563465873487435767468763574675693846576546874658746385634658734874357674687635746756938465765468746587463856346587348743576746876357467569384657654687465874638563465873487435767468763574675693846576546874658746385634658734874357674687635746756938465765468746587463856346587348743576746876357467569384657654687465874638563465873487435767468763574675693846576546874658746385634658734874357674687635746756938465765468746587463856346587348</data4>

   </dataList>
 </datasheet>

结果应该是一个像表格一样的东西:
|cell1  |cell2  |cell3  |cell4  |
_________________________________
|intro  |section|cutters|7435767|
                        |4687635|
                        |7467569|
                        |3846576|
_________________________________

在第四个单元格中等等

现在以上内容可以正常工作


为了帮助您的模板尝试插入零间隔, 请展示已经放置的xsl:template开始标签(开头)以及名为“intersperse-with-zero-spaces”的模板。然后最好展示样本输入(至少一个长字符串块),期望输出(FO)和实际输出。 - LarsH
1
感谢您发布样本数据和更多的样式表。 样式表中必须存在复制/粘贴错误...有一个xsl:param,它只应该出现在模板开头,在模板中间。 因此,样式表无效。 是否应该有两个模板? 此外,我还没有看到名为intersperse-with-zero-spaces的模板;但也许您仍在添加它。 最后,您使用的是哪个XSLT处理器 - Xalan? XSLT 1.1是一个被放弃的版本。 - LarsH
xsl参数是复制粘贴错误,已经被删除了。我使用最新的FOP二进制文件。 - Thevagabond
就是这样,你找到了解决方案...需要在<xsl:param name="str"/>行上方添加<xsl:template name="intersperse-with-zero-spaces">,删除上面的参数并关闭第一个模板...现在它可以工作了(已修复,请参见上文)。 - Thevagabond
很好。我很惊讶你的XSLT处理器没有给你报错。我会把我的评论中的“解决方案”部分放到我的答案中,这样你就可以接受它(点击复选标记)。 :-) - LarsH
3个回答

3
<xsl:template name="intersperse-with-zero-spaces">
<xsl:param name="str"/>
<xsl:variable name="spacechars">
    &#x9;&#xA;
    &#x2000;&#x2001;&#x2002;&#x2003;&#x2004;&#x2005;
    &#x2006;&#x2007;&#x2008;&#x2009;&#x200A;&#x200B;
</xsl:variable>

<xsl:if test="string-length($str) &gt; 0">
    <xsl:variable name="c1" select="substring($str, 1, 1)"/>
    <xsl:variable name="c2" select="substring($str, 2, 1)"/>

    <xsl:value-of select="$c1"/>
    <xsl:if test="$c2 != '' and
        not(contains($spacechars, $c1) or
        contains($spacechars, $c2))">
        <xsl:text>&#x200B;</xsl:text>
    </xsl:if>

    <xsl:call-template name="intersperse-with-zero-spaces">
        <xsl:with-param name="str" select="substring($str, 2)"/>
    </xsl:call-template>
</xsl:if>
</xsl:template> 

调用位置通常像下面这样进行调用:

<fo:block text-align="left">
   <xsl:call-template name="intersperse-with-zero-spaces">
<xsl:with-param name="str" select="data4"/>
</xsl:call-template>
  </fo:block>

我进行了测试,一切运作正常。


2
一种选择是在你转换为FO的时候,在XSLT中将长数字/单词拆分。你需要决定在哪里进行拆分。
另请参阅:XSL-FO fop. Long text flows into adjacent cells/block, obscuring stuff there 更新(从评论中复制):样式表中必须存在复制/粘贴错误……有一个,它只应出现在模板的开头,在模板的中间。因此,样式表无效。是否应该有两个模板?另外,我还没有看到名为intersperse-with-zero-spaces的模板;但是也许您还在添加它。
更新2:上述问题已经得到解决。
我原以为第二个模板不能是intersperse-with-zero-spaces,因为它正在调用该模板。我没有想到它应该是一个递归模板!
顺便说一下,如果有使用XSLT 2.0的选项,则可以通过更有效的正则表达式替换来替换intersperse-with-zero-spaces模板。

我尝试过这个方法,但对我没有用。 我现在正在尝试零宽度空格的方法,但运气不太好...请看我上面的编辑。 - Thevagabond
前面的评论是指第一个更新之前答案的一部分。 - LarsH

-1
添加 hyphenate="true",它将自动调整表格单元格中的空格。
<fo:table-cell border-bottom-width="0px" border-left-width="0px" 
    border-right-width="0px" border-top-width="0px" font-size="9px" 
    padding-left="0px" border-style="solid" border-width="1pt" 
    border-color="white" padding-start="0px" padding-end="2px" 
    padding-before="0px" padding-after="0px" width="16%" display-align="center"
    text-align="start" hyphenate="true">

连字号与“调整空格”无关,通常不适用于引起问题的长数字字符串。 - lfurini

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