title: increment counter in XSLT 1.0
body: <p>This is the input XML:</p>
  
  <pre><code>&lt;Move-Afile&gt;
    &lt;Afile&gt;
      &lt;Item&gt;
        &lt;PackNumber&gt;1234&lt;/PackNumber&gt;
      &lt;/Item&gt;
      &lt;Item&gt;
        &lt;PackNumber&gt;567&lt;/PackNumber&gt;
      &lt;/Item&gt;
      &lt;Item&gt;
        &lt;PackNumber&gt;567&lt;/PackNumber&gt;
      &lt;/Item&gt;
      &lt;Item&gt;
        &lt;PackNumber&gt;126&lt;/PackNumber&gt;
      &lt;/Item&gt;
      &lt;Item&gt;
        &lt;PackNumber&gt;876&lt;/PackNumber&gt;
      &lt;/Item&gt;
    &lt;/Afile&gt;
  &lt;/Move-Afile&gt;
  </code></pre>
  
  <p><code>&lt;Item&gt;</code> is an unbounded element which contains <code>&lt;PackNumber&gt;</code> as a child element. For each pack number we need to increment the counter variable, but here one condition is present like if previous <code>&lt;PackNumber&gt;</code> is equal to current <code>&lt;PackNumber&gt;</code> we have to ignore the counter (there is no need to increment) like below output.</p>
  
  <p>Inside the <code>for-each</code>, can we  get the counter like below XSLT sample?</p>
  
  <p>This is my XSLT template</p>
  
  <pre><code>&lt;xsl:template match="/"&gt;
    &lt;A&gt;
      &lt;target&gt;
        &lt;xsl:for-each select="/inp1:Move-Afile/inp1:Afile/inp1:Item/inp1:PalletNumber"&gt;
  
          &lt;xsl:variable name="count"&gt;
            &lt;!-- get the count here--&gt;
          &lt;/xsl:variable&gt;
  
          &lt;counter&gt;$count&lt;/counter&gt;
          &lt;PNumber&gt;&lt;xsl:value-of select="."/&gt;&lt;/PNumber&gt;
  
        &lt;/xsl:for-each&gt;
      &lt;/target&gt;
    &lt;/A&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>This is the XML output:</p>
  
  <pre><code>&lt;A&gt;
    &lt;target&gt;
      &lt;Item&gt;
        &lt;counter&gt;1&lt;/counter&gt;&lt;!-- if previous &lt;PackNumber&gt; is not equal to current &lt;PackNumber&gt; increment the count--&gt;
        &lt;PNumber&gt;1234&lt;/PNumber&gt;
      &lt;/Item&gt;
      &lt;Item&gt;
        &lt;counter&gt;2&lt;/counter&gt;&lt;!-- if previous &lt;PackNumber&gt; is not equal to current &lt;PackNumber&gt; increment the count--&gt;
        &lt;PNumber&gt;567&lt;/PNumber&gt;
      &lt;/Item&gt;
      &lt;Item&gt;&lt;!-- if previous &lt;PackNumber&gt; is  equal to current &lt;PackNumber&gt; ignore the count--&gt;no need to increment 
        &lt;PNumber&gt;567&lt;/PNumber&gt;
      &lt;/Item&gt;
      &lt;Item&gt;
        &lt;counter&gt;3&lt;/counter&gt;&lt;!-- if previous &lt;PackNumber&gt; is not equal to current &lt;PackNumber&gt; increment the count--&gt;
        &lt;PNumber&gt;126&lt;/PNumber&gt;
      &lt;/Item&gt;
      &lt;Item&gt;&lt;!-- if previous &lt;PackNumber&gt; is  equal to current &lt;PackNumber&gt; ignore the count--&gt;no need to increment 
        &lt;PNumber&gt;126&lt;/PNumber&gt;
      &lt;/Item&gt;
    &lt;/target&gt;
  &lt;/A&gt;
  </code></pre>
  
  <p>XML output 2:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;A&gt;
    &lt;target&gt;
      &lt;counter&gt;1&lt;/counter&gt;
      &lt;PNumber&gt;1234&lt;/PNumber&gt;
      &lt;counter&gt;2&lt;/counter&gt;
      &lt;PNumber&gt;567&lt;/PNumber&gt;
      &lt;!-- IF PNumber is equal we have to ignore the Total loop --&gt;
      &lt;counter&gt;3&lt;/counter&gt;
      &lt;PNumber&gt;126&lt;/PNumber&gt;
      &lt;counter&gt;4&lt;/counter&gt;
      &lt;PNumber&gt;876&lt;/PNumber&gt;
    &lt;/target&gt;
  &lt;/A&gt;
  </code></pre>