将项目列表打印为多列 xsl-fo 表



美好的一天,请指教。我有一个项目列表:

<List>
  <Item>
    <Description>Item 1</Description>
    <Code>0001</Code>
  </Item>
  <Item>
    <Description>Item 2</Description>
    <Code>0002</Code>
  </Item>
  <Item>
    <Description>Item 3</Description>
    <Code>0003</Code>
  </Item>
  <Item>
    <Description>Item 4</Description>
    <Code>0004</Code>
  </Item>
</List>

我想打印一个包含这些项目的 3 列表,如下所示:

| Column 1 | Column 2 | Column 3 |
==================================
| Item 1   | Item 2   | Item 3   |
----------------------------------
| Item 4   |          |          |
----------------------------------

下面的代码为每个项目呈现表单元格。它不会呈现第二行中第 2 列和第 3 列的单元格:

<fo:table>
  <fo:table-column width="82mm"/>
  <fo:table-column width="82mm"/>
  <fo:table-column width="82mm"/>
  <fo:table-body>
    <xsl:for-each select="/List/Item[position() mod 3 = 1]">
      <fo:table-row><xsl:apply-templates select=". | following-sibling::*[3 > position()]"/></fo:table-row>
    </xsl:for-each>
  </fo:table-body>
</fo:table>
<xsl:template match="Item">
  <fo:table-cell><fo:block><xsl:value-of select="Description"/></fo:block></fo:table-cell>
</xsl:template>

然后按如下方式打印该表:

| Column 1 | Column 2 | Column 3 |
==================================
| Item 1   | Item 2   | Item 3   |
----------------------------------
| Item 4   |
------------

有没有一种优雅的方式来呈现甚至空的表格单元格,以使表格看起来已满?我必须使用 FOP 0.95,xsl 版本 1.0。

提前谢谢你

沃伊泰克

希望这能给你一个可能的解决方案:

让我们有这个简单的XML文档:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

那么这个转换:

<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 omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/*">
        <fo:table>
            <fo:table-column width="82mm"/>
            <fo:table-column width="82mm"/>
            <fo:table-column width="82mm"/>
            <fo:table-body>
               <xsl:for-each select="*[position() mod 3 = 1]">
                 <fo:table-row>
                   <xsl:apply-templates select=
                                  ". | following-sibling::*[3 > position()]"/>
                        <xsl:variable name="vPos" select="position()"/>
                        <xsl:variable name="vUnfilled"
                             select=" 2 - count(following-sibling::*)"/>
                        <xsl:if test="position() = last()">
                         <xsl:for-each select=
                                   "../*[not(position() > $vUnfilled)]">
                            <fo:table-cell>
                                <fo:block>
                                    <xsl:value-of select="' '"/>
                                </fo:block>
                            </fo:table-cell>
                         </xsl:for-each>
                        </xsl:if>
                    </fo:table-row>
                </xsl:for-each>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <xsl:template match="num">
        <fo:table-cell>
            <fo:block>
                <xsl:value-of select="."/>
            </fo:block>
        </fo:table-cell>
    </xsl:template>
</xsl:stylesheet>

当应用于上述 XML 文档时,会生成一个表,其中包含每行所需的完整单元格数

<fo:table xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:table-column width="82mm"/>
   <fo:table-column width="82mm"/>
   <fo:table-column width="82mm"/>
   <fo:table-body>
      <fo:table-row>
         <fo:table-cell>
            <fo:block>01</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>02</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>03</fo:block>
         </fo:table-cell>
      </fo:table-row>
      <fo:table-row>
         <fo:table-cell>
            <fo:block>04</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>05</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>06</fo:block>
         </fo:table-cell>
      </fo:table-row>
      <fo:table-row>
         <fo:table-cell>
            <fo:block>07</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>08</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>09</fo:block>
         </fo:table-cell>
      </fo:table-row>
      <fo:table-row>
         <fo:table-cell>
            <fo:block>10</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block> </fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block> </fo:block>
         </fo:table-cell>
      </fo:table-row>
   </fo:table-body>
</fo:table>

相关内容

  • 没有找到相关文章