title: xsl for-each within for-each-group group-by
body: <p>I am new to xsl. I understand that some similar questions have been asked, but I need help since I can't figure out what was wrong with my code. I have xml transactions file. I want to ,first, group by date, then sort each group of transactions by time</p>
  
  <p>I have this xml</p>
  
  <pre><code>&lt;History&gt;
      &lt;ShareLoanResults&gt;
          &lt;Account&gt;123&lt;/Account&gt;
          &lt;PostingDt&gt;20180718&lt;/PostingDt&gt;
          &lt;PostingTime&gt;215642&lt;/PostingTime&gt;
          &lt;AmtLoan&gt;000000000&lt;/AmtLoan&gt;
          &lt;AmtShare&gt;000000001&lt;/AmtShare&gt;
          &lt;AmtInt&gt;000000000&lt;/AmtInt&gt;
          &lt;Balance&gt;000000487&lt;/Balance&gt;
          &lt;Description /&gt;
      &lt;/ShareLoanResults&gt;
      &lt;ShareLoanResults&gt;
          &lt;Account&gt;123&lt;/Account&gt;
          &lt;PostingDt&gt;20180719&lt;/PostingDt&gt;
          &lt;PostingTime&gt;215650&lt;/PostingTime&gt;
          &lt;AmtLoan&gt;000000000&lt;/AmtLoan&gt;
          &lt;AmtShare&gt;000000003&lt;/AmtShare&gt;
          &lt;AmtInt&gt;000000000&lt;/AmtInt&gt;
          &lt;Balance&gt;000000494&lt;/Balance&gt;
          &lt;Description /&gt;
      &lt;/ShareLoanResults&gt;
      &lt;ShareLoanResults&gt;
          &lt;Account&gt;123&lt;/Account&gt;
          &lt;PostingDt&gt;20180719&lt;/PostingDt&gt;
          &lt;PostingTime&gt;215640&lt;/PostingTime&gt;
          &lt;AmtLoan&gt;000000000&lt;/AmtLoan&gt;
          &lt;AmtShare&gt;000000002&lt;/AmtShare&gt;
          &lt;AmtInt&gt;000000000&lt;/AmtInt&gt;
          &lt;Balance&gt;000000489&lt;/Balance&gt;
          &lt;Description /&gt;
      &lt;/ShareLoanResults&gt;
      &lt;ShareLoanResults&gt;
          &lt;Account&gt;123&lt;/Account&gt;
          &lt;PostingDt&gt;20180717&lt;/PostingDt&gt;
          &lt;PostingTime&gt;215641&lt;/PostingTime&gt;
          &lt;AmtLoan&gt;000000000&lt;/AmtLoan&gt;
          &lt;AmtShare&gt;000000004&lt;/AmtShare&gt;
          &lt;AmtInt&gt;000000000&lt;/AmtInt&gt;
          &lt;Balance&gt;000000486&lt;/Balance&gt;
          &lt;Description /&gt;
      &lt;/ShareLoanResults&gt;
  &lt;/History&gt;
  </code></pre>
  
  <p>I am looking for this result</p>
  
  <pre><code>&lt;History&gt;
  &lt;ShareLoanResults&gt;
      &lt;Order&gt;1&lt;/Order&gt;
      &lt;Account&gt;123&lt;/Account&gt;
      &lt;PostingDt&gt;20180717&lt;/PostingDt&gt;
      &lt;PostingTime&gt;215641&lt;/PostingTime&gt;
      &lt;Amount&gt;4&lt;/Amount&gt;
  &lt;/ShareLoanResults&gt;
  &lt;ShareLoanResults&gt;
      &lt;Order&gt;1&lt;/Order&gt;
      &lt;Account&gt;123&lt;/Account&gt;
      &lt;PostingDt&gt;20180718&lt;/PostingDt&gt;
      &lt;PostingTime&gt;215642&lt;/PostingTime&gt;
      &lt;Amount&gt;1&lt;/Amount&gt;
  &lt;/ShareLoanResults&gt;
  &lt;ShareLoanResults&gt;
      &lt;Order&gt;1&lt;/Order&gt;
      &lt;Account&gt;123&lt;/Account&gt;
      &lt;PostingDt&gt;20180719&lt;/PostingDt&gt;
      &lt;PostingTime&gt;215640&lt;/PostingTime&gt;
      &lt;Amount&gt;3&lt;/Amount&gt;
  &lt;/ShareLoanResults&gt;
  &lt;ShareLoanResults&gt;
      &lt;Order&gt;2&lt;/Order&gt;
      &lt;Account&gt;123&lt;/Account&gt;
      &lt;PostingDt&gt;20180719&lt;/PostingDt&gt;
      &lt;PostingTime&gt;215650&lt;/PostingTime&gt;
      &lt;Amount&gt;3&lt;/Amount&gt;
  &lt;/ShareLoanResults&gt;
  &lt;/History&gt;
  </code></pre>
  
  <p>Here is my xsl</p>
  
  <pre><code>&lt;xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'&gt;
  &lt;xsl:output method="xml" omit-xml-declaration="no" /&gt;
  
  &lt;xsl:template match="/"&gt;      
  &lt;xsl:copy&gt;
      &lt;xsl:for-each-group select="History/ShareLoanResults" group-by="PostingDt"&gt;
              &lt;xsl:for-each select="current-group()"&gt;
              &lt;xsl:sort select="PostingTime" order="ascending"/&gt;
                  &lt;ShareLoanResults&gt;
                      &lt;Order&gt;
                          &lt;xsl:value-of select="position()"/&gt;
                      &lt;/Order&gt;
                      &lt;Account&gt;
                          &lt;xsl:value-of select="Account"/&gt;
                      &lt;/Account&gt;
                      &lt;PostingDt&gt;
                          &lt;xsl:value-of select="PostingDt"/&gt;
                      &lt;/PostingDt&gt;    
                      &lt;PostingTime&gt;
                          &lt;xsl:value-of select="PostingTime"/&gt;
                      &lt;/PostingTime&gt;  
                      &lt;Amount&gt;
                          &lt;xsl:value-of select="format-number(AmtShare,'#.000000')"/&gt;
                      &lt;/Amount&gt;
                  &lt;/ShareLoanResults&gt;
              &lt;/xsl:for-each&gt;  
      &lt;/xsl:for-each-group&gt;
  &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>However, I am getting exception: javax.xml.transform.TransformerException: Could not find function: current-group</p>
  
  <p>If I remove the <code>&lt;xsl:for-each-group select="current-group()"&gt;</code> and its corresponding closed tag, I get empty result below</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;ShareLoanResults&gt;&lt;Order/&gt;&lt;Account/&gt;&lt;PostingDt/&gt;&lt;PostingTime/&gt;&lt;Amount&gt;NaN&lt;/Amount&gt;&lt;/ShareLoanResults&gt;
  </code></pre>
  
  <p>Could someone please tell me what I did wrong and/or was missing?
  Thanks!</p>