title: In XSLT nested foreach loop output count is twice and same
body: <p>In xslt transformation below is the input xml.</p>
  
  <pre><code>    &lt;root xmlns="test1"&gt;
      &lt;Entries xmlns="test2"&gt;
          &lt;root xmlns="test1"&gt;
              &lt;LAT&gt;1&lt;/LAT&gt;
          &lt;/root&gt;
          &lt;root xmlns="test1"&gt;
              &lt;LAT&gt;2&lt;/LAT&gt;
          &lt;/root&gt;
      &lt;/Entries&gt;
      &lt;Entries xmlns="test2"&gt;
          &lt;root xmlns="test1"&gt;
              &lt;LAT&gt;3&lt;/LAT&gt;
          &lt;/root&gt;
          &lt;root xmlns="test1"&gt;
              &lt;LAT&gt;4&lt;/LAT&gt;
          &lt;/root&gt;
      &lt;/Entries&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>The XSLT code i am using against this xml is.</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:p="test1"
  xmlns:r="test2"
                  version="1.0"&gt;
    &lt;xsl:template match="//*[local-name()='root']"&gt;
  &lt;xsl:element name="Test"&gt;
  &lt;xsl:for-each select="//p:root/r:Entries"&gt;
  
  &lt;xsl:variable name="i" select="position()"/&gt;
         &lt;xsl:for-each select="//p:root/r:Entries/p:root"&gt;
         &lt;xsl:element name="{concat('imei', $i)}"&gt; &lt;xsl:element name="LAT"&gt;&lt;xsl:value-of select="//*[local-name()='LAT']"/&gt;&lt;/xsl:element&gt;
  
  &lt;/xsl:element&gt;
        &lt;/xsl:for-each&gt;
  
        &lt;/xsl:for-each&gt;
  &lt;/xsl:element&gt;
    &lt;/xsl:template&gt; 
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The result I wanted is :</p>
  
  <pre><code>&lt;Test&gt;
  &lt;imei1&gt;
   &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei1&gt;
  &lt;imei1&gt;
   &lt;LAT&gt;2&lt;/LAT&gt;
  &lt;/imei1&gt;
  &lt;imei2&gt;
   &lt;LAT&gt;3&lt;/LAT&gt;
  &lt;/imei2&gt;
  &lt;imei4&gt;
   &lt;LAT&gt;4&lt;/LAT&gt;
  &lt;/imei4&gt;
  &lt;/Test&gt;
  </code></pre>
  
  <p>The result I am getting is:</p>
  
  <pre><code>&lt;Test&gt;
  &lt;imei1&gt;
      &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei1&gt;
  &lt;imei1&gt;
      &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei1&gt;
  &lt;imei1&gt;
      &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei1&gt;
  &lt;imei1&gt;
      &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei1&gt;
  &lt;imei2&gt;
      &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei2&gt;
  &lt;imei2&gt;
      &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei2&gt;
  &lt;imei2&gt;
      &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei2&gt;
  &lt;imei2&gt;
      &lt;LAT&gt;1&lt;/LAT&gt;
  &lt;/imei2&gt;
  </code></pre>
  
  <p></p>
  
  <p>The same value is repeating and looping twice, did i given the wrong xpath. The first loop is correctly executing twice as expected which we can see in the concatinated expression imei1 and imei2 is there but the second loop is executing twice the expected result count and giving same result.</p>