[xsl] Using position() to retrieve corresponding value in another node

Subject: [xsl] Using position() to retrieve corresponding value in another node
From: John Brown <johnbrown105@xxxxxxxxxxx>
Date: Mon, 31 Jan 2011 00:50:43 +0000 (UTC)
Hello All,

First of all, I am  sorry for asking what must be a very basic question:

My XML looks like this:

<table>
  <fields>
    <field name="f1"/> <! can also have other attributes such as -->
    <field name="f2"/> <!-- datatype, size, nullablity -->
    <field name="f3"/>
  </fields>
  <records> <!-- rows in the table -->
    <record>
      <field>9</field><field>8</field><field>7</field>
    </record>  
    <record>
      <field>6</field><field>5</field><field>4</field>
    </record>  
    <!-- blah-blah-blah -->
  </records>
</table> 

For each <record>, I want to output something like:
<record>
  <field>
    <name>f1</name> <value>9</value>
  </field>
  <field>
    <name>f2</name> <value>8</value>
  </field>
  <field>
    <name>f3</name> <value>7</value>
  </field>
</record>
<!-- etc. -->

My template looks like this:

  <xsl:template match="record">
    <record>
    <xsl:for-each select="field">
      <field>
        <name>
        <!-- *********** This is the problem ********** -->
          <xsl:value-of select="(ancestor::table)/fields/field[position()]/@name"/>
        </name>
        <value>
           <xsl:value-of select="."/>
        </value>
    </xsl:for-each>   


    </record>
  </xsl:template>

It seems that in my template, position() always = 1, so the field name
is always f1.

How can I pass the correct value of position() to the field name predicate?

Regards,
Alias John Brown.

Current Thread