title: position() and last() inside nested for-each xslt
body: <p>I have a stylesheet I'm using with a perl module that only works with XSLT 1.0. I want to create a JSON array inside a JSON dictionary so I need proper comma seperation for the elements. I'm parsing an XHTML table where there are 1 or more spans in the second cell. So for-each select="./tr" and then for-each select="./td[1]/span" or something like that. </p>
  
  <p><em><strong>After changing it a little it behaves as expected as Ian said it would.</em></strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="text" version="1.0" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD HTML 4.01//EN"  encoding="UTF-8" /&gt;
      &lt;xsl:template match="text()"&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="table/tbody"&gt;
          &lt;xsl:text&gt;[&lt;/xsl:text&gt;
          &lt;xsl:for-each select="./tr[not(@class='no-results')]"&gt;
              &lt;xsl:text&gt;{"&lt;/xsl:text&gt;
              &lt;xsl:value-of select="normalize-space(.//strong)" /&gt;
              &lt;xsl:text&gt;":{"ingredients":{&lt;/xsl:text&gt;
              &lt;xsl:for-each select=".//div[@class='reagent-list']//a[@class='item-link reagent']"&gt;
                  &lt;xsl:value-of select="substring(./@href, 14)" /&gt;:&lt;xsl:value-of select="normalize-space(./span[1])" /&gt;
                  &lt;xsl:if test="position() != last()"&gt;
                      &lt;xsl:text&gt;,&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:for-each&gt;
              &lt;xsl:text&gt;}&lt;/xsl:text&gt;
              &lt;xsl:if test="position() != last()"&gt;
                  &lt;xsl:text&gt;,&lt;/xsl:text&gt;
              &lt;/xsl:if&gt;
              &lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt;
          &lt;/xsl:for-each&gt;
          &lt;xsl:text&gt;]&lt;/xsl:text&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>I realize that the stylesheet does not match the xml below. The actual document is huge. I hope you understand what I mean, though. I just made this up:</p>
  
  <pre><code>&lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;a&lt;/th&gt;
        &lt;th&gt;b&lt;/th&gt;
        &lt;th&gt;c&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;foo&lt;/td&gt;
        &lt;td&gt;&lt;span&gt;an element&lt;/span&gt;&lt;/td&gt;
        &lt;td&gt;bar&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;foo&lt;/td&gt;
        &lt;td&gt;&lt;span&gt;an element&lt;/span&gt;&lt;span&gt;an element&lt;/span&gt;&lt;/td&gt;
        &lt;td&gt;bar&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;foo&lt;/td&gt;
        &lt;td&gt;&lt;span&gt;an element&lt;/span&gt;&lt;span&gt;an element&lt;/span&gt;&lt;span&gt;an element&lt;/span&gt;&lt;span&gt;an element&lt;/span&gt;&lt;/td&gt;
        &lt;td&gt;bar&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>=></p>
  
  <pre><code>{
    "Row one":["an element"],
    "Row two":["an element", "an element"],
    "Row three":["an element", "an element", "an element", "an element"]
  }
  </code></pre>
  
  <p>Instead I get this:</p>
  
  <pre><code>{
    "Row one":["an element",],
    "Row two":["an element", "an element",],
    "Row three":["an element" "an element" "an element" "an element"]
  }
  </code></pre>
  
  <p>I've been using <code>position()</code> and <code>last()</code> in a test tag to print a comma and it seems to work correctly for the outer loop, but how do I tell my test tag to use the inner for-each scope when printing the commas that seperate the array?</p>