title: How to use attribute values from another XML file as an element value selection in the current XML
body: <p>I have two XML files. One is the main XML file and the other one is used as a lookup table. Here is the main XML:</p>
  
  <pre><code>&lt;Report&gt;
        &lt;row&gt;
              &lt;field1&gt;test1&lt;/field1&gt;
              &lt;field2&gt;test2&lt;/field2&gt;
              &lt;field3&gt;test3&lt;/field3&gt;
        &lt;/row&gt;
        &lt;row&gt;
              &lt;field1&gt;test4&lt;/field1&gt;
              &lt;field2&gt;test5&lt;/field2&gt;
              &lt;field3&gt;test6&lt;/field3&gt;
        &lt;/row&gt;
  &lt;/Report&gt;
  </code></pre>
  
  <p>The lookup xml file looks like this:</p>
  
  <pre><code>&lt;lookup&gt;
        &lt;fieldmapping name="field1"&gt;fieldA&lt;/fieldmapping&gt;
        &lt;fieldmapping name="field2"&gt;fieldB&lt;/fieldmapping&gt;
        &lt;fieldmapping name="field3"&gt;fieldC&lt;/fieldmapping&gt;
  &lt;/lookup&gt;
  </code></pre>
  
  <p>Here is the output xml I want:</p>
  
  <pre><code>&lt;Items&gt;
        &lt;Item&gt;
             &lt;FieldName name="fieldA"&gt;test1&lt;/FieldName&gt;
             &lt;FieldName name="fieldB"&gt;test2&lt;/FieldName&gt;
             &lt;FieldName name="fieldC"&gt;test3&lt;/FieldName&gt;
        &lt;/Item&gt;
        &lt;Item&gt;
             &lt;FieldName name="fieldA"&gt;test4&lt;/FieldName&gt;
             &lt;FieldName name="fieldB"&gt;test5&lt;/FieldName&gt;
             &lt;FieldName name="fieldC"&gt;test6&lt;/FieldName&gt;
        &lt;/Item&gt;
  &lt;/Items&gt;
  </code></pre>
  
  <p>I am using the following XSLT and can not figure out how to select the value from field1, field2, and field3:</p>
  
  <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
  &lt;xsl:variable name="mappingLookupDoc" select="document('lookup.xml')/lookup/fieldmapping "/&gt;
  
  &lt;xsl:key name="mappingKey" match="fieldmapping " use="@name"/&gt;
  
  &lt;xsl:template match="report"&gt;
      &lt;xsl:apply-templates select="$mappingLookupDoc"/&gt;
      &lt;xsl:apply-templates/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/"&gt;
      &lt;Items xmlns="http://www.w3.org/1999/xhtml" schemaVersion="1.0"&gt;
          &lt;xsl:for-each select="report/row"&gt;
              &lt;Item&gt;
                  &lt;xsl:for-each select="$mappingLookupDoc"&gt;
                      &lt;xsl:variable name="fieldname" select="@name"/&gt;
                      &lt;FieldName&gt;
                      &lt;xsl:attribute name="name"&gt;
                              &lt;xsl:value-of select="." /&gt;
                      &lt;/xsl:attribute&gt;
                      &lt;xsl:value-of select="/report/row/?????"/&gt;
                      &lt;/FieldName&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/Item&gt;
          &lt;/xsl:for-each&gt;
      &lt;/Items&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>