是否可以将XSL表中的行分成单独的页面序列?

时间:2011-02-11 18:02:14

标签: xml xsl-fo apache-fop

我是xsl的新手,所以这可能是一个明显的答案。

我正在使用FOP在xml和xsl样式表中创建一个表。该表可以有几千行(> 50,000),因此我希望减少FOP的内存占用,以避免耗尽Java堆空间。

现在,我的xsl将整个<fo:table-body&gt;分组。将元素转换为单个页面序列,因此在生成表的行时永远不会回收任何内存。是否有可能以某种方式将单个或一组行分成单独的页面序列?

我知道我可以通过索引遍历元素(请参阅此stackoverflow答案:Xslt - iterate nodes in chunks),但我不认为<fo:page-sequence&gt;元素在<fo:table&gt;内是合法的。元件。

如果无法分解表中的行,是否有办法将行分解为单独的表?

编辑:我从建议中得出了这个

输入xml:

<?xml version="1.0" encoding="UTF-8"?>
<table title="sample">
<headers>
    <column>title1</column>
    <column>title2</column>
    <column>title3</column>
    <column>title4</column>
    <column>title5</column>
    <column>title6</column>
</headers>
<row>
    <column>0</column>
    <column>ABC</column>
    <column>0</column>
    <column>Claim Appeal</column>
    <column>asldkjf98aet24</column>
    <column>897123947623</column>
</row>
<row>
     ...
</row>
    ...
</table>

输入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:template match="table">
<xsl:variable name="startRow" select="0"/>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4" page-height="29.7cm"
page-width="22cm" margin-top="2cm" margin-bottom="2cm"
margin-left="1.5cm" margin-right="1.5cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set> 
<xsl:param name="startRow"/>
<fo:page-sequence>  
<fo:table>
      <xsl:if test="$startRow = 0">
  <fo:table-header>
        <fo:table-row font-weight="bold">
                <xsl:apply-templates select="headers"/>
        </fo:table-row>
  </fo:table-header>
      </xsl:if>
      <xsl:foreach select="row[position() &gt; $startRow &amp;&amp;
position() &lt; 54]">
          <!-- call row rendering template -->
          <fo:table-body>
            <xsl:apply-templates select="row"/>
          </fo:table-body>
      </xsl:foreach>    
   </fo:table>
   </fo:page-sequence>   
   </fo:root>       
    <xsl:if test="$startRow &lt; 100000">
       <xsl:apply-templates select=".">
          <xsl:with-param name="startRow" select="$startRow + 54"/> 
       </xsl:apply-templates>
    </xsl:if>
    <xsl:template match="headers">
        <xsl:for-each select="column">
            <fo:table-cell>
                <fo:block>
                    <xsl:value-of select="."/>
                </fo:block>
            </fo:table-cell>
        </xsl:for-each>
    </xsl:template>     
    <xsl:template match="row">
    <fo:table-row>
        <xsl:for-each select="column">
        <fo:table-cell>
            <fo:block>
                <xsl:value-of select="."/>
            </fo:block>
        </fo:table-cell>
    </xsl:for-each>
    </fo:table-row>
    </xsl:template>
</xsl:template>
</xsl:stylesheet>

有了这个,我在<page-sequence&gt;上收到错误关于master-reference的标签,但是我用master-reference =“simpleA4”尝试了它无济于事。是什么给了什么?

除了语法错误之外,还有什么其他错误?

1 个答案:

答案 0 :(得分:0)

他们绝对不合法。

但您可以关闭表格,关闭页面序列,然后启动新的页面序列和具有相同参数的新表格(减去标题)。

这是一些伪xslt代码:

<xsl:template match="myTableElement">
   <xsl:param name="startRow"/>
   <fo:page-sequence>   <!-- you may only want to put this in if this isn't the first chunk -->
   <fo:table>
      <xsl:if test="$startRow = 0">
          <!-- render header -->
      </xsl:if>
      <xsl:foreach select="elements between between startRow and startRow+50000">
          <!-- call row rendering template -->
      </xsl:foreach>

   </fo:table>
   </fo:page-sequence>       

    <xsl:if test="there are unrendered elements left">
       <xsl:apply-templates select=".">
          <xsl:with-param name="startRow" select="$startRow + 50000"/> 
       </xsl:apply-templates>
    </xsl:if>
</xsl:template>

这是,我担心,尽可能接近理想的解决方案。

相关问题