title: XSLT - find position() when inside a nested for-each
body: <p>I'm using XSLT to perform a transformation on some fairly complex XML. In order to achieve the output I need to, I've had to create a nested for loop something like the following:</p>
  
  <p><em>Source XML</em></p>
  
  <pre><code>&lt;root&gt;
    &lt;element1&gt;
      &lt;child&gt;
        &lt;aaa&gt;&lt;/aaa&gt;
        &lt;bbb&gt;&lt;/bbb&gt;
      &lt;/child&gt;
    &lt;/element1&gt;
    &lt;element2/&gt;
    &lt;element3/&gt;
    &lt;element3/&gt;
    &lt;element3/&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p><em>XSLT</em></p>
  
  <pre><code>&lt;xsl:for-each select="element3"&gt;
    &lt;!-- Do some stuff --&gt;
  
    &lt;xsl:for-each select="../element1/child/*"&gt;
      &lt;!-- Do some more stuff --&gt;
    &lt;/xsl:for-each&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>What I'm trying to do here is whilst in my nested loop (on <code>../element1/child/*</code>) - I'd like to find out:</p>
  
  <ol>
  <li>The position of the element I'm currently looping - if, for example, I was currently focused on <code>&lt;bbb&gt;</code> then this position (I think) would be '1'</li>
  <li>The position of the parent loop (on <code>element3</code>) - so if, for example, I was on the third instance of <code>&lt;element3&gt;</code> and then looping through <code>../element1/child/*</code> and was focused on <code>&lt;aaa&gt;</code> - the two values I would be after would be '2' and '0'.</li>
  </ol>
  
  <p>Ideally, I'd like to be able to assign these values to a variable. I've tried using the <code>position()</code> notation like below but this doesn't seem to be working.</p>
  
  <pre><code>&lt;xsl:for-each select="../element1/child/*"&gt;
    &lt;xsl:variable name="postion_current_loop" select="position()"/&gt;
    &lt;xsl:variable name="postion_parent_loop" select="??????"/&gt;
    &lt;!-- Do some more stuff --&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>If anyone has any ideas on how I could achieve this that would be greatly appreciated! I'm using XSLT 2.0, but am open to solutions using XSLT 1.0 if need be. Thanks in advance.</p>