title: How to do sum based on 2 elements using XSLT
body: <p>I want to do summing of population per Timestamp &amp; Reasoncode. It wasn't working, given the sample code below. Could you pls help me in making it working</p>
  
  <p>My XML: </p>
  
  <p>I have used the following XML as a Input file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Earnings&gt;
      &lt;Collection&gt;
          &lt;Timestamp&gt;20170101&lt;/Timestamp&gt;
          &lt;Points&gt;100&lt;/Points&gt;
          &lt;ReasonCode&gt;AN&lt;/ReasonCode&gt;
      &lt;/Collection&gt;
      &lt;Collection&gt;
          &lt;Timestamp&gt;20170102&lt;/Timestamp&gt;
          &lt;Points&gt;100&lt;/Points&gt;
          &lt;ReasonCode&gt;AN&lt;/ReasonCode&gt;
      &lt;/Collection&gt;
      &lt;Collection&gt;
          &lt;Timestamp&gt;20170101&lt;/Timestamp&gt;
          &lt;Points&gt;10000&lt;/Points&gt;
          &lt;ReasonCode&gt;BP&lt;/ReasonCode&gt;
      &lt;/Collection&gt;
      &lt;Collection&gt;
          &lt;Timestamp&gt;20170101&lt;/Timestamp&gt;
          &lt;Points&gt;10000&lt;/Points&gt;
          &lt;ReasonCode&gt;BP&lt;/ReasonCode&gt;
      &lt;/Collection&gt;
      &lt;Collection&gt;
          &lt;Timestamp&gt;20170102&lt;/Timestamp&gt;
          &lt;Points&gt;100&lt;/Points&gt;
          &lt;ReasonCode&gt;PPTS&lt;/ReasonCode&gt;
      &lt;/Collection&gt;
  &lt;/Earnings&gt;
  </code></pre>
  
  <p>My XSLT</p>
  
  <p>I have used the following XSLT used to convert my XML to designated format</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-16"?&gt;
  &lt;xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
      &lt;xsl:key name="Earnings" match="Earnings/Collection" use="concat(Timestamp, '+',ReasonCode)"/&gt;
      &lt;xsl:template match="Earnings"&gt;
          &lt;Earnings&gt;
              &lt;xsl:for-each select="Collection[generate-id() = generate-id(key('Earnings', concat(Timestamp, '+',ReasonCode))[1])]"&gt;
                  &lt;xsl:sort select="concat(Timestamp, '+',ReasonCode)"/&gt;
                  &lt;xsl:variable name="current-group" select="/Earnings/Collection[ReasonCode = current()/ReasonCode]"/&gt;
                  &lt;Collection&gt;
                      &lt;Timestamp&gt;
                          &lt;xsl:value-of select="Timestamp"/&gt;
                      &lt;/Timestamp&gt;
                      &lt;Points&gt;
                          &lt;xsl:value-of select="sum($current-group/Points)"/&gt;
                      &lt;/Points&gt;
                      &lt;ReasonCode&gt;
                          &lt;xsl:value-of select="ReasonCode"/&gt;
                      &lt;/ReasonCode&gt;
                  &lt;/Collection&gt;
              &lt;/xsl:for-each&gt;
          &lt;/Earnings&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Expected Output</p>
  
  <pre><code>&lt;Collection&gt;
      &lt;Timestamp&gt;20170101&lt;/Timestamp&gt;
      &lt;Points&gt;100&lt;/Points&gt;
      &lt;ReasonCode&gt;AN&lt;/ReasonCode&gt;
  &lt;/Collection&gt;
  &lt;Collection&gt;
      &lt;Timestamp&gt;20170101&lt;/Timestamp&gt;
      &lt;Points&gt;2000&lt;/Points&gt;
      &lt;ReasonCode&gt;BP&lt;/ReasonCode&gt;
  &lt;/Collection&gt;
  &lt;Collection&gt;
      &lt;Timestamp&gt;20170102&lt;/Timestamp&gt;
      &lt;Points&gt;100&lt;/Points&gt;
      &lt;ReasonCode&gt;AN&lt;/ReasonCode&gt;
  &lt;/Collection&gt;
  &lt;Collection&gt;
      &lt;Timestamp&gt;20170102&lt;/Timestamp&gt;
      &lt;Points&gt;100&lt;/Points&gt;
      &lt;ReasonCode&gt;PPTS&lt;/ReasonCode&gt;
  &lt;/Collection&gt;
  </code></pre>
  
  <p>Thanks,<br>
  Mohan</p>