title: How to count number of nodes in XSLT with various conditions
body: <p>I have XML, based on which I am creating a text file output. In the trailer record I have to count the total number of records in the output file. The logic used to create output file from XML is if the <code>PreTaxAmt</code>,or <code>MatchAmt</code>, or <code>RothAmt</code> for each report entry has a value greater than zero, there is one line created in output. If the <code>Loanamt</code> is greater than zero for each report entry then  the record is outputted in a separate line. Please suggest me how can I use the count function to create the total counts of the line in the XSLT. I am pasting below what I have tried.</p>
  
  <pre><code>&lt; Report_Entry&gt;
      &lt; Participant_ID&gt;033709571&lt;/ Participant_ID&gt;
      &lt; Workers&gt;
          &lt; FULL_PART_Time_Indicator&gt;1&lt;/ FULL_PART_Time_Indicator&gt;
          &lt; Last_Name&gt;Vani&lt;/ Last_Name&gt;
          &lt; First_Name&gt;Mehul&lt;/ First_Name&gt;
          &lt; Middle_Name&gt;N&lt;/ Middle_Name&gt;
          &lt; Rehire_Date_1&gt;2013-04-15-07:00&lt;/ Rehire_Date_1&gt;
      &lt;/ Workers&gt;
      &lt; End_Date_from_Pay_Period&gt;2016-01-03-08:00&lt;/ End_Date_from_Pay_Period&gt;
      &lt; PreTaxAmt&gt;100.8&lt;/ PreTaxAmt&gt;
      &lt; MatchAmt&gt;100.8&lt;/ MatchAmt&gt;
      &lt; RothAmt&gt;0&lt;/ RothAmt&gt;
      &lt; LoanAmt&gt;80&lt;/ LoanAmt&gt;
  &lt;/ Report_Entry&gt;
  &lt; Report_Entry&gt;
      &lt; Participant_ID&gt;037686040&lt;/ Participant_ID&gt;
      &lt; Workers&gt;
          &lt; FULL_PART_Time_Indicator&gt;1&lt;/ FULL_PART_Time_Indicator&gt;
          &lt; Last_Name&gt;Phonevilay&lt;/ Last_Name&gt;
          &lt; First_Name&gt;Somsaath&lt;/ First_Name&gt;
          &lt; Gender_Code&gt;1&lt;/ Gender_Code&gt;
          &lt; Rehire_Date_1&gt;2012-06-18-07:00&lt;/ Rehire_Date_1&gt;
      &lt;/ Workers&gt;
      &lt; End_Date_from_Pay_Period&gt;2016-01-03-08:00&lt;/ End_Date_from_Pay_Period&gt;
      &lt; PreTaxAmt&gt;61.81&lt;/ PreTaxAmt&gt;
      &lt; MatchAmt&gt;61.81&lt;/ MatchAmt&gt;
      &lt; RothAmt&gt;0&lt;/ RothAmt&gt;
      &lt; LoanAmt&gt;76&lt;/ LoanAmt&gt;
  &lt;/ Report_Entry&gt;
  &lt; Report_Entry&gt;
      &lt; Participant_ID&gt;043781931&lt;/ Participant_ID&gt;
      &lt; Workers&gt;
          &lt; FULL_PART_Time_Indicator&gt;1&lt;/ FULL_PART_Time_Indicator&gt;
          &lt; Last_Name&gt;Ousouphan&lt;/ Last_Name&gt;
          &lt; First_Name&gt;Phouangmala&lt;/ First_Name&gt;
          &lt; Gender_Code&gt;2&lt;/ Gender_Code&gt;
          &lt; Rehire_Date_1&gt;2001-03-20-08:00&lt;/ Rehire_Date_1&gt;
      &lt;/ Workers&gt;
      &lt; End_Date_from_Pay_Period&gt;2016-01-03-08:00&lt;/ End_Date_from_Pay_Period&gt;
      &lt; PreTaxAmt&gt;84.19&lt;/ PreTaxAmt&gt;
      &lt; MatchAmt&gt;0&lt;/ MatchAmt&gt;
      &lt; RothAmt&gt;75&lt;/ RothAmt&gt;
      &lt; LoanAmt&gt;90&lt;/ LoanAmt&gt;
  &lt;/ Report_Entry&gt;
  </code></pre>
  
  <p><strong>The XSLT code that I have used is below and it should output 6.</strong></p>
  
  <pre><code>&lt;xsl:if test="( PreTaxAmt != 0) or( MatchAmt != 0) or ( RothAmt != 0) "&gt;
          &lt;xsl:variable name="recordCount"&gt;
          &lt;xsl:value-of select=" 
              count( Report_Data/ Report_Entry/ PreTaxAmt) 
              "/&gt;
          &lt;/xsl:variable&gt;
      &lt;/xsl:if&gt;
              &lt;xsl:if  test=" LoanAmt != 0"&gt; 
                  &lt;xsl:variable name="recordCount1"&gt;
                      &lt;xsl:value-of select=" 
                          count( Report_Data/ Report_Entry/ LoanAmt) 
                          "/&gt;
  
                  &lt;/xsl:variable&gt;
                  &lt;xsl:variable name="var3" select="number($recordCount) + number($recordCount1)"/&gt;
              &lt;/xsl:if&gt;
  </code></pre>