title: When XSLT for-each is in a loop. How can I add an attribute or node to that XML based on other XML value. USING XSLT
body: <p>Can any one help me to sort this problem?</p>
  
  <p>I have an XML and filtering the values based on some condition. Storing the filtered xml in a variable. While filtering the condition, I'm trying to add an attribute or node to the filtered xml but it is not working for me..</p>
  
  <p>Input XML:</p>
  
  <pre><code>    &lt;root&gt;
          &lt;a id="13"&gt;
              &lt;b&gt;XXX1&lt;/b&gt;
              &lt;c&gt;YYY1&lt;/c&gt;
          &lt;/a&gt;
          &lt;a id="2"&gt;
              &lt;b&gt;XXX2&lt;/b&gt;
              &lt;c&gt;YYY2&lt;/c&gt;
          &lt;/a&gt;
          &lt;a id="15"&gt;
              &lt;b&gt;XXX3&lt;/b&gt;
              &lt;c&gt;YYY3&lt;/c&gt;
          &lt;/a&gt;
          &lt;a id="37"&gt;
              &lt;b&gt;XXX4&lt;/b&gt;
              &lt;c&gt;YYY4&lt;/c&gt;
          &lt;/a&gt;
          &lt;a id="51"&gt;
              &lt;b&gt;XXX5&lt;/b&gt;
              &lt;c&gt;YYY5&lt;/c&gt;
          &lt;/a&gt;
      &lt;/root&gt;
  </code></pre>
  
  <p>Another XML which is stored in a variable called "data" (this is for filtering):</p>
  
  <pre><code>&lt;sample&gt;
      &lt;con id="37" order="1"/&gt;
      &lt;con id="13" order="2"/&gt;
      &lt;con id="51" order="3"/&gt;
      &lt;con id="2" order="4"/&gt;
      &lt;con id="15" order="5"/&gt;
  &lt;/sample&gt;
  </code></pre>
  
  <p>Using XSLT, I'm trying to filter &amp; add a element in this way.</p>
  
  <pre><code>&lt;xsl:variable name="filteredData"&gt;
      &lt;newroot&gt;
        &lt;xsl:for-each select="/root/a[@id &gt; 14]"&gt;
          &lt;xsl:if test="msxsl:node-set($data)/sample/con[@id = current()/@id]/@id = current()/@id"&gt;
            &lt;xsl:element name="order"&gt;
              &lt;xsl:value-of select="msxsl:node-set($data)/sample/con[@id = current()/@id]/@order"/&gt;
            &lt;/xsl:element&gt;
          &lt;/xsl:if&gt;
        &lt;/xsl:for-each&gt;
      &lt;/newroot&gt;
  &lt;/xsl:variable&gt;
  </code></pre>
  
  <p>OUTPUT XML (i.e., "filteredData" variable should contain below XML):</p>
  
  <pre><code>     &lt;newroot&gt;
          &lt;a id="15"&gt;
              &lt;b&gt;XXX3&lt;/b&gt;
              &lt;c&gt;YYY3&lt;/c&gt;
              &lt;order&gt;5&lt;/order&gt;
          &lt;/a&gt;
          &lt;a id="37"&gt;
              &lt;b&gt;XXX4&lt;/b&gt;
              &lt;c&gt;YYY4&lt;/c&gt;
              &lt;order&gt;1&lt;/order&gt;
          &lt;/a&gt;
          &lt;a id="51"&gt;
              &lt;b&gt;XXX5&lt;/b&gt;
              &lt;c&gt;YYY5&lt;/c&gt;
              &lt;order&gt;3&lt;/order&gt;
          &lt;/a&gt;
      &lt;/newroot&gt;
  </code></pre>