title: xslt compare two different nodes and then combine
body: <p>I have a requirement where i need to show the difference amount (<code>&lt;TotalAmount&gt;</code>) based on two different values which is in different section.The input xml as below.</p>
  
  <pre><code> &lt;TXLife xmlns="http://ACORD.org/Standards/Life/2"&gt;
      &lt;TXLifeRequest&gt;
          &lt;FundCode&gt;LTRW00&lt;/FundCode&gt;
          &lt;AccountNumber&gt;34142&lt;/AccountNumber&gt;
          &lt;ReversalInd&gt;Cr&lt;/ReversalInd&gt;
          &lt;TotalAmount&gt;1600&lt;/TotalAmount&gt;
      &lt;/TXLifeRequest&gt;
      &lt;TXLifeRequest&gt;
          &lt;FundCode&gt;LTRW00&lt;/FundCode&gt;
          &lt;AccountNumber&gt;34142&lt;/AccountNumber&gt;
          &lt;ReversalInd&gt;Dr&lt;/ReversalInd&gt;
          &lt;TotalAmount&gt;350&lt;/TotalAmount&gt;
      &lt;/TXLifeRequest&gt;
      &lt;TXLifeRequest&gt;
          &lt;FundCode&gt;LUL500&lt;/FundCode&gt;
          &lt;AccountNumber&gt;34142&lt;/AccountNumber&gt;
          &lt;ReversalInd&gt;Cr&lt;/ReversalInd&gt;
          &lt;TotalAmount&gt;500&lt;/TotalAmount&gt;
      &lt;/TXLifeRequest&gt;
      &lt;TXLifeRequest&gt;
          &lt;FundCode&gt;LUL500&lt;/FundCode&gt;
          &lt;AccountNumber&gt;34142&lt;/AccountNumber&gt;
          &lt;ReversalInd&gt;Dr&lt;/ReversalInd&gt;
          &lt;TotalAmount&gt;800&lt;/TotalAmount&gt;
      &lt;/TXLifeRequest&gt;    
  &lt;/TXLife&gt;
  </code></pre>
  
  <p>From the above xml the criteria to find difference amount is <code>&lt;FundCode&gt;</code> and <code>&lt;AccountNumber&gt;</code>.
   If any sections having same <code>&lt;FundCode&gt;</code> and <code>&lt;AccountNumber&gt;</code> retrieve <code>&lt;TotalAmount&gt;</code> and find the difference.</p>
  
  <p>for example from above xml:- </p>
  
  <p>There are two sections with the same <code>&lt;fundcode&gt;</code> and <code>&lt;Accountnumber&gt;</code> with LTRW00 and 34142.
  Now the difference of <code>&lt;TotalAmount&gt;</code> is 1250 (1600 - 250). I need to repeat this logic on other sections also.</p>
  
  <p>So the final output xml should like this:</p>
  
  <pre><code> &lt;TXLife xmlns="http://ACORD.org/Standards/Life/2"&gt;
          &lt;TXLifeRequest&gt;
              &lt;FundCode&gt;LTRW00&lt;/FundCode&gt;
              &lt;AccountNumber&gt;34142&lt;/AccountNumber&gt;
              &lt;ReversalInd&gt;Cr&lt;/ReversalInd&gt;
              &lt;TotalAmount&gt;1250&lt;/TotalAmount&gt;
          &lt;/TXLifeRequest&gt;
          &lt;TXLifeRequest&gt;
              &lt;FundCode&gt;LUL500&lt;/FundCode&gt;
              &lt;AccountNumber&gt;34142&lt;/AccountNumber&gt;
              &lt;ReversalInd&gt;Dr&lt;/ReversalInd&gt;
              &lt;TotalAmount&gt;300&lt;/TotalAmount&gt;
          &lt;/TXLifeRequest&gt;    
      &lt;/TXLife&gt;
  </code></pre>
  
  <p>And also if you observe the <code>&lt;ReversalInd&gt;</code> that is CR/DR should be identified based on highest TotalAmount value.</p>
  
  <p>I have applied the xslt below but no output. Any ideas how to implement in xslt 1.0. Greatly appreciated.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0" xmlns:ns="http://ACORD.org/Standards/Life/2"&gt;
      &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt;
      &lt;xsl:strip-space elements="*"/&gt;
  
      &lt;xsl:template match="node()|@*"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="node()|@*"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="/ns:TXLife/ns:TXLifeRequest"&gt;
          &lt;xsl:element name="TXLife" namespace="http://ACORD.org/Standards/Life/2"&gt;
             &lt;xsl:call-template name="balance"&gt;
                 &lt;xsl:with-param name="total" select="ns:TotalAmount"&gt;&lt;/xsl:with-param&gt;
             &lt;/xsl:call-template&gt;
              &lt;xsl:copy-of select="."/&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template name="balance"&gt;
          &lt;xsl:param name="total"&gt;&lt;/xsl:param&gt;
          &lt;xsl:variable name="reminder" select="0"&gt;&lt;/xsl:variable&gt;        
          &lt;xsl:variable name="val1"&gt;
          &lt;xsl:value-of select="$total[1]"/&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:variable name="val2"&gt;
          &lt;xsl:value-of select="$total[position() &amp;gt; 1]"/&gt;
          &lt;/xsl:variable&gt;
  
          &lt;xsl:if test="$val1 &amp;gt; $val2"&gt;
              &lt;remainingAmount&gt;&lt;xsl:value-of select="$val1 - $val2"/&gt;&lt;/remainingAmount&gt;
          &lt;/xsl:if&gt;
  
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>