title: Read XML file inside XSLT and using that to construct new XML
body: <p>I want to read an XML file inside XSLT and check its nodes. if they match the value of XML node to which I am applying the transform, then grab the value of the XML node and use it to construct a new XML structure. </p>
  
  <p>Here is an example to illustrate my problem. I think many people can use this question for reference to achieve similar functionality. </p>
  
  <p><strong>Referenced.xml</strong></p>
  
  <pre><code>&lt;xml&gt;
    &lt;root&gt;
      &lt;Id id = "1"&gt;
        &lt;fields&gt;
          &lt;field&gt;
            &lt;name&gt; Name1 &lt;/name&gt;
            &lt;value&gt; Val1 &lt;/value&gt;
          &lt;/field&gt;
          &lt;field&gt;
            &lt;name&gt; Name2 &lt;/name&gt;
            &lt;value&gt; Val2 &lt;/value&gt;
          &lt;/field&gt;
        &lt;/fields&gt; 
      &lt;/Id&gt;
      &lt;Id id = "2"&gt;
      ...
      &lt;/Id&gt;
    &lt;/root&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p><strong>Xml.xml</strong></p>
  
  <pre><code>&lt;XML&gt;
    &lt;Fields&gt;
     &lt;Id id = "1"&gt;
      &lt;F1&gt; Value1 &lt;/F1&gt;
      &lt;F2&gt; Value2 &lt;/F2&gt;
      &lt;F1&gt; Value3 &lt;/F1&gt;
      &lt;F4&gt; Value4 &lt;/F4&gt;
   &lt;/Id&gt;
    &lt;/Fields&gt;
  &lt;/XML&gt;
  </code></pre>
  
  <p>Now, I want to create a transform which will iterate through the XML file (<code>Referenced.xml</code>) and check where Id inside the both xml's match and then, inside that id, <code>Name1 = F1</code> and wherever it is then, fetch 'value' for that 'name' and create an XML structure like</p>
  
  <pre><code>&lt;outputXml&gt;
    &lt;Field id="Val1"&gt;
      &lt;val&gt; Value1 &lt;/val&gt;
    &lt;/Field&gt;
    &lt;Field id="Val2"&gt;
      &lt;val&gt; Value2 &lt;/val&gt;
    &lt;/Field&gt; ... and so on
  &lt;/outputXml&gt;
  </code></pre>
  
  <p>I know I have to use <a href="http://www.w3schools.com/XSL/func_document.asp" rel="nofollow">document()</a>, but I am not sure how do you iterate through the <code>Referenced.xml</code> inside xsl and use if, else to achieve the functionality needed ?</p>