如何比较XSL中两个单个节点的长度



我有两个用于循环,一个是打印标题,另一个循环是以表格格式完全打印相应的值。请参阅以下代码:

<fo:table>
            <fo:table-body>
            <xsl:for-each select="./list">
                     <xsl:if test="position()&lt;= 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">&#160;<xsl:value-of select="val" />&#160;</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"   >&#160;<xsl:value-of select="val" />&#160;</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下没有出现的表单元,我知道这是发生这种情况的,因为在第一个节点中,no of of项目即将到达13,而在其余节点中,每个节点中的项目no no no no no no no node no no no no no no no nos no no no no。但是,即使项目数量从一个节点到另一个节点的数量与另一个节点有所不同,我仍然希望完全表。就像列的数量应等于标题数。

我想在第一个循环中计算迭代次数,然后在第二个循环中计算迭代次数,然后通过将它们减去它们,然后考虑在其余时间内执行相同的表单元格,但我做不到。

,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>

任何人都可以帮助您。谢谢

列的数量应等于标题数。

一个简单的解决方案是在标题列表中的项目上迭代并为每个项目创建一个单元格 - 无论您正在处理如何处理。然后从当前列表中的相应项目中获取值 - 如果存在。这是一个简化的示例,创建一个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()&gt;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>

这是一种不同的方法,使用迭代器来计算第一个列表中的项目数:

<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 &lt;= $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>

最新更新