title: How to split table after certain number of columns
body: <p>I have a little issue that I've been struggling with for a couple of days now.</p>
  
  <p>Here is input xml:</p>
  
  <pre><code>&lt;Facture&gt;
      &lt;Mensualite&gt;
          &lt;Lines&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;1&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2015&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20150618&lt;/DateEcheance&gt;
                  &lt;Montant&gt;163.14&lt;/Montant&gt;
              &lt;/Line&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;2&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2015&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20150718&lt;/DateEcheance&gt;
                  &lt;Montant&gt;120&lt;/Montant&gt;
              &lt;/Line&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;3&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2015&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20150818&lt;/DateEcheance&gt;
                  &lt;Montant&gt;120&lt;/Montant&gt;
              &lt;/Line&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;4&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2015&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20150918&lt;/DateEcheance&gt;
                  &lt;Montant&gt;120&lt;/Montant&gt;
              &lt;/Line&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;5&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2016&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20151018&lt;/DateEcheance&gt;
                  &lt;Montant&gt;120&lt;/Montant&gt;
              &lt;/Line&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;6&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2016&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20151118&lt;/DateEcheance&gt;
                  &lt;Montant&gt;120&lt;/Montant&gt;
              &lt;/Line&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;6&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2016&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20151118&lt;/DateEcheance&gt;
                  &lt;Montant&gt;120&lt;/Montant&gt;
              &lt;/Line&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;6&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2017&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20151113&lt;/DateEcheance&gt;
                  &lt;Montant&gt;122&lt;/Montant&gt;
              &lt;/Line&gt;
              &lt;Line&gt;
                  &lt;NumeroMensualite&gt;6&lt;/NumeroMensualite&gt;
                  &lt;AnneeEcheance&gt;2017&lt;/AnneeEcheance&gt;
                  &lt;DateEcheance&gt;20151112&lt;/DateEcheance&gt;
                  &lt;Montant&gt;124&lt;/Montant&gt;
              &lt;/Line&gt;
          &lt;/Lines&gt;
      &lt;/Mensualite&gt;
  &lt;/Facture&gt;
  </code></pre>
  
  <p>And here is the xslfo that i've created:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:set="http://exslt.org/sets" xmlns:exsl="http://exslt.org/common" xmlns:java="http://xml.apache.org/xalan/java"&gt;
  
  
  
  &lt;xsl:key name="years" match="Mensualite/Lines/Line" use="AnneeEcheance" /&gt;
  
  
  &lt;!-- Start --&gt;
  &lt;xsl:template match="/"&gt;
      &lt;xsl:apply-templates select="exsl:node-set(document(/*/Document))/Facture"/&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="Facture"&gt;   
      &lt;fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"&gt;
          &lt;!--Page definitions--&gt;
          &lt;fo:layout-master-set&gt;
              &lt;fo:simple-page-master master-name="firstPageMaster" page-height="29.7cm" page-width="21cm" margin-top="7mm" margin-bottom="8mm" margin-left="8mm" margin-right="0mm"&gt;
                  &lt;fo:region-body margin-top="23mm" extent="297mm" margin-left="8mm" margin-right="8mm" region-name="body"/&gt;
                  &lt;fo:region-before extent="23mm" region-name="header1"/&gt;
              &lt;/fo:simple-page-master&gt;
              &lt;fo:simple-page-master master-name="secondPageMaster" page-height="29.7cm" page-width="21cm" margin-top="7mm" margin-bottom="8mm" margin-left="8mm" margin-right="0mm"&gt;
                  &lt;fo:region-body margin-top="26mm" margin-bottom="95mm" extent="255mm" margin-left="8mm" margin-right="8mm" region-name="body"/&gt;
                  &lt;fo:region-before extent="23mm" region-name="header"/&gt;
              &lt;/fo:simple-page-master&gt;
              &lt;fo:simple-page-master master-name="nextPagesMaster" page-height="29.7cm" page-width="21cm" margin-top="7mm" margin-bottom="8mm" margin-left="8mm" margin-right="0mm"&gt;
                  &lt;fo:region-body margin-top="26mm" extent="255mm" margin-left="8mm" margin-right="8mm" region-name="body"/&gt;
                  &lt;fo:region-before extent="23mm" region-name="header"/&gt;
              &lt;/fo:simple-page-master&gt;
              &lt;fo:page-sequence-master master-name="firstSecondAndRestPages"&gt;
                          &lt;fo:single-page-master-reference master-reference="firstPageMaster"/&gt;
                          &lt;fo:repeatable-page-master-reference master-reference="nextPagesMaster" maximum-repeats="no-limit"/&gt;
              &lt;/fo:page-sequence-master&gt;
          &lt;/fo:layout-master-set&gt;
  
          &lt;fo:page-sequence master-reference="firstSecondAndRestPages" font-family="Neo Sans Std" font-size="8.5pt" color="&amp;darkGreen;" force-page-count="no-force" initial-page-number="1"&gt;
              &lt;fo:flow flow-name="body"&gt;
              &lt;fo:block-container position="fixed" left="10mm" top="86mm" height="30mm" width="186mm" font-size="9pt" color="black" display-align="center"&gt;
                  &lt;fo:table table-layout="fixed" font-size="9pt" width="186mm" space-before="3mm" space-after="1mm"&gt;
                  &lt;fo:table-column column-width="30mm" /&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 0"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 1"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 2"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 3"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 4"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 5"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 6"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 7"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 8"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 9"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 10"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                      &lt;xsl:if test="count(Mensualite/Lines/Line) &gt; 11"&gt;&lt;fo:table-column column-width="10mm"/&gt;&lt;/xsl:if&gt;
                  &lt;fo:table-body&gt;
                  &lt;fo:table-row font-weight="600" text-align="center"&gt;
                      &lt;fo:table-cell border-bottom="thin solid &amp;darkGreen;" border-right="thin solid &amp;darkGreen;"&gt;&lt;fo:block&gt;&amp;nbsp;&lt;/fo:block&gt;&lt;/fo:table-cell&gt; 
                      &lt;xsl:for-each select="Mensualite/Lines/Line[generate-id(.)=generate-id(key('years', AnneeEcheance)[1] ) ]"&gt;
                      &lt;fo:table-cell border-top="thin solid &amp;darkGreen;" border-right="thin solid &amp;darkGreen;" number-columns-spanned="{count(key('years', AnneeEcheance))}"&gt;&lt;fo:block&gt;&lt;xsl:value-of select="AnneeEcheance"/&gt;&lt;/fo:block&gt;&lt;/fo:table-cell&gt;
                      &lt;/xsl:for-each&gt;
                  &lt;/fo:table-row&gt;
                  &lt;fo:table-row border-bottom="thin solid &amp;darkGreen;" border-top="thin solid &amp;darkGreen;" border-left="thin solid &amp;darkGreen;" border-right="thin solid &amp;darkGreen;"&gt;
                  &lt;fo:table-cell border-right="thin solid &amp;darkGreen;" font-weight="600"&gt;&lt;fo:block&gt;&amp;nbsp;Mensualité&lt;/fo:block&gt;&lt;/fo:table-cell&gt;
                  &lt;xsl:for-each select="key('years', Mensualite/Lines/Line/AnneeEcheance)"&gt;
                  &lt;fo:table-cell border-right="thin solid &amp;darkGreen;"&gt;&lt;fo:block&gt;&lt;xsl:value-of select="NumeroMensualite"/&gt;&lt;/fo:block&gt;&lt;/fo:table-cell&gt;
                  &lt;/xsl:for-each&gt;
                  &lt;/fo:table-row&gt;
                  &lt;fo:table-row border-bottom="thin solid &amp;darkGreen;" border-top="thin solid &amp;darkGreen;" border-left="thin solid &amp;darkGreen;" border-right="thin solid &amp;darkGreen;"&gt;
                  &lt;fo:table-cell border-right="thin solid &amp;darkGreen;" font-weight="600"&gt;&lt;fo:block&gt;&amp;nbsp;Échéance&lt;/fo:block&gt;&lt;/fo:table-cell&gt;
                  &lt;xsl:for-each select="key('years', Mensualite/Lines/Line/AnneeEcheance)"&gt;
                  &lt;fo:table-cell border-right="thin solid &amp;darkGreen;"&gt;&lt;fo:block&gt;&lt;xsl:value-of select="concat(substring(DateEcheance,7,2),'/',substring(DateEcheance,5,2))"/&gt;&lt;/fo:block&gt;&lt;/fo:table-cell&gt;
                  &lt;/xsl:for-each&gt;
                  &lt;/fo:table-row&gt;
                  &lt;fo:table-row border-bottom="thin solid &amp;darkGreen;" border-top="thin solid &amp;darkGreen;" border-left="thin solid &amp;darkGreen;" border-right="thin solid &amp;darkGreen;"&gt;
                  &lt;fo:table-cell border-right="thin solid &amp;darkGreen;" font-weight="600"&gt;&lt;fo:block&gt;&amp;nbsp;Montant (€)&lt;/fo:block&gt;&lt;/fo:table-cell&gt;
                  &lt;xsl:for-each select="key('years', Mensualite/Lines/Line/AnneeEcheance)"&gt;
                  &lt;fo:table-cell border-right="thin solid &amp;darkGreen;"&gt;&lt;fo:block&gt;&lt;xsl:value-of select="Montant"/&gt;&lt;/fo:block&gt;&lt;/fo:table-cell&gt;
                  &lt;/xsl:for-each&gt;
                  &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
                  &lt;/fo:table&gt;
                  &lt;fo:block/&gt;
              &lt;/fo:block-container&gt;
              &lt;/fo:flow&gt;
          &lt;/fo:page-sequence&gt;
      &lt;/fo:root&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <p>I need to split the table if the xml contains more then 12 Lines (columns in that case) and make a new table after.
  I'm a bit stuck on it and it's really frustrating. Does anyone have any pointers?</p>