这对我不起作用。我试图为我的表获取正确数量的附加列,具体取决于我的XML中的节点数。在这个例子中,我有两个"东西"节点,但我的XML数据可以有一到四个,因此我的表可以有一到四个额外的列。第一列将永远在那里。
这是我的XML:
<stuff>
<thing>house</thing>
<color>red</color>
</stuff>
<stuff>
<thing>hat</thing>
<color>brown</color>
</stuff>
这是我的XSL:
<fo:table width="100%" table-layout="fixed">
<fo:table-column column-width="27%" column-number="1"/>
<xsl:for-each select="stuff">
<xsl:if test="position()=1">
<fo:table-column column-width="73%" column-number="2"/>
</xsl:if>
<xsl:if test="position()=2">
<fo:table-column column-width="36.5%" column-number="2"/>
<fo:table-column column-width="36.5%" column-number="3"/>
</xsl:if>
<xsl:if test="position()=3">
<fo:table-column column-width="24%" column-number="2"/>
<fo:table-column column-width="24%" column-number="3"/>
<fo:table-column column-width="25%" column-number="4"/>
</xsl:if>
<xsl:if test="position()=4">
<fo:table-column column-width="18.25%" column-number="2"/>
<fo:table-column column-width="18.25%" column-number="3"/>
<fo:table-column column-width="18.25%" column-number="4"/>
<fo:table-column column-width="18.25%" column-number="5"/>
</xsl:if>
</xsl:for-each>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block>
<fo:block>First Row</fo:block>
</fo:block>
</fo:table-cell>
<xsl:for-each select="stuff">
<fo:table-cell>
<fo:block>
<xsl:value-of select="thing"/>
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>
<fo:block>Second Row</fo:block>
</fo:block>
</fo:table-cell>
<xsl:for-each select="stuff">
<fo:table-cell>
<fo:block>
<xsl:value-of select="color"/>
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</fo:table-body>
</fo:table>
答案 0 :(得分:0)
我想你想做这样的事情:
<xsl:template match="parent-of-stuff">
<fo:table width="100%" table-layout="fixed">
<fo:table-column column-width="27%" column-number="1"/>
<xsl:variable name="n" select="count(stuff)" />
<xsl:for-each select="stuff">
<fo:table-column column-width="{73 div $n}%" column-number="{position() + 1}"/>
</xsl:for-each>
<!-- the rest of the table -->
</fo:table>
</xsl:template>
如果3列方案的column-width="24.3333333333333%"
结果不可接受,请执行以下操作:
<xsl:template match="parent-of-stuff">
<fo:table width="100%" table-layout="fixed">
<fo:table-column column-width="27%" column-number="1"/>
<xsl:variable name="n" select="count(stuff)" />
<xsl:for-each select="stuff">
<fo:table-column column-number="{position() + 1}">
<xsl:attribute name="column-width">
<xsl:choose>
<xsl:when test="$n=3 and position()=3">25%</xsl:when>
<xsl:when test="$n=3">24%</xsl:when>
<xsl:otherwise>
<xsl:value-of select="73 div $n" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</fo:table-column>
</xsl:for-each>
<!-- the rest -->
</fo:table>
</xsl:template>