美好的一天,请指教。我有一个项目清单:
<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。
提前谢谢
Vojtech
答案 0 :(得分:1)
希望这会让您了解可能的解决方案:
让我们拥有这个简单的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>