title: Apache fop, XSL FO: Occasionally adds empty page at the end of flow in page sequence (not the last one)
body: <p>Using apache-fop 1.0  also tried 2.1 with the same behaviour.</p>
  
  <p>My Questions:</p>
  
  <ul>
  <li>Why is the empty page generated? </li>
  <li>How can the page numbering be as
  weird as Page 4 of 3? </li>
  <li>How can I omit the empty page, while keeping
  the page numbering?</li>
  </ul>
  
  <p>The Scenario:</p>
  
  <p>Using the following XSL <strong>fop inserts occasionally an empty</strong> page at the end of the page flow. This also leads to <strong>strange page numbering like Page 4 of 3 Pages</strong>. </p>
  
  <p>The are 3 different page-sequences and for each sequence the page numbering should start with 1. The first should just add something like a cover page. The second page-sequence should render some table data using different footers on first-page, last-page and others. The third page-sequence should represent something like a copy of page-sequence two, having the same output but in reality having slightly different headers and/or footers.</p>
  
  <p>However, <strong>at the end of each sequence an empty blank page is insert - except for the last page-sequence</strong>. </p>
  
  <p>Each page-sequence for itself (the others commented out) is working fine, without generating the empty page.</p>
  
  <p>My XSL</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"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  version="1.0"
                  xsi:schemaLocation="http://www.w3.org/1999/XSL/Format http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop/src/foschema/fop.xsd?view=co"&gt;
  
  
      &lt;xsl:template match="/"&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-cover"
                                         page-width="210mm" page-height="297mm" margin="1cm"&gt;
                      &lt;fo:region-body region-name="body"/&gt;
                  &lt;/fo:simple-page-master&gt;
  
                  &lt;fo:simple-page-master master-name="A4-first"
                                         page-width="210mm" page-height="297mm" margin="1cm"&gt;
                      &lt;fo:region-body region-name="body" space-before="11cm" space-after="3cm"/&gt;
                      &lt;fo:region-before region-name="header-first" extent="11cm"/&gt;
                      &lt;fo:region-after region-name="footer-first" extent="3cm"/&gt;
                  &lt;/fo:simple-page-master&gt;
  
  
                  &lt;fo:simple-page-master master-name="A4-other"
                                         page-width="210mm" page-height="297mm" margin="1cm"&gt;
                      &lt;fo:region-body region-name="body" space-before="3cm" space-after="5cm"/&gt;
                      &lt;fo:region-before region-name="header-other" extent="3cm"/&gt;
                      &lt;fo:region-after region-name="footer-other" extent="5cm"/&gt;
                  &lt;/fo:simple-page-master&gt;
  
  
                  &lt;fo:simple-page-master master-name="A4-last"
                                         page-width="210mm" page-height="297mm" margin="1cm"&gt;
                      &lt;fo:region-body region-name="body" space-before="3cm" space-after="5cm"/&gt;
                      &lt;fo:region-before region-name="header-last" extent="3cm"/&gt;
                      &lt;fo:region-after region-name="footer-last" extent="5cm"/&gt;
                  &lt;/fo:simple-page-master&gt;
  
  
                  &lt;fo:page-sequence-master master-name="A4-multi"&gt;
                      &lt;fo:repeatable-page-master-alternatives&gt;
                          &lt;fo:conditional-page-master-reference page-position="first" master-reference="A4-first"/&gt;
                          &lt;fo:conditional-page-master-reference page-position="rest" master-reference="A4-other"/&gt;
                          &lt;fo:conditional-page-master-reference page-position="last" master-reference="A4-last"/&gt;
                      &lt;/fo:repeatable-page-master-alternatives&gt;
                  &lt;/fo:page-sequence-master&gt;
  
  
                  &lt;fo:page-sequence-master master-name="A4-very-first"&gt;
                      &lt;fo:repeatable-page-master-alternatives&gt;
                          &lt;fo:conditional-page-master-reference page-position="first" master-reference="A4-cover"/&gt;
                          &lt;fo:conditional-page-master-reference page-position="rest" master-reference="A4-other"/&gt;
                      &lt;/fo:repeatable-page-master-alternatives&gt;
                  &lt;/fo:page-sequence-master&gt;
              &lt;/fo:layout-master-set&gt;
  
  
  
              &lt;fo:page-sequence master-reference="A4-very-first" initial-page-number="1"&gt;
                  &lt;fo:flow flow-name="body"&gt;
                      &lt;fo:block&gt;just the first page&lt;/fo:block&gt;
                  &lt;/fo:flow&gt;
              &lt;/fo:page-sequence&gt;
  
              &lt;fo:page-sequence master-reference="A4-multi" initial-page-number="1"&gt;
  
                  &lt;xsl:call-template name="header-first"/&gt;
                  &lt;xsl:call-template name="header-other"/&gt;
                  &lt;xsl:call-template name="header-last"/&gt;
  
                  &lt;xsl:call-template name="footer-first"/&gt;
                  &lt;xsl:call-template name="footer-other"/&gt;
                  &lt;xsl:call-template name="footer-last"/&gt;
  
                  &lt;xsl:call-template name="flow-1"/&gt;
              &lt;/fo:page-sequence&gt;
  
  
              &lt;fo:page-sequence master-reference="A4-multi" initial-page-number="1"&gt;
  
                  &lt;xsl:call-template name="header-first"/&gt;
                  &lt;xsl:call-template name="header-other"/&gt;
                  &lt;xsl:call-template name="header-last"/&gt;
  
                  &lt;xsl:call-template name="footer-first"/&gt;
                  &lt;xsl:call-template name="footer-other"/&gt;
                  &lt;xsl:call-template name="footer-last"/&gt;
                  &lt;xsl:call-template name="flow-2"/&gt;
  
              &lt;/fo:page-sequence&gt;
  
  
          &lt;/fo:root&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template name="flow-1"&gt;
          &lt;fo:flow flow-name="body"&gt;
              &lt;xsl:call-template name="item-table"/&gt;
              &lt;fo:block id="last-page-flow-1"&gt;&lt;/fo:block&gt;
          &lt;/fo:flow&gt;
      &lt;/xsl:template&gt;
  
      &lt;!-- just a copy of flow 1--&gt;
      &lt;xsl:template name="flow-2"&gt;
          &lt;fo:flow flow-name="body"&gt;
              &lt;xsl:call-template name="item-table"/&gt;
              &lt;fo:block id="last-page-flow-2"&gt;&lt;/fo:block&gt;
          &lt;/fo:flow&gt;
      &lt;/xsl:template&gt;
      &lt;!-- table --&gt;
      &lt;xsl:template name="item-table"&gt;
          &lt;fo:table width="100%" border="1px solid black" table-layout="fixed"&gt;
              &lt;fo:table-column column-width="proportional-column-width(1)"/&gt;
  
              &lt;fo:table-header&gt;
                  &lt;fo:table-row border="1px solid red"&gt;
                      &lt;fo:table-cell&gt;
                          &lt;fo:block&gt;Item: header&lt;/fo:block&gt;
                      &lt;/fo:table-cell&gt;
                  &lt;/fo:table-row&gt;
              &lt;/fo:table-header&gt;
  
              &lt;fo:table-footer&gt;
                  &lt;fo:table-row&gt;
                      &lt;fo:table-cell border="1px solid blue"&gt;
                          &lt;fo:block&gt;Item: footer&lt;/fo:block&gt;
                      &lt;/fo:table-cell&gt;
                  &lt;/fo:table-row&gt;
              &lt;/fo:table-footer&gt;
  
              &lt;fo:table-body&gt;
                  &lt;xsl:apply-templates select="list"/&gt;
              &lt;/fo:table-body&gt;
  
          &lt;/fo:table&gt;
      &lt;/xsl:template&gt;
      &lt;!-- row --&gt;
      &lt;xsl:template match="item"&gt;
          &lt;fo:table-row&gt;
              &lt;fo:table-cell border-bottom="1px dashed black"&gt;
                  &lt;fo:block&gt;
                      &lt;xsl:value-of select="out"/&gt;
                  &lt;/fo:block&gt;
              &lt;/fo:table-cell&gt;
          &lt;/fo:table-row&gt;
      &lt;/xsl:template&gt;
  
      &lt;!-- the different headers  --&gt;
      &lt;xsl:template name="header-initial"&gt;
          &lt;fo:static-content flow-name="header-initial"&gt;
              &lt;fo:table background-color="yellow" width="100%" table-layout="fixed"&gt;
                  &lt;fo:table-body&gt;
                      &lt;fo:table-row&gt;
                          &lt;fo:table-cell height="4cm"&gt;
                              &lt;fo:block&gt;Header first page&lt;/fo:block&gt;
                          &lt;/fo:table-cell&gt;
                      &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
              &lt;/fo:table&gt;
          &lt;/fo:static-content&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template name="header-first"&gt;
          &lt;fo:static-content flow-name="header-first"&gt;
              &lt;fo:table background-color="yellow" width="100%" table-layout="fixed"&gt;
                  &lt;fo:table-body&gt;
                      &lt;fo:table-row&gt;
                          &lt;fo:table-cell height="11cm"&gt;
                              &lt;fo:block&gt;Header first page&lt;/fo:block&gt;
                          &lt;/fo:table-cell&gt;
                      &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
              &lt;/fo:table&gt;
          &lt;/fo:static-content&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template name="header-other"&gt;
          &lt;fo:static-content flow-name="header-other"&gt;
              &lt;fo:table background-color="red" width="100%" table-layout="fixed"&gt;
                  &lt;fo:table-body&gt;
                      &lt;fo:table-row&gt;
                          &lt;fo:table-cell height="3cm"&gt;
                              &lt;fo:block&gt;Header other pages&lt;/fo:block&gt;
                          &lt;/fo:table-cell&gt;
                      &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
              &lt;/fo:table&gt;
          &lt;/fo:static-content&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template name="header-last"&gt;
          &lt;fo:static-content flow-name="header-last"&gt;
              &lt;fo:table background-color="blue" width="100%" table-layout="fixed"&gt;
                  &lt;fo:table-body&gt;
                      &lt;fo:table-row&gt;
                          &lt;fo:table-cell height="3cm"&gt;
                              &lt;fo:block&gt;Header last page&lt;/fo:block&gt;
                          &lt;/fo:table-cell&gt;
                      &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
              &lt;/fo:table&gt;
          &lt;/fo:static-content&gt;
      &lt;/xsl:template&gt;
      &lt;!-- the different footers  --&gt;
      &lt;xsl:template name="footer-initial"&gt;
          &lt;fo:static-content flow-name="footer-initial"&gt;
              &lt;fo:table background-color="orange" width="100%" table-layout="fixed"&gt;
                  &lt;fo:table-body&gt;
                      &lt;fo:table-row&gt;
                          &lt;fo:table-cell height="106mm"&gt;
                              &lt;fo:block&gt;Footer initial page - ESR&lt;/fo:block&gt;
                              &lt;fo:block&gt;P&lt;fo:page-number/&gt;/
                                  &lt;fo:page-number-citation-last ref-id="last-page-flow-1"/&gt;
                                  &lt;fo:page-number-citation-last ref-id="last-page-flow-2"/&gt;
                              &lt;/fo:block&gt;
                          &lt;/fo:table-cell&gt;
                      &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
              &lt;/fo:table&gt;
          &lt;/fo:static-content&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template name="footer-first"&gt;
          &lt;fo:static-content flow-name="footer-first"&gt;
              &lt;fo:table background-color="yellow" width="100%" table-layout="fixed"&gt;
                  &lt;fo:table-body&gt;
                      &lt;fo:table-row&gt;
                          &lt;fo:table-cell height="3cm"&gt;
                              &lt;fo:block&gt;Footer first page&lt;/fo:block&gt;
                              &lt;fo:block&gt;Page
                                  &lt;fo:page-number/&gt;
                                  of
                                  &lt;fo:page-number-citation-last ref-id="last-page-flow-1"/&gt;
                                  &lt;fo:page-number-citation-last ref-id="last-page-flow-2"/&gt;
                                  Pages
                              &lt;/fo:block&gt;
                          &lt;/fo:table-cell&gt;
                      &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
              &lt;/fo:table&gt;
          &lt;/fo:static-content&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template name="footer-other"&gt;
          &lt;fo:static-content flow-name="footer-other"&gt;
              &lt;fo:table background-color="red" width="100%" table-layout="fixed"&gt;
                  &lt;fo:table-body&gt;
                      &lt;fo:table-row&gt;
                          &lt;fo:table-cell height="5cm"&gt;
                              &lt;fo:block&gt;Footer other pages&lt;/fo:block&gt;
                              &lt;fo:block&gt;Page
                                  &lt;fo:page-number/&gt;
                                  of
                                  &lt;fo:page-number-citation-last ref-id="last-page-flow-1"/&gt;
                                  &lt;fo:page-number-citation-last ref-id="last-page-flow-2"/&gt;
                                  Pages
                              &lt;/fo:block&gt;
                          &lt;/fo:table-cell&gt;
                      &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
              &lt;/fo:table&gt;
  
          &lt;/fo:static-content&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template name="footer-last"&gt;
          &lt;fo:static-content flow-name="footer-last"&gt;
              &lt;fo:table background-color="blue" width="100%" table-layout="fixed"&gt;
                  &lt;fo:table-body&gt;
                      &lt;fo:table-row&gt;
                          &lt;fo:table-cell height="5cm"&gt;
                              &lt;fo:block&gt;Footer last page&lt;/fo:block&gt;
                              &lt;fo:block&gt;Page&lt;fo:page-number/&gt;of
                                  &lt;fo:page-number-citation-last ref-id="last-page-flow-1"/&gt;
                                  &lt;fo:page-number-citation-last ref-id="last-page-flow-2"/&gt;
                                  Pages
                              &lt;/fo:block&gt;
                          &lt;/fo:table-cell&gt;
                      &lt;/fo:table-row&gt;
                  &lt;/fo:table-body&gt;
              &lt;/fo:table&gt;
          &lt;/fo:static-content&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>My XML with some simple data</p>
  
  <pre><code>&lt;list&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test1&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test2&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test3&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test4&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test5&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test6&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test7&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test8&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test9&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test10&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test11&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test12&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test13&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test14&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test15&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test16&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test17&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test18&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test19&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test20&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test21&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test22&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test23 -- last on page-first&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;!-- max items on first page --&gt;
  &lt;item&gt;
      &lt;out&gt;Test24 -- first on page-other/page-last&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test25&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test26&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test27&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test28&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test29&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test30&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test31&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test32&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test33&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test34&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test35&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test36&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test37&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test38&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test39&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test40&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test41&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test42&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test43&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test44&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test45&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test46&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test47&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test48&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test49&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test50&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test51&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test52&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test53&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test54&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test55&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test56&lt;/out&gt;
  &lt;/item&gt;
  &lt;item&gt;
      &lt;out&gt;Test57 - last on page-other&lt;/out&gt;
  &lt;/item&gt;
  
  &lt;item&gt;
      &lt;out&gt;Test58 -first on page-last&lt;/out&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p></p>
  
  <p>Empty Page (red) after last page of page-sequence two (blue), 
  <a href="https://i.stack.imgur.com/y5yXP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y5yXP.jpg" alt=""></a></p>