hysoftwareeng
hysoftwareeng

Reputation: 497

Counter Increment Conditional Statement

I need a loop to go through every single PV_RiskAssessment and test a variable called @RPN, so whenever an RPN falls within the range of 0-36, I want to increase the variable by 1. SO basically, if there are 3 that falls in this range, I want to print 3. But what I am getting is a printed loop of all the numbers from 1-36... (I have removed the template since it is not working)

        <xsl:for-each select="Root/individualCQA">
          <tr>            
              <td valign="top">
                <xsl:for-each select="PV_RiskAssessment">

                <p align="center">
                  <xsl:if test="@RPN &gt;'0' and @RPN &lt;'36'">
                     <!-- I want to print the number of CPPs here that fall under this range-->
                  </xsl:if>
                </p>
                </xsl:for-each>
              </td>
         </tr>
        </xsl:for-each>

Upvotes: 0

Views: 319

Answers (2)

Michael Kay
Michael Kay

Reputation: 163595

I need a loop to go through every single PV_RiskAssessment and test a variable called @RPN, so whenever an RPN falls within the range of 0-36, I want to increase the variable by 1.

That's a very procedural description of your proposed solution to the problem, not a good description of the problem. Generally for XSLT it's much better to start with a description of the problem.

A less procedural desription would be: I want to know how many PV_RiskAssessment elements have a value of @RPN in the range 0-36.

That kind of description translates much more naturally into an XSLT solution:

count(PV_RiskAssessment[@RPN >= 0 and 36 >= @RPN])

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 117140

In XSLT, you don't need to loop with increasing counter in order to get the total count of something. You can simply use (from the appropriate context):

count(some-node[some-predicate])

For example, the following 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:for-each select="Root/individualCQA">
            <tr> 
                <td><xsl:value-of select="@CQA"/></td>
                <td><xsl:value-of select="count(PV_RiskAssessment[@RPN > 0 and @RPN &lt; 36])"/></td>
            </tr>   
        </xsl:for-each>
    </table>
</xsl:template> 

</xsl:stylesheet>

when applied to a simplified version of your input:

<Root>
    <individualCQA CQA="Dissolusion">
        <PV_RiskAssessment RPN="500" />
        <PV_RiskAssessment RPN="216" />
        <PV_RiskAssessment RPN="1" />
    </individualCQA>
    <individualCQA CQA="Hardness (Compression)">
        <PV_RiskAssessment RPN="6" />
    </individualCQA>
</Root>

will return:

<?xml version="1.0" encoding="utf-8"?>
<table>
   <tr>
      <td>Dissolusion</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Hardness (Compression)</td>
      <td>1</td>
   </tr>
</table>

Upvotes: 2

Related Questions