title: Transforming xml based on node values
body: <p>My requirement is slightly complex one.I have the following input xml:</p>
  
  
  
  <pre><code>&lt;results&gt;
     &lt;row&gt;
        &lt;CASEID&gt;C1&lt;/CASEID&gt;
        &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
        &lt;ISSUEID&gt;I1&lt;/ISSUEID&gt;
        &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
        &lt;OBJECTID&gt;1&lt;/OBJECTID&gt;
     &lt;/row&gt;
     &lt;row&gt;
        &lt;CASEID&gt;C1&lt;/CASEID&gt;
        &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
        &lt;ISSUEID&gt;I2&lt;/ISSUEID&gt;
        &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
        &lt;OBJECTID&gt;2&lt;/OBJECTID&gt;
     &lt;/row&gt;
     &lt;row&gt;
        &lt;CASEID&gt;C1&lt;/CASEID&gt;
        &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
        &lt;ISSUEID&gt;I1&lt;/ISSUEID&gt;
        &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
        &lt;OBJECTID&gt;extra&lt;/OBJECTID&gt;
     &lt;/row&gt;
     &lt;row&gt;
        &lt;CASEID&gt;C2&lt;/CASEID&gt;
        &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
        &lt;ISSUEID&gt;I3&lt;/ISSUEID&gt;
        &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
        &lt;OBJECTID&gt;3&lt;/OBJECTID&gt;
     &lt;/row&gt;
  &lt;/results&gt;
  </code></pre>
  
  <p>I have to transform the above xml to simpler one using XSLT by following the below conditions:</p>
  
  <p>1)First target is to move all the <code>&lt;CASEID&gt;</code>'s whose values are same under the new <code>&lt;CASE&gt;</code> tag along with <code>&lt;CASEBA&gt;</code> node. </p>
  
  <p>For Example: </p>
  
  <pre><code>&lt;CASE&gt;
     &lt;CASEID&gt;C1&lt;/CASEID&gt;
     &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
  &lt;/CASE&gt;
  </code></pre>
  
  <p>2)List out all the <code>&lt;ISSUEID&gt;</code> &amp;<code>&lt;ISSUEBA&gt;</code> whose <code>&lt;CASEID&gt;</code>'s are equal in different <code>&lt;row&gt;</code>'s and move them under the new tag<code>&lt;ISSUE&gt;</code> within the recently created <code>&lt;CASE&gt;</code> tag.</p>
  
  <p>For example:</p>
  
  <pre><code> &lt;CASE&gt;
       &lt;CASEID&gt;C1&lt;/CASEID&gt;
       &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
       &lt;ISSUE&gt;
          &lt;ISSUEID&gt;I1&lt;/ISSUEID&gt;
          &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
       &lt;/ISSUE&gt;
       &lt;ISSUE&gt;
          &lt;ISSUEID&gt;I2&lt;/ISSUEID&gt;
          &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
       &lt;/ISSUE&gt;
    &lt;/CASE&gt;
  </code></pre>
  
  <p>3)List out all the <code>&lt;OBJECTID&gt;</code>'s whose <code>&lt;ISSUEID&gt;</code>'s are equal in different <code>&lt;row&gt;</code>'s and move them under the new <code>&lt;SOURCE&gt;</code> tag within <code>&lt;ISSUE&gt;</code>tag which will be definitely under the <code>&lt;CASE&gt;</code> .
  For example:</p>
  
  <pre><code>&lt;CASE&gt;
           &lt;CASEID&gt;C1&lt;/CASEID&gt;
           &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
           &lt;ISSUE&gt;
              &lt;ISSUEID&gt;I1&lt;/ISSUEID&gt;
              &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
              &lt;SOURCE&gt;
                 &lt;OBJECTID&gt;1&lt;/OBJECTID&gt;
              &lt;/SOURCE&gt;
              &lt;SOURCE&gt;
                 &lt;OBJECTID&gt;extra&lt;/OBJECTID&gt;
              &lt;/SOURCE&gt;
           &lt;/ISSUE&gt;
           &lt;ISSUE&gt;
              &lt;ISSUEID&gt;I2&lt;/ISSUEID&gt;
              &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
              &lt;SOURCE&gt;
                 &lt;OBJECTID&gt;2&lt;/OBJECTID&gt;
              &lt;/SOURCE&gt;
           &lt;/ISSUE&gt;
        &lt;/CASE&gt;
  </code></pre>
  
  <p>The final output xml should be as below:</p>
  
  
  
  <pre><code>&lt;results&gt;
     &lt;CASE&gt;
        &lt;CASEID&gt;C1&lt;/CASEID&gt;
        &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
        &lt;ISSUE&gt;
           &lt;ISSUEID&gt;I1&lt;/ISSUEID&gt;
           &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
           &lt;SOURCE&gt;
              &lt;OBJECTID&gt;1&lt;/OBJECTID&gt;
           &lt;/SOURCE&gt;
           &lt;SOURCE&gt;
              &lt;OBJECTID&gt;extra&lt;/OBJECTID&gt;
           &lt;/SOURCE&gt;
        &lt;/ISSUE&gt;
        &lt;ISSUE&gt;
           &lt;ISSUEID&gt;I2&lt;/ISSUEID&gt;
           &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
           &lt;SOURCE&gt;
              &lt;OBJECTID&gt;2&lt;/OBJECTID&gt;
           &lt;/SOURCE&gt;
        &lt;/ISSUE&gt;
     &lt;/CASE&gt;
     &lt;CASE&gt;
        &lt;CASEID&gt;C2&lt;/CASEID&gt;
        &lt;CASEBA&gt;MEDICAID&lt;/CASEBA&gt;
        &lt;ISSUE&gt;
           &lt;ISSUEID&gt;I3&lt;/ISSUEID&gt;
           &lt;ISSUEBA&gt;MEDICAID&lt;/ISSUEBA&gt;
           &lt;SOURCE&gt;
              &lt;OBJECTID&gt;3&lt;/OBJECTID&gt;
           &lt;/SOURCE&gt;
        &lt;/ISSUE&gt;
     &lt;/CASE&gt;
  &lt;/results&gt;
  </code></pre>
  
  <p>Please forgive me if I didn't explain my requirement properly. Please do ask me if you need any additional information. It would be really great if someone helps me out. </p>