title: XSLT 1.0: counting nodes in XSLT
body: <p>I need to output outbuilding nodes (only and only the ones that have a 'vacant'-child) with correct sequence number (see XML)</p>
  
  <p>There are no attributes in XML below, just elements and values.</p>
  
  <pre><code>&lt;stuff&gt;
      &lt;locations&gt;
          &lt;location&gt;
              &lt;properties&gt;
                  &lt;property&gt;
                      &lt;belongings&gt;
                          &lt;house&gt;
                              &lt;houseWithAC/&gt;
                          &lt;/house&gt;
                      &lt;/belongings&gt;
                  &lt;/property&gt;
                  &lt;property&gt;
                      &lt;belongings&gt;
                          &lt;outbuilding/&gt;
                      &lt;/belongings&gt;
                  &lt;/property&gt;
                  &lt;property&gt;
                      &lt;belongings&gt;
                          &lt;outbuilding&gt;
                              &lt;vacant/&gt;
                          &lt;/outbuilding&gt;
                      &lt;/belongings&gt;
                  &lt;/property&gt;
                  &lt;property&gt;              
                      &lt;belongings&gt;
                          &lt;outbuilding&gt;
                              &lt;vacant/&gt;
                          &lt;/outbuilding&gt;
                      &lt;/belongings&gt;
                  &lt;/property&gt;
                  &lt;property&gt;
                      &lt;belongings&gt;
                          &lt;vehicle/&gt;
                      &lt;/belongings&gt;
                  &lt;/property&gt;
              &lt;/properties&gt;
          &lt;/location&gt;
          &lt;location&gt;
              &lt;properties&gt;
                  &lt;property&gt;
                      &lt;belongings&gt;
                          &lt;vehicle/&gt;
                      &lt;/belongings&gt;
                  &lt;/property&gt;
                  &lt;property&gt;
                     &lt;belongings&gt;
                         &lt;outbuilding/&gt;
                     &lt;/belongings&gt;
                  &lt;/property&gt;
                  &lt;property&gt;
                      &lt;belongings&gt;
                          &lt;house/&gt;
                      &lt;/belongings&gt;
                  &lt;/property&gt;
                  &lt;property&gt;
                       &lt;belongings&gt;
                          &lt;outbuilding&gt;
                              &lt;vacant/&gt;
                          &lt;/outbuilding&gt;
                       &lt;/belongings&gt;
                   &lt;/property&gt;
                   &lt;property&gt;
                       &lt;belongings&gt;
                           &lt;barn/&gt;
                       &lt;/belongings&gt;
                   &lt;/property&gt;
               &lt;/properties&gt;
            &lt;/location&gt;
        &lt;/locations&gt;
  &lt;/stuff&gt;
  </code></pre>
  
  <ol>
  <li><p>sequencing: I need to count every belonging child node per location (for ex. house, outbuilding, vehicle)  <strong>Note, house with houseWithAC child counts as two nodes !!</strong>
  the sequence format is Loc ### / Item ###</p></li>
  <li><p>I need to output every outbuilding node that has vacant child, with the correct sequence numbers (see above)</p></li>
  </ol>
  
  <p>Note also: "collection" nodes like locations, properties have many children, while node belongings - only one.</p>
  
  <p>I tried to create a recursive loop, but id does not work: if I have outbuilding node without "vacant"-child I still step into the if statement (looks like that condition is allways true)</p>
  
  <p>Something like this:
      . . . . . . . . . . .</p>
  
  <pre><code>&lt;xsl:for-each select="Locations/Location"&gt;
      &lt;xsl:variable name="LOCID"&gt;
          &lt;xsl:number level="single" count="*" format="001"/&gt;
      &lt;/xsl:variable&gt;
     &lt;xsl:call-template name="WriteItems"&gt;
         &lt;xsl:with-param name="propertiesNodes" select="properties/property"/&gt;
         &lt;xsl:with-param name="NumberOfProperties" select="count(properties/property)"/&gt;
         &lt;xsl:with-param name="LocCount" select="$LOCID"/&gt;
     &lt;/xsl:call-template&gt;
  &lt;/xsl:for-each&gt;     
  . . . . . . . . . . . . .
  
  
  &lt;xsl:template name="WriteItems"&gt;
      &lt;xsl:param name="propertiesNodes"/&gt;
      &lt;xsl:param name="NumberOfProperties"/&gt;
      &lt;xsl:param name="LocCount"/&gt;
      &lt;xsl:param name="Index"&gt;1&lt;/xsl:param&gt;
      &lt;xsl:param name="ItemCount"&gt;1&lt;/xsl:param&gt;     
      &lt;xsl:choose&gt;
          &lt;xsl:when test="$Index &gt; $NumberOfProperties"/&gt;
           &lt;xsl:otherwise&gt;
             &lt;xsl:choose&gt;
                 &lt;xsl:when test="$propertiesNodes[$Index]/belongings/house/houseWithAC"&gt;
                    &lt;xsl:call-template name="WriteItems"&gt;
                        &lt;xsl:with-param name="propertiesNodes" select="$propertiesNodes"/&gt;
                        &lt;xsl:with-param name="NumberOfProperties" select="$NumberOfProperties"/&gt;
                        &lt;xsl:with-param name="LocCount" select="$LocCount"/&gt;
                        &lt;xsl:with-param name="Index" select="$Index + 1"/&gt;
                        &lt;xsl:with-param name="ItemCount" select="$ItemCount + 2"/&gt;
                    &lt;/xsl:call-template&gt;
                 &lt;/xsl:when&gt;
                 &lt;xsl:when test="$propertiesNodes[$Index]/belongings/house"&gt;
                     &lt;xsl:call-template name="WriteItems"&gt;
                         &lt;xsl:with-param name="propertiesNodes" select="$propertiesNodes"/&gt;
                         &lt;xsl:with-param name="NumberOfProperties" select="$NumberOfProperties"/&gt;
                         &lt;xsl:with-param name="LocCount" select="$LocCount"/&gt;
                         &lt;xsl:with-param name="Index" select="$Index + 1"/&gt;
                         &lt;xsl:with-param name="ItemCount" select="$ItemCount + 1"/&gt;
                     &lt;/xsl:call-template&gt;
                 &lt;/xsl:when&gt;
                 &lt;xsl:when test="$propertiesNodes[$Index]/belongings/vehicle"&gt;
                     &lt;xsl:call-template name="WriteItems"&gt;
                         &lt;xsl:with-param name="propertiesNodes" select="$propertiesNodes"/&gt;
                         &lt;xsl:with-param name="NumberOfProperties" select="$NumberOfProperties"/&gt;
                         &lt;xsl:with-param name="LocCount" select="$LocCount"/&gt;
                         &lt;xsl:with-param name="Index" select="$Index + 1"/&gt;
                         &lt;xsl:with-param name="ItemCount" select="$ItemCount + 1"/&gt;
                     &lt;/xsl:call-template&gt;
                 &lt;/xsl:when&gt;
                 &lt;xsl:when test="$propertiesNodes[$Index]/belongings/barn"&gt;
                     &lt;xsl:call-template name="WriteItems"&gt;
                         &lt;xsl:with-param name="propertiesNodes" select="$propertiesNodes"/&gt;
                         &lt;xsl:with-param name="NumberOfProperties" select="$NumberOfProperties"/&gt;
                         &lt;xsl:with-param name="LocCount" select="$LocCount"/&gt;
                         &lt;xsl:with-param name="Index" select="$Index + 1"/&gt;
                         &lt;xsl:with-param name="ItemCount" select="$ItemCount + 1"/&gt;
                     &lt;/xsl:call-template&gt;
                 &lt;/xsl:when&gt;
                 &lt;xsl:when test="$propertiesNodes[$Index]/belongings/outbuilding"&gt;
                    &lt;xsl:if test="$propertiesNodes[$Index]/belongings/outbuilding/vacant"&gt;
                          &lt;xsl:variable name="ITEMID"&gt;
                               &lt;xsl:value-of select="format-number($ItemCount,'000')"/&gt;
                          &lt;/xsl:variable&gt;  
  
                           .... output as  concat('-  ', $LocCount, '/', $ITEMID) ...... some description ....
  
  
                     &lt;/xsl:if&gt;
                     &lt;xsl:call-template name="WriteItems"&gt;
                         &lt;xsl:with-param name="propertiesNodes" select="$propertiesNodes"/&gt;
                         &lt;xsl:with-param name="NumberOfProperties" select="$NumberOfProperties"/&gt;
                         &lt;xsl:with-param name="LocCount" select="$LocCount"/&gt;
                         &lt;xsl:with-param name="Index" select="$Index + 1"/&gt;
                         &lt;xsl:with-param name="ItemCount" select="$ItemCount + 1"/&gt;
                     &lt;/xsl:call-template&gt;
                 &lt;/xsl:when&gt;
             &lt;/xsl:choose&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  </code></pre>