title: Page numbers in xsl-fo (and apache fop) with multiple page-sequences
body: <p>I have a document consisting of multiple sub-documents. Here is how a sub-document looks like:</p>
  
  <p>1 page<br>
  2-n pages</p>
  
  <p>Then comes the next sub-document with the same structure:</p>
  
  <p>1 page<br>
  2-m pages</p>
  
  <p>As I said, those sub-documents are combined in a single .pdf file.</p>
  
  <p>Here is my xsl-fo tempalte:</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:output method="xml" indent="yes" /&gt;
    &lt;xsl:template match="/"&gt;
      &lt;fo:root font-size="11pt" font-family="serif"&gt;
        &lt;fo:layout-master-set&gt;
          &lt;fo:simple-page-master master-name="A4-portrait"
            page-height="29.7cm" page-width="21.0cm" margin-top="1cm"
            margin-left="1.5cm" margin-right="1cm" margin-bottom="1cm"&gt;
            &lt;fo:region-body /&gt;
            &lt;fo:region-after region-name="footer" extent="15mm"/&gt;
          &lt;/fo:simple-page-master&gt;
        &lt;/fo:layout-master-set&gt;
  
        &lt;!-- first sub-document --&gt;
        &lt;fo:page-sequence master-reference="A4-portrait" initial-page-number="1"&gt;
        ...........
          &lt;fo:block text-align="center"&gt;
              Page &lt;fo:page-number/&gt;/&lt;fo:page-number-citation-last ref-id="end"/&gt;
          &lt;/fo:block&gt;
        ...........
        &lt;/fo:page-sequence&gt;
        &lt;fo:page-sequence master-reference="A4-portrait" id="end"&gt;
        ...........
          &lt;fo:block text-align="center"&gt;
              Page &lt;fo:page-number/&gt;/&lt;fo:page-number-citation-last ref-id="end"/&gt;
          &lt;/fo:block&gt;
        ...........
        &lt;/fo:page-sequence&gt;
  
        &lt;!-- second sub-document --&gt;
        &lt;fo:page-sequence master-reference="A4-portrait" initial-page-number="1"&gt;
        ...........
          &lt;fo:block text-align="center"&gt;
              Page &lt;fo:page-number/&gt;/&lt;fo:page-number-citation-last ref-id="end"/&gt;
          &lt;/fo:block&gt;
        ...........
        &lt;/fo:page-sequence&gt;
        &lt;fo:page-sequence master-reference="A4-portrait" id="end"&gt;
        ...........
          &lt;fo:block text-align="center"&gt;
              Page &lt;fo:page-number/&gt;/&lt;fo:page-number-citation-last ref-id="end"/&gt;
          &lt;/fo:block&gt;
        ...........
        &lt;/fo:page-sequence&gt;
  
      &lt;/fo:root&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>So, I have 2 identical blocks consisting of 2 page-sequences each. In my example, let's say sub-document 1 has 4 pages (n=4) and sub-document 2 has 2 pages (m=2). Here are the page numbers that I get:</p>
  
  <pre><code>1/4
  2/4
  3/4
  4/4
  1/4
  2/2
  </code></pre>
  
  <p>Everything is OK, except the first page of the second sub-document. At that point, <code>&lt;fo:page-number-citation-last ref-id="end"/&gt;</code> returns 4, which is the value from sub-document 1. So, instead of <code>1/2</code>, I get <code>1/4</code>.</p>
  
  <p>Any suggestions how I can fix this?</p>