title: How to use xsl group-by for multiple element values
body: <p>I'm working with the below xml structure:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;Report_Data&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;10513&lt;/ID&gt;
              &lt;Code&gt;XXX&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;100&lt;/Amount&gt;
          &lt;/Report_Entry&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;10513&lt;/ID&gt;
              &lt;Code&gt;XXX&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;-25&lt;/Amount&gt;
          &lt;/Report_Entry&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;12076&lt;/ID&gt;
              &lt;Code&gt;HHH&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;100&lt;/Amount&gt;
          &lt;/Report_Entry&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;12166&lt;/ID&gt;
              &lt;Code&gt;HHH&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;5&lt;/Amount&gt;
          &lt;/Report_Entry&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;12166&lt;/ID&gt;
              &lt;Code&gt;HHH&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;35&lt;/Amount&gt;
          &lt;/Report_Entry&gt;
      &lt;/Report_Data&gt;
  </code></pre>
  
  <p>I need to group by ID and Code only when the Code is 'XXX' and sum the Amount. So in the end, my final output should look like the following:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;Report_Data&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;10513&lt;/ID&gt;
              &lt;Code&gt;XXX&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;75&lt;/Amount&gt;
          &lt;/Report_Entry&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;12076&lt;/ID&gt;
              &lt;Code&gt;HHH&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;100&lt;/Amount&gt;
          &lt;/Report_Entry&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;12166&lt;/ID&gt;
              &lt;Code&gt;HHH&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;5&lt;/Amount&gt;
          &lt;/Report_Entry&gt;
          &lt;Report_Entry&gt;
              &lt;ID&gt;12166&lt;/ID&gt;
              &lt;Code&gt;HHH&lt;/Code&gt;
              &lt;Payment_Date&gt;2016-05-05-07:00&lt;/Payment_Date&gt;
              &lt;Amount&gt;35&lt;/Amount&gt;
      &lt;/Report_Entry&gt;
  </code></pre>
  
  <p></p>
  
  <p>I know how i can group by multiple elements but what I can't figure out is how to condition the group by by an element value. I don't foray into xsl very much so my knowledge is a bit lacking. I have tried using the below xsl (latest version anyways):</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          exclude-result-prefixes="xs"
          version="2.0"&gt;
  
      &lt;xsl:template match="Report_Data"&gt;
          &lt;Report_Data&gt;
              &lt;xsl:for-each-group select="Report_Entry" group-by="concat(ID,' ',Code)"&gt;
                  &lt;xsl:copy&gt;
                      &lt;xsl:copy-of select="ID"/&gt;
                      &lt;xsl:copy-of select="Code"/&gt;
                      &lt;xsl:copy-of select="Payment_Date"/&gt;
                      &lt;Amount&gt;&lt;xsl:value-of select="sum(current-group()/Amount[. &amp;gt; 0] - sum(current-group()/Amount[. &amp;lt; 0]))"/&gt;&lt;/Amount&gt;
                  &lt;/xsl:copy&gt;
              &lt;/xsl:for-each-group&gt;
          &lt;/Report_Data&gt;
      &lt;/xsl:template&gt;
     &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Another issue I'm having is that I don't have the sum logic quite right but that is probably a different post.</p>
  
  <p>Any kind of help or guidance would be much appreciated. </p>