title: XSLT counting child nodes
body: <p>I rarely use XLST and get confusing results when I try to count child nodes in a parent node.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The XML is structured as follows:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Response&gt;
    &lt;result&gt;
      &lt;name&gt;Someone&lt;/name&gt;
      **&lt;rating&gt;4.5&lt;/rating&gt;**
      &lt;review&gt;
        &lt;text&gt;Some review.&lt;/text&gt;
      &lt;/review&gt;
      &lt;review&gt;
        &lt;text&gt;Another review.&lt;/text&gt;
      &lt;/review&gt;
    &lt;/result&gt;
    &lt;result&gt;
      &lt;name&gt;Another one&lt;/name&gt;
      **&lt;rating&gt;2&lt;/rating&gt;**
      &lt;review&gt;
        &lt;text&gt;Blah, grieve, blah.&lt;/text&gt;
      &lt;/review&gt;
      &lt;review&gt;
        &lt;text&gt;Blah, grrrrr, blah.&lt;/text&gt;
      &lt;/review&gt;
      &lt;review&gt;
        &lt;text&gt;Blah, good grrrrr, blah.&lt;/text&gt;
      &lt;/review&gt;
    &lt;/result&gt;
    ...
    ...
  &lt;/Response&gt;
  </code></pre>
  
  <p>The template (simplified) is as follows:</p>
  
  <pre><code>**&lt;body&gt;
        &lt;xsl:apply-templates/&gt;
  &lt;/body&gt;**
  
  &lt;xsl:template match="Response/result"&gt;
    &lt;div class="item"&gt;
      &lt;div class="name"&gt;
        &lt;xsl:value-of select="name"/&gt;
      &lt;/div&gt;
      &lt;xsl:if test="rating"&gt;
          &lt;span class="review-count"&gt;
            **(&lt;xsl:value-of select="count(review)"/&gt;)**
          &lt;/span&gt;
      &lt;/xsl:if&gt;
    &lt;/div&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>I do not get the correct child node count from this approach. In addition to the <code>count(review)</code>, I tried <code>count(descendant::review)</code> and several xPath variations. I know I'm missing something simple - but what?</p>