title: XSLT Increment reset upon change in element
body: <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
    &lt;Projects&gt; 
      &lt;Project GUID="K9"&gt; 
      &lt;Name&gt;Range&lt;/Name&gt;
      &lt;WBSs&gt;
          &lt;WBS GUID="2"&gt;
              &lt;Name&gt;Site&lt;/Name&gt;
              &lt;ParentWBS/&gt;
              &lt;ProjectGUID&gt;K9&lt;/ProjectGUID&gt;
              &lt;ObjectId&gt;99040&lt;/ObjectId&gt;
              &lt;Activities/&gt;
              &lt;WBSs&gt;
                  &lt;WBS GUID="1"&gt;
                  &lt;Name&gt;West&lt;/Name&gt;
                  &lt;ParentWBS&gt;2&lt;/ParentWBS&gt;
                  &lt;ProjectGUID&gt;K9&lt;/ProjectGUID&gt;
                  &lt;ObjectId&gt;99046&lt;/ObjectId&gt;
                  &lt;Activities&gt;
                    &lt;Activity GUID="A100"&gt;
                      &lt;Name&gt;Task&lt;/Name&gt;
                      &lt;ProjectGUID&gt;K17&lt;/ProjectGUID&gt;
                      &lt;WBSCode&gt;1&lt;/WBSCode&gt;
                    &lt;/Activity&gt;
                  &lt;/Activities&gt;
               &lt;/WBS&gt;
               &lt;WBS GUID="2"&gt;
                   &lt;Name&gt;South&lt;/Name&gt;
                   &lt;ParentWBS&gt;2&lt;/ParentWBS&gt;
                   &lt;ProjectGUID&gt;K9&lt;/ProjectGUID&gt;
                   &lt;ObjectId&gt;99047&lt;/ObjectId&gt;
                   &lt;Activities&gt;
                     &lt;Activity GUID="A200"&gt;
                       &lt;Name&gt;Task 2&lt;/Name&gt;
                     &lt;/Activity&gt;
                   &lt;/Activities&gt;
               &lt;/WBS&gt;
               &lt;WBS GUID="3"&gt;
                   &lt;Name&gt;North&lt;/Name&gt;
                   &lt;ParentWBS&gt;2&lt;/ParentWBS&gt;
                   &lt;ProjectGUID&gt;K9&lt;/ProjectGUID&gt;
                   &lt;ObjectId&gt;99048&lt;/ObjectId&gt;
                   &lt;Activities/&gt;
               &lt;/WBS&gt;
            &lt;/WBSs&gt;
        &lt;/WBS&gt;
      &lt;/WBSs&gt;
      &lt;Activities/&gt;
    &lt;/Project&gt;
  &lt;/Projects&gt;
  </code></pre>
  
  <p>This is my current output:</p>
  
  <pre><code>&lt;OBJECTID&gt;99046&lt;/OBJECTID &gt;
  &lt;ACTIVITY&gt;0010&lt;/ACTIVITY&gt;
  &lt;OBJECTID&gt;99047&lt;/OBJECTID &gt;
  &lt;ACTIVITY&gt;0020&lt;/ACTIVITY&gt;
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>&lt;OBJECTID&gt;99046&lt;/OBJECTID &gt;
  &lt;ACTIVITY&gt;0010&lt;/ACTIVITY&gt;
  &lt;OBJECTID&gt;99047&lt;/OBJECTID &gt;
  &lt;ACTIVITY&gt;0010&lt;/ACTIVITY&gt;
  </code></pre>
  
  <p>When I have existing Activity and existing OBJECTID, the first 2 WHEN clauses work fine. The problem arises when there is no existing 
  ObjectID or ObjectID changes in the 3rd WHEN. What I need is to start at ‘0010’ and increment by 10 until $ObjectID changes, then start at ‘0010’ again and so on. My background is procedural programming so still getting used to functional way of doing things and the restriction of not changing variables.
  Thanks in advance.</p>
  
  <p>Here's the relevant XSLT code:</p>
  
  <pre><code>&lt;ACTIVITY&gt; 
  &lt;!-- count($ExistingActivity) = 1 -&gt; ObjectId &amp; Activity Id exist, increment $LastObjectIdActivity/ACTIVITY
     count($ExistingObjectId) = 1 -&gt; ObjectId exists, increment $LastObjectId/ACTIVITY  
     count($ExistingObjectId) = 0 -&gt; $ObjectId start at 0010 and keep incrementing until ObjectId change. 
  --&gt;
     &lt;xsl:variable name="incr" select="10"/&gt;
       &lt;xsl:choose&gt;
          &lt;xsl:when test="count($ExistingActivity) = 1"&gt;      &lt;!-- Exact match of ObjectId &amp; Activity --&gt;
          &lt;!-- Get last existing ObjectId activity --&gt;                        
            &lt;xsl:variable name="temp" select="$LastObjectIdActivity/ACTIVITY"/&gt;                                 
            &lt;xsl:variable name="Oper" select="format-number($temp, '0000')"/&gt;                              
            &lt;xsl:value-of select="$Oper"/&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:when test="count($ExistingObjectId) = 1"&gt; &lt;!-- Activity not found, But ObjectId exists--&gt;
          &lt;!-- Get last existing ObjectId activity --&gt;
            &lt;xsl:variable name="temp" select="$LastObjectId/ACTIVITY + $incr"/&gt;                                 
            &lt;xsl:variable name="Oper" select="format-number($temp, '0000')"/&gt;                              
            &lt;xsl:value-of select="$Oper"/&gt;
          &lt;/xsl:when&gt;                                                                            
          &lt;xsl:when test="count($ExistingObjectId) = 0"&gt;   &lt;!-- No Existing ObjectId --&gt;
            &lt;xsl:variable name="pos" select="(position() * 10)"/&gt;  
            &lt;xsl:variable name="NextOper" select="format-number($pos, '0000')"/&gt;  &lt;!-- Start Activity at 0010 reset on new ObjectId--&gt;
            &lt;xsl:value-of select="$NextOper"/&gt;
            &lt;!-- $NextOper should be incremented by 10 --&gt;                           
          &lt;/xsl:when&gt; 
          &lt;xsl:otherwise&gt;
          &lt;/xsl:otherwise&gt;                           
       &lt;/xsl:choose&gt;                    
  &lt;/ACTIVITY&gt;
  </code></pre>