title: How to Count Nodes in XSLT?
body: <p>I now came to the last step of my XSL stylesheet, which needs me to output a number representing the total number of nodes added. Actually, it doesn't seem to be so straight-forward to me:</p>
  
  <p>First of all, I am not counting the nodes from the original xml document, I am going to count certain nodes from the resulting XML(the initial xml document could be empty).</p>
  
  <p>Secondly, I am not counting all of the nodes.</p>
  
  <p>For example, here is a piece of my XSLT code:</p>
  
  <pre><code>&lt;xsl:template name="Loop2000A"&gt;
    &lt;Loop2000A&gt;
  &lt;HL&gt;
    &lt;HL01&gt;
      &lt;xsl:value-of select="'1'"/&gt;
    &lt;/HL01&gt;
    &lt;HL03&gt;
      &lt;xsl:value-of select="'20'"/&gt;
    &lt;/HL03&gt;
  &lt;/HL&gt;
   &lt;xsl:if test="$recbat//provider_taxonomy_qual !='' "&gt;
  &lt;PRV&gt;
   &lt;PRV01&gt;
     &lt;xsl:value-of select="'BI'"/&gt;
   &lt;/PRV01&gt;
       &lt;PRV02&gt;
     &lt;xsl:value-of select="$recbat//provider_taxonomy_qual"/&gt;
   &lt;/PRV02&gt;
  &lt;PRV03&gt;
     &lt;xsl:value-of select="$recbat//provider_taxonomy"/&gt;
  &lt;/PRV03&gt;
     &lt;/PRV&gt;
    &lt;/xsl:if&gt;
  &lt;xsl:call-template name="Loop2010AA"/&gt;
  </code></pre>
  
  <p>So for this code,  and  are called segments, and those $recbat etc. are data sources. So want I really want to count is the number of these segments, and it can across different templates(like "Loop2010AA" template above, which is another template), and these segments may have loops, so they may be created 3 times consecutively, which our counter should increment 3 accordingly, or maybe the "if" statement fails, thus no such segment gets created, then the counter should stay the same.</p>
  
  <p>I am not sure the right way to implement this, since most of the tutorial I've googled are about counting the nodes in the original XML documents. One idea now I have is to use the parameter passing, but I have no idea then how to make this parameter a global one?</p>
  
  <p>Alternatively, I am thinking of a more direct way of doing this. Instead of using a counter variable, is there any way that I can generate the output xml document first, then I count the nodes I want in this xml and then generate the TRUE output with the number in it.</p>