我有两个for循环,一个是打印标题,另一个循环是以表格格式打印相应的值。请参阅以下代码:
<fo:table>
<fo:table-body>
<xsl:for-each select="./list">
<xsl:if test="position()<= 1">
<fo:table-row>
<xsl:for-each select="./item">
<xsl:if test="position() = 1 " >
<fo:table-cell text-align="center" width="auto">
<fo:block color="#000000" font-family="Helvetica" font-size="11pt" font-weight="700" padding="5px" space-before="5px" space-after="5px"> <xsl:value-of select="val" /> </fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = 2 or position() = 7 or position() = 12" >
<fo:table-cell text-align="center" width="auto">
<fo:block color="#541f84" font-family="Helvetica" font-size="11pt" font-weight="700" padding="5px" space-before="5px" space-after="5px"><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = 3 or position() = 8 or position() = 13 or position() = 17" >
<fo:table-cell text-align="center" width="auto">
<fo:block color="#bd5b10" font-family="5px" font-size="11pt" font-weight="700" padding="5px" space-before="5px" space-after="5px"><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = 4 or position() = 9 or position() = 14 or position() = 18" >
<fo:table-cell text-align="center" width="auto">
<fo:block color="#147993" font-family="5px" font-size="11pt" font-weight="900" padding="5px" space-before="5px" space-after="5px"><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = 5 or position() = 10 or position() = 15 or position() = 19">
<fo:table-cell text-align="center" width="auto">
<fo:block color="#38441a" font-family="5px" font-size="11pt" font-weight="900" padding="5px" space-before="5px" space-after="5px"><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test=" position() = 6 or position() = 11 or position() = 16" >
<fo:table-cell text-align="center" >
<fo:block color="#656565" font-family="5px" font-size="11pt" font-weight="900" padding="5px" space-before="5px" space-after="5px"><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = last()" >
<xsl:variable name="count" select="position()" />
<fo:table-cell text-align="center" width="auto">
<fo:block color="#38441a" font-family="5px" font-size="11pt" font-weight="900" padding="5px" space-before="5px" space-after="5px"><xsl:value-of select="position()" /></fo:block>
</fo:table-cell>
</xsl:if>
</xsl:for-each>
</fo:table-row>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="./list">
<xsl:if test="position()!=1">
<fo:table-row>
<xsl:for-each select="./item">
<xsl:if test="position() = 1 or position() = 6 or position() = 11 or position() = 16">
<fo:table-cell border="1px" text-align="center" width="auto" background-color="#c0c0c0" border-color="#1d467b" border-width="1px" border-style="solid" >
<fo:block white-space-treatment="preserve" font-family="5px" font-size="9pt" > <xsl:value-of select="val" /> </fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = 2 or position() = 7 or position() = 12 or position() = 17">
<fo:table-cell border="1px" text-align="center" width="auto" background-color="#b3a1c7" border-color="#1d467b" border-width="1px" border-style="solid" >
<fo:block font-family="5px" font-size="9pt" ><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = 3 or position() = 8 or position() = 13">
<fo:table-cell border="1px" text-align="center" width="auto" background-color="#fabf8f" border-color="#1d467b" border-width="1px" border-style="solid" >
<fo:block font-family="5px" font-size="9pt" ><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = 4 or position() = 9 or position() = 14">
<fo:table-cell border="1px" text-align="center" width="auto" background-color="#91cddb" border-color="#1d467b" border-width="1px" border-style="solid" >
<fo:block font-family="5px" font-size="9pt" ><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
<xsl:if test="position() = 5 or position() = 10 or position() = 15">
<fo:table-cell border="1px" text-align="center" width="auto" background-color="#c3d69b" border-color="#1d467b" border-width="1px" border-style="solid" >
<fo:block font-family="5px" font-size="9pt" ><xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:if>
</xsl:for-each>
</fo:table-row>
</xsl:if>
</xsl:for-each>
</fo:table-body>
</fo:table>
但是问题在第一个节点中有些时候来自xsl的项目数与其余节点中的项目数不同。
现在您可以观察到在v10和v11下没有表格单元格出现,我知道这种情况发生了,因为在第一个节点中,所有项目的数量都是13,而对于其余节点,每个节点中没有项目是11。但是,即使项目数量从一个节点到另一个节点不同,我仍然完全想要表格。比如列数应该等于标题数。
我想计算第一个循环中的迭代次数,然后计算第二个循环中的迭代次数,然后将它们一起减去然后再考虑执行相同的表格单元,但我不能这样做。
,xml输入如下:
<records>
<end-date>28-Jan-2014</end-date>
<month>January</month>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>Subject</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V2</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V2</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V3</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V3</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V4</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V5</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V6</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V7</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V8</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V9</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V10</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>V11</val>
</item>
</list>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>999870017</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item"/>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>01-Jan-2014 (6 Months after Day 1 +/- 4 Weeks)</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item"/>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item"/>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item"/>
</list>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>999870018</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item"/>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item"/>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item"/>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>01-Jan-2014 (6 Months after Day 1 +/- 4 Weeks)</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
</list>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>999870020</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>01-Jan-2014 (6 Months after Day 1 +/- 4 Weeks)</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item"/>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val> </val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="item">
<val>01-Jan-2014 (36 Months after Day 1 +/- 4 Weeks)</val>
</item>
</list>
<start-date>01-Jan-2014</start-date>
任何人都可以提供帮助。 谢谢
答案 0 :(得分:2)
列数应该等于标题数。
一个简单的解决方案是迭代标题列表中的项目并为每个项目创建一个单元格 - 无论您正在处理哪个列表。然后从当前列表中的相应项获取值 - 如果存在。这是一个简化的示例,创建一个HTML表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<table>
<thead>
<tr>
<xsl:for-each select="records/list[1]/item">
<th><xsl:value-of select="val" /></th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="records/list[position()>1]">
<xsl:variable name="row" select="position()+1" />
<tr>
<xsl:for-each select="../list[1]/item">
<xsl:variable name="col" select="position()" />
<td><xsl:value-of select="../../list[$row]/item[$col]/val" /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
这是一种不同的方法,使用迭代器计算第一个列表中的项目数:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<table>
<xsl:variable name="columns" select="count(records/list[1]/item)" />
<xsl:for-each select="records/list">
<tr>
<xsl:call-template name="create-cells">
<xsl:with-param name="columns" select="$columns"/>
</xsl:call-template>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template name="create-cells">
<xsl:param name="i" select="1"/>
<xsl:param name="columns"/>
<xsl:if test="$i <= $columns">
<td><xsl:value-of select="item[$i]/val" /></td>
<xsl:call-template name="create-cells">
<xsl:with-param name="i" select="$i + 1"/>
<xsl:with-param name="columns" select="$columns"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>