title: Repeat position within a loop in xslt
body: <p>I have two nested loops. The first loop is picking up distinct value and comparing in second loop. In second loop i am giving position() and it is coming sequential as 1,2,3 but i want it to appear as 1,1,1 during first iteration and 2,2,2 in second iteration and 3,3,3 in third iteration and so on.</p>
  <p>XML:</p>
  <pre><code>&lt;root&gt;
      &lt;Items&gt;
          &lt;Item&gt;
            &lt;ItemCode&gt;12345&lt;/ItemCode&gt;
            &lt;ItemColor&gt;Red&lt;/ItemColor&gt;
            &lt;Weight&gt;1Kg&lt;/Weight&gt;
          &lt;/Item&gt;
  
  
          &lt;Item&gt;
            &lt;ItemCode&gt;19087&lt;/ItemCode&gt;
            &lt;ItemColor&gt;Blue&lt;/ItemColor&gt;
            &lt;Weight&gt;1Kg&lt;/Weight&gt;
          &lt;/Item&gt;
    
      &lt;/Items&gt;
  
  &lt;Items&gt;
         
          &lt;Item&gt;
            &lt;ItemCode&gt;12345&lt;/ItemCode&gt;
            &lt;ItemColor&gt;Yellow&lt;/ItemColor&gt;
            &lt;Weight&gt;1Kg&lt;/Weight&gt;
          &lt;/Item&gt;
  
          &lt;Item&gt;
            &lt;ItemCode&gt;19087&lt;/ItemCode&gt;
            &lt;ItemColor&gt;Green&lt;/ItemColor&gt;
            &lt;Weight&gt;1Kg&lt;/Weight&gt;
          &lt;/Item&gt;
      
      &lt;/Items&gt;
  &lt;/root&gt;
  </code></pre>
  <p>Required Output:</p>
  <pre><code>  12345
    1.Red
    1.Yellow
    
    19087
    2.Blue
    2.Green
  
  (Or)
    1.12345
      Red
      Yellow
    
    2.19087
      Blue
      Green
  </code></pre>
  <p>Code:</p>
  <pre><code> &lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  
  &lt;xsl:key name=&quot;check&quot; match=&quot;Items&quot; use=&quot;ItemCode&quot; /&gt;
  
  &lt;xsl:template match=&quot;root/Items&quot;&gt;
       &lt;xsl:for-each select=&quot;Item[count(. | key('check', ItemCode)[1]) = 1]&quot;&gt;
               &lt;xsl:value-of select=&quot;ItemCode&quot;/&gt;
          &lt;xsl:for-each select=&quot;key('check', ItemCode)&quot;&gt; 
                 
                 &lt;xsl:value-of select=&quot;position()&quot;/&gt;
                 &lt;xsl:value-of select=&quot;ItemColor&quot;/&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p>I am using xslt-1.0
  Your help is appreciated. Thank you!</p>