title: dynamic rows-number-spanned in xsl fo:table based on column values
body: <p>Hi i am trying to group cell based on values in <a href="https://i.stack.imgur.com/Sw4mz.png" rel="nofollow noreferrer">column two</a>,
  Using this to make PDF creation using apache FOP</p>
  
  <p>My code is</p>
  
  <p><strong>document.xml</strong></p>
  
  <pre><code>    &lt;data&gt;
      &lt;data-body&gt;
          &lt;table-header&gt;
              &lt;column-one&gt;Column One&lt;/column-one&gt;
              &lt;column-two&gt;Column Two&lt;/column-two&gt;
              &lt;column-three&gt;Column Three&lt;/column-three&gt;
              &lt;column-four&gt;Column Four&lt;/column-four&gt;
          &lt;/table-header&gt;
          &lt;table-data&gt;
              &lt;column-one&gt;One&lt;/column-one&gt;
              &lt;column-two&gt;5000&lt;/column-two&gt;
              &lt;column-three&gt;Three&lt;/column-three&gt;
              &lt;column-four&gt;Four&lt;/column-four&gt;
          &lt;/table-data&gt;
          &lt;table-data&gt;
              &lt;column-one&gt;One&lt;/column-one&gt;
              &lt;column-two&gt;5000&lt;/column-two&gt;
              &lt;column-three&gt;Three&lt;/column-three&gt;
              &lt;column-four&gt;Four&lt;/column-four&gt;
          &lt;/table-data&gt;
          &lt;table-data&gt;
              &lt;column-one&gt;One&lt;/column-one&gt;
              &lt;column-two&gt;1200&lt;/column-two&gt;
              &lt;column-three&gt;Three&lt;/column-three&gt;
              &lt;column-four&gt;Four&lt;/column-four&gt;
          &lt;/table-data&gt;
          &lt;table-data&gt;
              &lt;column-one&gt;One&lt;/column-one&gt;
              &lt;column-two&gt;2000&lt;/column-two&gt;
              &lt;column-three&gt;Three&lt;/column-three&gt;
              &lt;column-four&gt;Four&lt;/column-four&gt;
          &lt;/table-data&gt;
          &lt;table-data&gt;
              &lt;column-one&gt;One&lt;/column-one&gt;
              &lt;column-two&gt;2000&lt;/column-two&gt;
              &lt;column-three&gt;Three&lt;/column-three&gt;
              &lt;column-four&gt;Four&lt;/column-four&gt;
          &lt;/table-data&gt;
           &lt;/data-body&gt;
      &lt;/data&gt;
  </code></pre>
  
  <p><strong>template.xsl</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;xsl:stylesheet version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:fo="http://www.w3.org/1999/XSL/Format"&gt;
          &lt;xsl:template match="data"&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="simple"
                          page-height="8.5in" page-width="11in" margin-top=".5in"
                          margin-bottom=".5in" margin-left=".5in" margin-right=".5in"&gt;
                          &lt;fo:region-body margin-top="2cm" margin-bottom="2cm" /&gt;
                          &lt;fo:region-before extent="2cm" overflow="hidden" /&gt;
                          &lt;fo:region-after extent="1cm" overflow="hidden" /&gt;
                      &lt;/fo:simple-page-master&gt;
                  &lt;/fo:layout-master-set&gt;
                  &lt;fo:page-sequence master-reference="simple"
                      initial-page-number="1"&gt;
                      &lt;fo:static-content flow-name="xsl-region-before"&gt;
                          &lt;fo:block font-size="13.0pt" font-family="serif"
                              padding-after="2.0pt" space-before="4.0pt" text-align="center"
                              border-bottom-style="solid" border-bottom-width="1.0pt"&gt;
                              &lt;xsl:text&gt;PDF Test&lt;/xsl:text&gt;
                          &lt;/fo:block&gt;
                      &lt;/fo:static-content&gt;
                      &lt;fo:static-content flow-name="xsl-region-after"&gt;
                          &lt;fo:block font-size="12.0pt" font-family="sans-serif"
                              padding-after="2.0pt" space-before="2.0pt" text-align="center"
                              border-top-style="solid" border-bottom-width="1.0pt"&gt;
                              &lt;xsl:text&gt;Page&lt;/xsl:text&gt;
                              &lt;fo:page-number /&gt;
                          &lt;/fo:block&gt;
                      &lt;/fo:static-content&gt;
                      &lt;fo:flow flow-name="xsl-region-body"&gt;
                          &lt;xsl:apply-templates select="data-body" /&gt;
                      &lt;/fo:flow&gt;
                  &lt;/fo:page-sequence&gt;
              &lt;/fo:root&gt;
          &lt;/xsl:template&gt;
          &lt;xsl:template match="data-body"&gt;
              &lt;fo:block text-align="center"&gt;
                  &lt;fo:table table-layout="fixed" width="100%"
                      border-style="dashed"&gt;
                      &lt;fo:table-column border-style="solid" /&gt;
                      &lt;fo:table-column border-style="solid" /&gt;
                      &lt;fo:table-column border-style="solid" /&gt;
                      &lt;fo:table-header&gt;
                          &lt;xsl:apply-templates select="table-header" /&gt;
                      &lt;/fo:table-header&gt;
                      &lt;fo:table-body&gt;
                          &lt;xsl:apply-templates select="table-data" /&gt;
                      &lt;/fo:table-body&gt;
                  &lt;/fo:table&gt;
              &lt;/fo:block&gt;
          &lt;/xsl:template&gt;
          &lt;xsl:template match="table-header"&gt;
              &lt;fo:table-row keep-together.within-page="always"
                  border-style="solid"&gt;
                  &lt;fo:table-cell&gt;
                      &lt;fo:block font-size="10pt" font-family="sans-serif"
                          padding-top="3pt"&gt;
                          &lt;xsl:value-of select="column-one"&gt;&lt;/xsl:value-of&gt;
                      &lt;/fo:block&gt;
                  &lt;/fo:table-cell&gt;
                  &lt;fo:table-cell&gt;
                      &lt;fo:block font-size="10pt" font-family="sans-serif"
                          padding-top="3pt"&gt;
                          &lt;xsl:value-of select="column-two"&gt;&lt;/xsl:value-of&gt;
                      &lt;/fo:block&gt;
                  &lt;/fo:table-cell&gt;
                  &lt;fo:table-cell&gt;
                      &lt;fo:block font-size="10pt" font-family="sans-serif"
                          padding-top="3pt"&gt;
                          &lt;xsl:value-of select="column-three"&gt;&lt;/xsl:value-of&gt;
                      &lt;/fo:block&gt;
                  &lt;/fo:table-cell&gt;
              &lt;/fo:table-row&gt;
          &lt;/xsl:template&gt;
          &lt;xsl:template match="table-data"&gt;
              &lt;fo:table-row keep-together.within-page="always"
                  border-style="solid"&gt;
                  &lt;fo:table-cell&gt;
                      &lt;fo:block font-size="10pt" font-family="sans-serif"
                          padding-top="3pt"&gt;
                          &lt;xsl:value-of select="column-one"&gt;&lt;/xsl:value-of&gt;
                      &lt;/fo:block&gt;
                  &lt;/fo:table-cell&gt;
                  &lt;fo:table-cell&gt;
                      &lt;fo:block font-size="10pt" font-family="sans-serif"
                          padding-top="3pt"&gt;
                          &lt;xsl:value-of select="column-two"&gt;&lt;/xsl:value-of&gt;
                      &lt;/fo:block&gt;
                  &lt;/fo:table-cell&gt;
                  &lt;fo:table-cell&gt;
                      &lt;fo:block font-size="10pt" font-family="sans-serif"
                          padding-top="3pt"&gt;
                          &lt;xsl:value-of select="column-three"&gt;&lt;/xsl:value-of&gt;
                      &lt;/fo:block&gt;
                  &lt;/fo:table-cell&gt;
              &lt;/fo:table-row&gt;
          &lt;/xsl:template&gt;
      &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong><em>GeneratePDF.java</em></strong></p>
  
  <pre class="lang-java prettyprint-override"><code>    package com.test.pdf;
      import java.io.File;
      import java.io.OutputStream;
      import javax.xml.transform.Result;
      import javax.xml.transform.Source;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.sax.SAXResult;
      import javax.xml.transform.stream.StreamSource;
      import org.apache.fop.apps.FOUserAgent;
      import org.apache.fop.apps.Fop;
      import org.apache.fop.apps.FopFactory;
      import org.apache.fop.apps.MimeConstants;
      public class GeneratePDF {
          public static void main(String[] args) {
              try {
                  System.out.println("FOP ExampleXML2PDF\n");
                  System.out.println("Preparing...");
                  // Setup input and output files
                  File xmlfile = new File("src/", "document.xml");
                  File xsltfile = new File("src/", "template.xsl");
                  File pdffile = new File("C:\\Users\\Punithan Antony\\Downloads\\pdf", "ResultPDF.pdf");
                  System.out.println("Input: XML (" + xmlfile + ")");
                  System.out.println("Stylesheet: " + xsltfile);
                  System.out.println("Output: PDF (" + pdffile + ")");
                  System.out.println();
                  System.out.println("Transforming...");
                  // configure fopFactory as desired
                  FopFactory fopFactory = FopFactory.newInstance(new File("src/fop.xconf"));
                  //new File("src/fop.xconf");
                  FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
                  // configure foUserAgent as desired
                  // Setup output
                  OutputStream out = new java.io.FileOutputStream(pdffile);
                  out = new java.io.BufferedOutputStream(out);
                  try {
                      // Construct fop with desired output format
                      Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
                              foUserAgent, out);
                      // Setup XSLT
                      TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
                      Transformer transformer = factory
                              .newTransformer(new StreamSource(xsltfile));
                      // Set the value of a &lt;param&gt; in the stylesheet
                      // Setup input for XSLT transformation
                      Source src = new StreamSource(xmlfile);
                      // Resulting SAX events (the generated FO) must be piped through
                      // to FOP
                      Result res = new SAXResult(fop.getDefaultHandler());
                      // Start XSLT transformation and FOP processing
                      transformer.transform(src, res);
                  } finally {
                      out.close();
                  }
                  System.out.println("Success!");
              } catch (Exception e) {
                  System.out.print("ERROR!!");
                  e.printStackTrace(System.err);
                  System.exit(-1);
              }
          }
      }
  </code></pre>
  
  <p>for a particular column i need to merge repeated values in order if 5000 is repeated in a same column 
  i need to merge 5000 in a single cell dynamically based on value</p>
  
  <p>I also tried with ** xsl:for-each-group** <strong>group-adjacent="column-two"</strong> , <strong>column-grouping-key()</strong>
  but it does not group like what i expected,
  Please help, Thanks in advance.</p>