title: using for-each-group technique in xsl-fo
body: <p>can I use for-each-group if yes then can someone show me an example. I am trying to generate a pdf using xsl-fo</p>
  
  <p>I am trying to output it using a table. Please show me an example which makes use of grouping technique and adding values. However, the output should be displayed in a table.</p>
  
  <p>xml file:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Library&gt;
  &lt;Book code="123"&gt;
  &lt;BookName&gt;XML&lt;/BookName&gt;
  &lt;Category&gt;Programming&lt;/Category&gt;
  &lt;Quantity&gt;10&lt;/Quantity&gt;
  &lt;Price&gt;100&lt;/Price&gt;
  &lt;/Book&gt;
  &lt;Book code="345"&gt;
  &lt;BookName&gt;Photoshop&lt;/BookName&gt;
  &lt;Category&gt;Design&lt;/Category&gt;
  &lt;Quantity&gt;50&lt;/Quantity&gt;
  &lt;Price&gt;200&lt;/Price&gt;
  &lt;/Book&gt;
  &lt;Book code="123"&gt;
  &lt;BookName&gt;XML&lt;/BookName&gt;
  &lt;Category&gt;Programming&lt;/Category&gt;
  &lt;Quantity&gt;5&lt;/Quantity&gt;
  &lt;Price&gt;100&lt;/Price&gt;
  &lt;/Book&gt;
  &lt;Book code="345"&gt;
  &lt;BookName&gt;Photoshop&lt;/BookName&gt;
  &lt;Category&gt;Design&lt;/Category&gt;
  &lt;Quantity&gt;10&lt;/Quantity&gt;
  &lt;Price&gt;200&lt;/Price&gt;
  &lt;/Book&gt;
    &lt;Book code="456"&gt;
  &lt;BookName&gt;Illustrator&lt;/BookName&gt;
  &lt;Category&gt;Design&lt;/Category&gt;
  &lt;Quantity&gt;100&lt;/Quantity&gt;
  &lt;Price&gt;300&lt;/Price&gt;
  &lt;/Book&gt;    
  &lt;/Library&gt;
  </code></pre>
  
  <p><strong>myxsl-fo</strong></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:fo="http://www.w3.org/1999/XSL/Format" version="1.0"&gt;
  &lt;xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/&gt;
  
  &lt;xsl:template match="Library"&gt;
      &lt;fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"&gt;
          &lt;fo:layout-master-set&gt;
              &lt;fo:simple-page-master master-name="A4-landscape" page-height="300mm" page-width="150mm" margin="1in"&gt;
                  &lt;fo:region-body margin="1in"/&gt;
              &lt;/fo:simple-page-master&gt;
          &lt;/fo:layout-master-set&gt;
  
          &lt;fo:page-sequence master-reference="A4-landscape"&gt;
              &lt;fo:flow flow-name="xsl-region-body"&gt;
                  &lt;fo:table border-top-style="solid" border-top-width="thick"&gt;
                      &lt;fo:table-body font-size="12pt" font-family="times new roman"&gt;
  
  
                          &lt;fo:table-row border-bottom-style="solid" border-bottom-color="#000" border-bottom-width="thick"&gt;
                              &lt;fo:table-cell padding-top="1mm" padding-bottom="1mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;Category&lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;
                              &lt;fo:table-cell padding-top="1mm" padding-bottom="1mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;Book Code&lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;
                              &lt;fo:table-cell padding-top="1mm" padding-bottom="1mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;Quantity&lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;
                              &lt;fo:table-cell padding-top="1mm" padding-bottom="1mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;Price&lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;
                              &lt;fo:table-cell padding-top="1mm" padding-bottom="1mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;Total&lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;                                
                          &lt;/fo:table-row&gt;
  
                          &lt;xsl:for-each select="Book"&gt;
                              &lt;xsl:sort select="Category"/&gt;
                              &lt;xsl:sort select="@code"/&gt;
                              &lt;fo:table-row&gt;
                                  &lt;fo:table-cell padding-top="3mm" padding-bottom="3mm"&gt;
                                      &lt;fo:block font-weight="bold"&gt;
                                          &lt;xsl:value-of select="Category"/&gt;
                                      &lt;/fo:block&gt;
                                  &lt;/fo:table-cell&gt;
                              &lt;fo:table-cell padding-top="3mm" padding-bottom="3mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;
                                      &lt;xsl:value-of select="@code"/&gt;
                                  &lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;
                              &lt;fo:table-cell padding-top="3mm" padding-bottom="3mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;
                                      &lt;xsl:value-of select="Quantity"/&gt;
                                  &lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;
                              &lt;fo:table-cell padding-top="3mm" padding-bottom="3mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;
                                      &lt;xsl:value-of select="Price"/&gt;
                                  &lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;
                              &lt;fo:table-cell padding-top="3mm" padding-bottom="3mm"&gt;
                                  &lt;fo:block font-weight="bold"&gt;
                                      &lt;xsl:value-of select="number(Quantity)*number(Price)"/&gt;
                                  &lt;/fo:block&gt;
                              &lt;/fo:table-cell&gt;
                              &lt;/fo:table-row&gt;                                         
                          &lt;/xsl:for-each&gt;
                      &lt;/fo:table-body&gt;
                  &lt;/fo:table&gt;
              &lt;/fo:flow&gt;
          &lt;/fo:page-sequence&gt;
      &lt;/fo:root&gt;
      &lt;/xsl:template&gt;
      &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Instead of xsl:for-each if I try to use xsl:for-each-group then it throws an error saying that the xsl:for-each-group cannot be in that location.</p>
  
  <p><strong>my current output:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/vdjpL.jpg" alt="enter image description here"></p>
  
  <p>however the output I want is shown in the image below:
  <img src="https://i.stack.imgur.com/mvFm3.jpg" alt="enter image description here"></p>
  
  <p>thanks</p>