title: XSLT: Process all following-sibling until a certain condition is met without processed nodes getting re-processed
body: <p>Consider the following XML structure:</p>
  
  <pre><code>&lt;a&gt;
      &lt;t&gt;abc&lt;/t&gt;
  &lt;/a&gt;
  &lt;a type="start"&gt;&lt;/a&gt;
  &lt;b&gt;
      &lt;t&gt;ignore&lt;/t&gt;
  &lt;/b&gt;
  &lt;a&gt;&lt;/a&gt;    
  &lt;a&gt;
      &lt;t&gt;1&lt;/t&gt;
  &lt;/a&gt;
  &lt;a&gt;
      &lt;t&gt;2&lt;/t&gt;
  &lt;/a&gt;
  &lt;a&gt;
      &lt;t&gt;3&lt;/t&gt;
  &lt;/a&gt;
  &lt;b&gt;
      &lt;t&gt;ignore&lt;/t&gt;
  &lt;/b&gt;
  &lt;a&gt;
      &lt;t&gt;4&lt;/t&gt;
  &lt;/a&gt;
  &lt;a type="end"&gt;&lt;/a&gt;
  &lt;a&gt;
      &lt;t&gt;def&lt;/t&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>I need to get the sum of contents of all the <code>a</code> tags between the <code>a</code> tags with attribute value <code>start</code> and <code>end</code>.</p>
  
  <p>I attempted it with the following XSL:</p>
  
  <pre><code>&lt;xsl:template match="a"&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="@type='start'"&gt;
              &lt;merged&gt;
                  &lt;xsl:call-template name="getMergedText"&gt;
                      &lt;xsl:with-param name="text" select="''"/&gt;
                  &lt;xsl:call-template&gt;
              &lt;/merged&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;single&gt;
                  &lt;xsl:value-of select="t"/&gt;
              &lt;/single&gt;
          &lt;/xsl:otherwise&gt;
      &lt;xsl:choose&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template name="getMergedText"&gt;
      &lt;xsl:param name="text"/&gt;
  
      &lt;xsl:choose&gt;
          &lt;xsl:when test="following-sibling::a[1]/@type='end'"&gt;
              &lt;xsl:value-of select="$text"/&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:when test="following-sibling::a[1]/t"&gt;
              &lt;xsl:variable name="text.update"&gt;
                  &lt;xsl:value-of select="$text"/&gt;
                  &lt;xsl:value-of select="following-sibling::a[1]/t"/&gt;
              &lt;/xsl:variable&gt;
              &lt;xsl:for-each select="following-sibling::a[1]"&gt;
                  &lt;xsl:call-template name="getMergedText"&gt;
                      &lt;xsl:with-param name="text" select="$text.update"/&gt;
                  &lt;xsl:call-template&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:for-each select="following-sibling::a[1]"&gt;
                  &lt;xsl:call-template name="getMergedText"&gt;
                      &lt;xsl:with-param name="text" select="$text"/&gt;
                  &lt;xsl:call-template&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>The required output is:</p>
  
  <pre><code>&lt;single&gt;abc&lt;/single&gt;
  &lt;merged&gt;1234&lt;/merged&gt;
  &lt;single&gt;def&lt;/single&gt;
  </code></pre>
  
  <p>The output I am getting is:</p>
  
  <pre><code>&lt;single&gt;abc&lt;/single&gt;
  &lt;merged&gt;1234&lt;/merged&gt;
  &lt;single&gt;1&lt;/single&gt;
  &lt;single&gt;2&lt;/single&gt;
  &lt;single&gt;3&lt;/single&gt;
  &lt;single&gt;4&lt;/single&gt;
  &lt;single&gt;def&lt;/single&gt;
  </code></pre>
  
  <p>How can I avoid the reprocessing of the <code>a</code> nodes already processed by <code>getMergedText</code> template?</p>
  
  <p>Thnx in advance!!</p>
  
  <p><strong>Note:</strong> I am working with XSLT 1.0. There can be multiple instances of the start-end node pairs in the XML with any number of nodes before, after and in between the pairs.</p>