0

I have two loops and want to count the iteration of cases OK and NotOK and overall cases inside xslt file. How I cold do so? If I want to know the sum of both iteration how could I write it?

my For loops are as:

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

Update:

               <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>
Royeh
  • 433
  • 5
  • 21
  • Please show us the input and the expected output. – michael.hor257k Oct 02 '15 at 11:10
  • You could simplify your XSLT by changing `attribute::name` to `@name`, and so on. If you changed the `node()` in `descendant::node()` to the name of the element that has the `@state`, then you could move the conditional logic to just setting the value of the `@bgcolor`, but how to best write the conditional logic depends on which XSLT version you are using. – Tony Graham Oct 02 '15 at 11:22
  • @TonyGraham Thanks for your comment. I am using version `1.0` ! – Royeh Oct 02 '15 at 11:24
  • Instead of the literal `@bgcolor`, add an `xsl:attribute` (http://www.w3.org/TR/xslt#creating-attributes) and put the logic in there. Best to start a new question if you need to know more. – Tony Graham Oct 02 '15 at 11:29
  • @martin-honnen did that. – Tony Graham Oct 02 '15 at 11:42

1 Answers1

0

for-each is not a loop. If you want to count all descendant nodes of the test elements then simply use <xsl:value-of select="count(test/descendant::node())"/>. You can also add predicates to XPath expressions, like count(test/descendant::node()[attribute::state='NotOK']).

Inside of a <xsl:for-each select="test"> the context node is a test element so any count expression should be relative to that e.g. count(descendant::node()[attribute::state='NotOK']).

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110