XSLT:计算循环内案例的迭代次数

XSLT: Count the iteration of a cases inside a loop

我有两个循环,想计算案例 OKNotOK 的迭代次数以及 xslt 文件中的所有案例。 我怎么这么冷呢?如果我想知道两次迭代的总和怎么写?

我的 For 循环如下:

   <xsl:for-each select="test">
       <xsl:variable name="LinkName" select="attribute::name"/>
           <tr>
              <th style="text-align:left;vertical-align:top;position:"><a name="{$LinkName}"><xsl:value-of select="$LinkName"/></a></th>
                <xsl:for-each select="descendant::node()">
                  <xsl:choose>                                  
                    <xsl:when test="attribute::state='NotOK'">
                        <tr>
                            <td bgcolor="red"><xsl:value-of select="description"/></td>
                        </tr>
                    </xsl:when>
                    <xsl:when test="attribute::state='OK'">
                        <tr>
                            <td bgcolor="lime"><xsl:value-of select="description"/></td>
                        </tr>
                    </xsl:when>
                  </xsl:choose>
                </xsl:for-each>
             </tr>
    </xsl:for-each>

更新:

               <table>
                     <tr bgcolor="coral">
                        <th>Test cases</th>
                        <th>Info</th>
                    </tr>
                    <xsl:for-each select="test">
                        <xsl:variable name="Summation"/>
                        <xsl:variable name="LinkIt" select="@name"/>
                        <xsl:choose>
                            <xsl:when test="descendant::node()/@state='NotOK'">
                                <tr>
                                    <td bgcolor="red"><a href="#{$LinkIt}" title="click for Information"><xsl:value-of select="$LinkIt"/></a></td>
                                    <td>
                                        <xsl:value-of select="count(descendant::node()[@state='NotOK'])"/> of <xsl:value-of select="count(descendant::node()[@state='OK']) + count(descendant::node()[@state='NotOK'])"/>
                                    </td>                                       
                                </tr>
                            </xsl:when>
                            <xsl:when test="descendant::node()/attribute::state='OK'">
                                <tr>
                                    <td bgcolor="lime"><a href="#{$LinkIt}" title="click for Information"><xsl:value-of select="$LinkIt"/></a></td>
                                    <td>
                                        ---
                                    </td>

                                </tr>
                            </xsl:when>
                        </xsl:choose>                           
                    </xsl:for-each>
                </table>

for-each 不是循环。如果你想计算 test 元素的所有后代节点,那么只需使用 <xsl:value-of select="count(test/descendant::node())"/>。您还可以向 XPath 表达式添加谓词,例如 count(test/descendant::node()[attribute::state='NotOK']).

<xsl:for-each select="test"> 内部,上下文节点是一个 test 元素,因此任何计数表达式都应该与该元素相关,例如count(descendant::node()[attribute::state='NotOK']).