Sorting a for-each using another node set with a common id
===

I'm hoping to sort a table generated by a for-each. The data which determins the sort is not contained within the for-each node set, <detail> but is instead contained within <seq>. There is a common_id between the <detal> node set and the <seq> node set. I've tried assigning the common_id to a local variable within the for-each but the sort cannot see it. Is there a way to achieve this? Some examples below.


Example XML

    <root>
      <seq>
        <common_id>B1U3</common_id>
        <seq_data>1</seq_data>
      </seq>
      <seq>
        <common_id>R3D</common_id>
        <seq_data>3</seq_data>
      </seq>
      <seq>
        <common_id>Y3110W</common_id>
        <seq_data>2</seq_data>
      </seq>

      <detail>
        <common_id>Y3110W</common_id>
        <other_data>spame</other_data>
      </detail>
      <detail>
        <common_id>B1U3</common_id>
        <other_data>spamo</other_data>
      </detail>
      <detail>
        <common_id>R3D</common_id>
        <other_data>spama</other_data>
      </detail>
    </root>

Required Output

    ____________________________    
    ¦Common Id      ¦Other Data¦
    ¦---------------¦----------¦
    ¦B1U3           ¦spama     ¦
    ¦Y3110W         ¦spame     ¦
    ¦R3d            ¦spamo     ¦
    ¦_______________¦__________¦

Current XSLT

    <xsl:template match="/">
      <table>
        <tr>
            <td>Common ID</td>
            <td>Other Data</td>
        </tr>
        <xsl:for-each select="detail">
          <xsl:variable name="local_id" select="common_id"/>
          <xsl:sort select="../seq[common_id = $local_id]/seq_data"/>
            <tr>
              <td><xsl:value-of select="common_id"/></td>
              <td><xsl:value-of select="other_data"/></td>
            </tr>
        </xsl:for-each>
      </table>
    </xsl:template>
  
    