I have the (simplified) xml:

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Testing.xsl"?>
<questionset>
  <question>
    <stem>Question 1.a</stem><answer>1</answer>
    <stem>Question 1.b</stem><answer>2</answer>
    <stem>Question 1.c</stem><answer>3</answer>
    <stem>Question 1.d</stem><answer>4</answer>
  </question>
  <question>
    <stem>Question 2.a</stem><answer>5</answer>
    <stem>Question 2.b</stem><answer>6</answer>
    <stem>Question 2.c</stem><answer>7</answer>
  </question>
</questionset>
Being transformed by the XSLT:

Code:
<xsl:for-each select="questionset/question/stem">
  <xsl:variable name="qnum">
    <xsl:value-of select="position()"/>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="self::stem">
      <xsl:for-each select="../answer">
        <xsl:variable name="zero" select="0"/>
        <xsl:variable name="cor_count">
          <xsl:value-of select="count(../answer)"/>
        </xsl:variable>
        <xsl:variable name="index">
          <xsl:value-of select="position()"/>
        </xsl:variable>
        <xsl:variable name="difference">
          <xsl:value-of select="$qnum mod $cor_count"/>
        </xsl:variable>
<div>
  <xsl:if test="($index = $difference) or ($index = $cor_count and $difference = $zero)">
    Answer_<xsl:value-of select="$qnum"/> = '<xsl:value-of select="."/>';
  </xsl:if>
</div>
      </xsl:for-each>
    </xsl:when>
  </xsl:choose>
</xsl:for-each>
The problem is that the answers come out of order... why is this???
If I add a fourth question to the second questionset above in the XML, then it comes out right. I'm confused???