title: XSLT external document lookup dynamic values
body: <p>I've looked through a number of Q&amp;A's here, but I'm still struggling how to start/get it done (I'm not fluent in xslt/xml). I've an XSL creating an XML feed. Now in addition, I've been given an XML file, in which I need to lookup a brand, and return the country and ID.</p>
  
  <p>The content of the brand_country.xml file:</p>
  
  <pre><code>&lt;Make&gt;
     &lt;man_id&gt;22&lt;/man_id&gt;
     &lt;man_name&gt;Bentley&lt;/man_name&gt;
     &lt;man_country&gt;Britain&lt;/man_country&gt;
     &lt;_type_&gt;car&lt;/_type_&gt;
  &lt;/Make&gt;
  &lt;Make&gt;
     &lt;man_id&gt;23&lt;/man_id&gt;
     &lt;man_name&gt;Benz&lt;/man_name&gt;
     &lt;man_country&gt;Germany&lt;/man_country&gt;
     &lt;_type_&gt;car&lt;/_type_&gt;
  &lt;/Make&gt;
  &lt;Make&gt;
     &lt;man_id&gt;24&lt;/man_id&gt;
     &lt;man_name&gt;Berkley&lt;/man_name&gt;
     &lt;man_country&gt;Britain&lt;/man_country&gt;
     &lt;_type_&gt;car&lt;/_type_&gt;
  &lt;/Make&gt;
  &lt;Make&gt;
     &lt;man_id&gt;25&lt;/man_id&gt;
     &lt;man_name&gt;Bitter&lt;/man_name&gt;
     &lt;man_country&gt;Germany&lt;/man_country&gt;
     &lt;_type_&gt;car&lt;/_type_&gt;
  &lt;/Make&gt;
  &lt;Make&gt;
     &lt;man_id&gt;28&lt;/man_id&gt;
     &lt;man_name&gt;BMW&lt;/man_name&gt;
     &lt;man_country&gt;Germany&lt;/man_country&gt;
     &lt;_type_&gt;car&lt;/_type_&gt;
  &lt;/Make&gt;
  </code></pre>
  
  <p>Now in my xsl I've</p>
  
  <pre><code>&lt;xsl:for-each select="entries/entry"&gt;
  
   &lt;root&gt;
    &lt;channel&gt;
     &lt;ad&gt;
      &lt;category_id&gt;1&lt;/category_id&gt;
      &lt;ad_id&gt;&lt;xsl:value-of select="id" /&gt;&lt;/ad_id&gt;
      &lt;locale&gt;en&lt;/locale&gt;
      &lt;country&gt;n/a&lt;/country&gt;
      &lt;make_id&gt;&lt;xsl:value-of select="fields/field_make/data" /&gt; &lt;/make_id&gt;
      &lt;year&gt;&lt;xsl:value-of select="fields/field_year/data" /&gt;&lt;/year&gt;
      &lt;handling&gt;&lt;xsl:value-of select="fields/field_lhdrhd/data" /&gt;&lt;/handling&gt;
      &lt;heading&gt;&lt;xsl:value-of select="fields/field_make/data" /&gt; &lt;xsl:text&gt; &lt;/xsl:text&gt; &lt;xsl:value-of select="name" /&gt;&lt;/heading&gt;
      &lt;reg_no&gt; &lt;/reg_no&gt;
      &lt;chassis_no&gt;&lt;xsl:value-of select="fields/field_chassis_nr/data" /&gt;&lt;/chassis_no&gt;
      &lt;engine_no&gt; &lt;/engine_no&gt;
  
      &lt;price_type&gt;
       &lt;xsl:choose&gt;
        &lt;xsl:when test="string-length( fields/field_price_poa/data )"&gt;
         &lt;xsl:text&gt;POA&lt;/xsl:text&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;Asking Pricefix&lt;/xsl:otherwise&gt;
       &lt;/xsl:choose&gt;
      &lt;/price_type&gt;
      &lt;price&gt;
       &lt;xsl:choose&gt;
        &lt;xsl:when test="string-length( fields/field_price_poa/data )"&gt;
         &lt;xsl:text&gt; &lt;/xsl:text&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;
         &lt;xsl:value-of select="fields/field_price/data" /&gt;
        &lt;/xsl:otherwise&gt;
       &lt;/xsl:choose&gt;
      &lt;/price&gt;
  
  
      &lt;currency_id&gt;
       &lt;xsl:choose&gt;
        &lt;xsl:when test="fields/field_currency/data = 'GBP'"&gt; 
         &lt;xsl:text&gt;20&lt;/xsl:text&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:when test="fields/field_currency/data = 'USD'"&gt; 
         &lt;xsl:text&gt;10&lt;/xsl:text&gt;
        &lt;/xsl:when&gt;               
       &lt;/xsl:choose&gt;     
      &lt;/currency_id&gt;   
  
     &lt;/ad&gt;
  
    &lt;/channel&gt;
   &lt;/root&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>This is what I need for the current feed.</p>
  
  <p>Now, I would need to use the content of…</p>
  
  <pre><code>&lt;xsl:value-of select="fields/field_make/data" /&gt;
  </code></pre>
  
  <p>…to lookup in the brand_country.xml file, and find: </p>
  
  <ol>
  <li>return the <em>"id"</em> for <code>&lt;make_id&gt;... &lt;/make_id&gt;</code></li>
  <li>return the *"_manufactured_in"* for <code>&lt;country&gt; ... &lt;/country&gt;</code></li>
  </ol>
  
  <p>this is what I have so far:</p>
  
  <pre><code>(cut....)
      &lt;xsl:key name="mancountry" match="man_country" use="../man_name"/&gt;
      &lt;xsl:key name="manid" match="man_id" use="../man_name"/&gt;
  
   &lt;xsl:template match="/section|/category|/entry_details"&gt;
  
     &lt;xsl:for-each select="entries/entry"&gt;
      &lt;root&gt;
      &lt;channel&gt;
       &lt;ad&gt;
        &lt;category_id&gt;1&lt;/category_id&gt;
        &lt;ad_id&gt;&lt;xsl:value-of select="id" /&gt;&lt;/ad_id&gt;
        &lt;locale&gt;en&lt;/locale&gt;
        &lt;xsl:variable name="inputmake" select="fields/field_make/data"/&gt;
  
        &lt;country&gt; 
        &lt;xsl:for-each select="document('http://www.xxx.yyy/dev/feed_data/brand_country.xml')"&gt;
               &lt;xsl:variable name="value" select="key('mancountry',$inputmake)"/&gt;
               &lt;xsl:choose&gt;
                 &lt;xsl:when test="$value"&gt;
                   &lt;xsl:value-of select="$value"/&gt;  
                 &lt;/xsl:when&gt;
                 &lt;xsl:otherwise&gt;world&lt;/xsl:otherwise&gt;
               &lt;/xsl:choose&gt;
             &lt;/xsl:for-each&gt;
        &lt;/country&gt;
  (cut....)
  </code></pre>
  
  <p>I appreciate any help and tips.</p>