2011-09-23 85 views
0

我在xsl-fo中创建了一个未定义数量的元素(indtr)的表。 ?使用XSLT创建动态XSL-FO表

我不知道是否有这样做一个聪明的办法

这里是我的XML输入文件

<pr-levels> 
    <prs> 
     <pr_nme><![CDATA[Level 1]]></pr_nme> 
     <nt><![CDATA[standards:]]></nt> 
     <b_is> 
      <indtr id="5684"><![CDATA[cell_1]]></indtr> 
      <indtr id="5684"><![CDATA[cell_2]]></indtr> 
      <indtr id="5684"><![CDATA[cell_3]]></indtr> 
      <indtr id="5684"><![CDATA[cell_4]]></indtr> 
     </b_is> 
    </prs> 
    <prs> 
     <pr_nme><![CDATA[Level 2]]></pr_nme> 
     <nt><![CDATA[standards:]]></nt> 
     <b_is> 
      <indtr id="5684"><![CDATA[cell_1]]></indtr> 
     </b_is> 
    </prs> 
    <prs> 
     <pr_nme><![CDATA[Level 3]]></pr_nme> 
     <nt><![CDATA[standards:]]></nt> 
     <b_is> 
      <indtr id="5684"><![CDATA[cell_1]]></indtr> 
      <indtr id="5684"><![CDATA[cell_2]]></indtr> 
     </b_is> 
    </prs> 
    <prs> 
     <pr_nme><![CDATA[Level 4]]></pr_nme> 
     <nt><![CDATA[standards:]]></nt> 
     <b_is> 
      <indtr id="5684"><![CDATA[cell_1]]></indtr> 
      <indtr id="5684"><![CDATA[cell_2]></indtr> 
      <indtr id="5684"><![CDATA[cell_3]]></indtr> 
      <indtr id="5684"><![CDATA[cell_4]></indtr> 
      <indtr id="5684"><![CDATA[cell_5]]></indtr> 
      <indtr id="5684"><![CDATA[cell_6]]></indtr> 
      <indtr id="5684"><![CDATA[cell_7]]></indtr> 
     </b_is> 
    </prs> 
    <prs> 
     <pr_nme><![CDATA[Level 5]]></pr_nme> 
     <nt><![CDATA[standards:]]></nt> 
     <b_is> 
      <indtr id="5684"><![CDATA[cell_1]]></indtr> 
      <indtr id="5684"><![CDATA[cell_2]]></indtr> 
      <indtr id="5684"><![CDATA[cell_3]]></indtr> 
      <indtr id="5684"><![CDATA[cell_4]]></indtr> 
      <indtr id="5684"><![CDATA[cell_5]]></indtr> 
      <indtr id="5684"><![CDATA[cell_6]]></indtr> 
      <indtr id="5684"><![CDATA[cell_7]]></indtr> 
      <indtr id="5684"><![CDATA[cell_8]]></indtr> 
      <indtr id="5684"><![CDATA[cell_9]]></indtr> 
      <indtr id="5684"><![CDATA[cell_10]]></indtr> 
      <indtr id="5684"><![CDATA[cell_11]]></indtr> 
      <indtr id="5684"><![CDATA[cell_12]]></indtr> 
      <indtr id="5684"><![CDATA[cell_13]]></indtr> 
      <indtr id="5684"><![CDATA[cell_14]]></indtr> 
      <indtr id="5684"><![CDATA[cell_15]]></indtr> 
      <indtr id="5684"><![CDATA[cell_16]]></indtr> 
      <indtr id="5684"><![CDATA[cell_17]]></indtr> 
      <indtr id="5684"><![CDATA[cell_18]]></indtr> 
      <indtr id="5684"><![CDATA[cell_19]]></indtr> 
     </b_is> 
    </prs> 
</pr-levels> 

这里是产生XSL-FO表我的XSLT模板:

<xsl:template match="pr-levels"> 
    <fo:table xsl:use-attribute-sets="table_p" break-after="page" force-page-count="no-force"> 
     <fo:table-column column-number="1" xsl:use-attribute-sets="table_col_p"/> 
     <fo:table-column column-number="2" xsl:use-attribute-sets="table_col_p"/> 
     <fo:table-column column-number="3" xsl:use-attribute-sets="table_col_p"/> 
     <fo:table-column column-number="4" xsl:use-attribute-sets="table_col_p"/> 
     <fo:table-column column-number="5" xsl:use-attribute-sets="table_col_p"/> 

     <fo:table-header xsl:use-attribute-sets="table_header"> 
       <xsl:for-each select="prs/pr_nme"> 
        <fo:table-cell> 
         <fo:block> 
          <xsl:apply-templates/> 
         </fo:block> 
        </fo:table-cell> 
       </xsl:for-each> 
     </fo:table-header> 

     <fo:table-body > 
      <fo:table-row> 
       <xsl:for-each select="prs/nt"> 
        <fo:table-cell> 
         <fo:block xsl:use-attribute-sets="nt" > 
          <xsl:apply-templates/> 
         </fo:block> 
        </fo:table-cell> 
       </xsl:for-each> 
      </fo:table-row> 

      <fo:table-row> 
       <xsl:for-each select="prs/b_is"> 
        <fo:table-cell padding="1pt"> 
         <fo:block> 
          <xsl:value-of select="indtr[1]"/> 
         </fo:block> 
        </fo:table-cell> 
       </xsl:for-each> 
      </fo:table-row> 
      <fo:table-row> 
       <xsl:for-each select="prs/b_is"> 
        <fo:table-cell padding="1pt"> 
         <fo:block> 
          <xsl:value-of select="indtr[2]"/> 
         </fo:block> 
        </fo:table-cell> 
       </xsl:for-each> 
      </fo:table-row> 
      <fo:table-row> 
       <xsl:for-each select="prs/b_is"> 
        <fo:table-cell padding="1pt"> 
         <fo:block> 
          <xsl:value-of select="indtr[3]"/> 
         </fo:block> 
        </fo:table-cell> 
       </xsl:for-each> 
      </fo:table-row> 
      <fo:table-row> 
       <xsl:for-each select="prs/b_is"> 
        <fo:table-cell padding="1pt"> 
         <fo:block> 
          <xsl:value-of select="indtr[4]"/> 
         </fo:block> 
        </fo:table-cell> 
       </xsl:for-each> 
      </fo:table-row> 
      <fo:table-row> 
       <xsl:for-each select="prs/b_is"> 
        <fo:table-cell padding="1pt"> 
         <fo:block> 
          <xsl:value-of select="indtr[5]"/> 
         </fo:block> 
        </fo:table-cell> 
       </xsl:for-each> 
      </fo:table-row> 
     </fo:table-body> 
     </fo:table> 
    </xsl:template> 

有可能是NULL,1,69或在<b_is>的任何数字,但现在我只是硬编码的数字。我的问题是我如何动态计数indtrs的数量和在我table.maybe一个XSL中添加行的for循环

________________________________________________________ 
| level 1 | level 2 | level 3 | level 4 | level 5 | 
-------------------------------------------------------- 
|standards:|standards:|standards:|standards:|standards:| 
-------------------------------------------------------- 
| cell 1 | cell 1 | cell 1 | cell 1 | cell 1 | 
-------------------------------------------------------- 
| cell 2 | cell 2 | cell 2 | cell 2 | cell 2 | 
-------------------------------------------------------- 
|   | cell 3 | cell 3 | cell 3 | cell 3 | 
-------------------------------------------------------- 
|   | cell 4 |   |   | cell 4 | 
-------------------------------------------------------- 
|   |   |   |   | cell 5 | 
-------------------------------------------------------- 
|   |   |   |   | cell 6 | 
-------------------------------------------------------- 
+0

表应该是什么样子?我可以看到每个''是一个新表,并且有一个新行,其中一个单元格用于''和''。你想用''和''孩子做什么? ''应该是一个新行,并且每个''是一个单元格?或者每个''应该是一个单元格的新行? –

+0

嘿@DevNull感谢您的回复。同一个b_is中的每一个indtr都会在同一列中,我添加了一个示例来帮助您可视化。 – Jimmy

+0

我会尽量在未来几天内把东西放在一起。 –

回答

0

只是因为它是一个有趣的问题......

替换为多硬编码fo:table-row

<xsl:variable name="pr-levels" select="." /> 
<xsl:for-each 
    select="1 to max(for $prs in prs 
         return count($prs/b_is/indtr))"> 
    <xsl:variable name="row" select="." as="xs:integer" /> 
    <fo:table-row> 
    <xsl:for-each select="$pr-levels/prs"> 
     <fo:table-cell padding="1pt"> 
     <fo:block> 
      <xsl:value-of select="b_is/indtr[$row]"/> 
     </fo:block> 
     </fo:table-cell> 
    </xsl:for-each> 
    </fo:table-row> 
</xsl:for-each>