title: XSLT dynamic attribute inside for-each
body: <p>I'm working on an XSLT file to transform an XML doc that represents a table into SQL commands.  The XML describes both the table's columns and each row of data with the following structure:</p>
  
  <pre><code>&lt;Table&gt;
  &lt;ObjectAttributes&gt;
    &lt;AttributeDetail&gt;
      &lt;Name&gt;COLNAME1&lt;/Name&gt;
      ...
    &lt;/AttributeDetail&gt;
    &lt;AttributeDetail&gt;
      &lt;Name&gt;COLNAME2&lt;/Name&gt;
      ...
    &lt;/AttributeDetail&gt;
    ...
  &lt;/ObjectAttributes&gt;
  &lt;Record RowID="00001"&gt;
    &lt;Attributes&gt;
      &lt;Attr&gt;
        &lt;AttrName&gt;COLNAME1&lt;/AttrName&gt;
        &lt;AttrValue&gt;VALUE&lt;/AttrValue&gt;
      &lt;/Attr&gt;
      &lt;Attr&gt;
        &lt;AttrName&gt;COLNAME2&lt;/AttrName&gt;
        &lt;AttrValue&gt;VALUE&lt;/AttrValue&gt;
      &lt;/Attr&gt;
      ...
    &lt;/Attributes&gt;
  &lt;/Record&gt;
  &lt;Record RowID="00002"&gt;
    &lt;Attributes&gt;
      &lt;Attr&gt;
        &lt;AttrName&gt;COLNAME1&lt;/AttrName&gt;
        &lt;AttrValue&gt;VALUE&lt;/AttrValue&gt;
      &lt;/Attr&gt;
      &lt;Attr&gt;
        &lt;AttrName&gt;COLNAME2&lt;/AttrName&gt;
        &lt;AttrValue&gt;VALUE&lt;/AttrValue&gt;
      &lt;/Attr&gt;
      ...
    &lt;/Attributes&gt;
  &lt;/Record&gt;
  ...
  &lt;/Table&gt;
  </code></pre>
  
  <p>I've written the following XSLT to create INSERT commands:</p>
  
  <pre><code>&lt;xsl:for-each select="/Table/Record"&gt;
    &lt;xsl:variable name="ThisRecord" select="."/&gt;
    &lt;Command&gt;
      INSERT INTO &lt;xsl:value-of select="../ObjectName"/&gt;
        (ROWID, 
        &lt;xsl:for-each select="../ObjectAttributes/AttributeDetail"&gt;
          &lt;xsl:value-of select="Name"/&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;) 
        VALUES 
        (&lt;xsl:value-of select="@RowID"/&gt;, 
        &lt;xsl:for-each select="../ObjectAttributes/AttributeDetail"&gt;
          &lt;xsl:text&gt;'&lt;/xsl:text&gt;&lt;xsl:value-of select="$ThisRecord/Attributes/Attr/AttrValue[../AttrName='COLNAME1']"/&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:for-each&gt;)
    &lt;/Command&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>This works great except for the fact that I'm struggling to figure out how to replace the value for AttrName='' with the value that would result from:</p>
  
  <pre><code>&lt;xsl:value-of select="Name"/&gt;
  </code></pre>
  
  <p>I've tried replacing it with a variable, but this did not work:</p>
  
  <pre><code>&lt;xsl:variable name="ThisAttr" select="Name"/&gt;
  &lt;xsl:value-of select="$ThisRecord/Attributes/Attr/AttrValue[../AttrName='$ThisAttr']"/&gt;
  </code></pre>
  
  <p>How can I fill in this attribute dynamically?</p>
  
  <p>Thanks!</p>