简体   繁体   English

具有多个页面序列的xsl-fo(和apache fop)中的页码

[英]Page numbers in xsl-fo (and apache fop) with multiple page-sequences

I have a document consisting of multiple sub-documents. 我有一个由多个子文档组成的文档。 Here is how a sub-document looks like: 以下是子文档的外观:

1 page 1页
2-n pages 2-n页

Then comes the next sub-document with the same structure: 然后是具有相同结构的下一个子文档:

1 page 1页
2-m pages 2米页

As I said, those sub-documents are combined in a single .pdf file. 正如我所说,这些子文档组合在一个.pdf文件中。

Here is my xsl-fo tempalte: 这是我的xsl-fo tempalte:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <fo:root font-size="11pt" font-family="serif">
      <fo:layout-master-set>
        <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">
          <fo:region-body />
          <fo:region-after region-name="footer" extent="15mm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <!-- first sub-document -->
      <fo:page-sequence master-reference="A4-portrait" initial-page-number="1">
      ...........
        <fo:block text-align="center">
            Page <fo:page-number/>/<fo:page-number-citation-last ref-id="end"/>
        </fo:block>
      ...........
      </fo:page-sequence>
      <fo:page-sequence master-reference="A4-portrait" id="end">
      ...........
        <fo:block text-align="center">
            Page <fo:page-number/>/<fo:page-number-citation-last ref-id="end"/>
        </fo:block>
      ...........
      </fo:page-sequence>

      <!-- second sub-document -->
      <fo:page-sequence master-reference="A4-portrait" initial-page-number="1">
      ...........
        <fo:block text-align="center">
            Page <fo:page-number/>/<fo:page-number-citation-last ref-id="end"/>
        </fo:block>
      ...........
      </fo:page-sequence>
      <fo:page-sequence master-reference="A4-portrait" id="end">
      ...........
        <fo:block text-align="center">
            Page <fo:page-number/>/<fo:page-number-citation-last ref-id="end"/>
        </fo:block>
      ...........
      </fo:page-sequence>

    </fo:root>
  </xsl:template>
</xsl:stylesheet>

So, I have 2 identical blocks consisting of 2 page-sequences each. 所以,我有2个相同的块,每个块由2个页面序列组成。 In my example, let's say sub-document 1 has 4 pages (n=4) and sub-document 2 has 2 pages (m=2). 在我的例子中,假设子文档1有4页(n = 4),子文档2有2页(m = 2)。 Here are the page numbers that I get: 以下是我获得的页码:

1/4
2/4
3/4
4/4
1/4
2/2

Everything is OK, except the first page of the second sub-document. 一切都很好,除了第二个子文档的第一页。 At that point, <fo:page-number-citation-last ref-id="end"/> returns 4, which is the value from sub-document 1. So, instead of 1/2 , I get 1/4 . 此时, <fo:page-number-citation-last ref-id="end"/>返回4,这是子文档1的值。因此,而不是1/2 ,我得到1/4

Any suggestions how I can fix this? 有什么建议我可以解决这个问题吗?

Can you please write this as an answer (to both my questions), so that I can accept it? 你能不能把它写成答案(对我的两个问题),以便我接受它?

Sure. 当然。 The precondition for referring to a specific element in XSL-FO is that you have a means of identifying it unambiguously . 在XSL-FO中引用特定元素的前提是您可以明确地识别它。 The best way to do this is to use an ID attribute for the element you wish to identify. 执行此操作的最佳方法是为要ID的元素使用ID属性。

An ID must consist of any sequence of alphanumerical characters and _ (that is, its value must be a valid NCName, see the specification here ). ID必须由任何字母数字字符序列和_ (即,其值必须是有效的NCName,请参阅此处的规范 )。

You can type out each ID by hand, but normally this is done with xsl:generate-id() (wich is a function of XSLT, not XSL-FO). 您可以手动输入每个ID,但通常使用xsl:generate-id() (这是XSLT的函数,而不是XSL-FO)。 generate-id() automatically ensures that the IDs are unique throughout a document. generate-id()自动确保ID在整个文档中是唯一的。

Note that generate-id() actually generates an ID for either the current template match or the node that is being processed in an iteration of xsl:for-each . 请注意, generate-id()实际上为当前模板匹配或在xsl:for-each的迭代中正在处理的节点生成ID。

You can put an ID on every element in XML and referring to something by ID is therefore possible for any element in XSL-FO. 您可以在XML中的每个元素上放置一个ID,因此可以为XSL-FO中的任何元素引用ID。


Since you provide more context here, I'll provide a more extensive answer. 由于您在此提供更多上下文,我将提供更广泛的答案。 It should now be clear how you can resolve the issue, but it is still unclear why the problem arises in the first place. 现在应该清楚如何解决这个问题,但仍然不清楚为什么问题首先出现。

The reason why the page numbers are wrong in your original stylesheet is that in the first page sequence of the second sub-document, you intend to refer to an ID that occurs after you refer to it, while at the same time IDs are not unique in the document. 原始样式表中页码错误的原因是,在第二个子文档的第一个页面序列中,您打算引用引用出现的ID,而同时ID不是唯一的在文件中。

In other words, in this page sequence: 换句话说,在这个页面序列中:

  <!-- second sub-document -->
  <fo:page-sequence master-reference="A4-portrait" initial-page-number="1">
  <!--...-->
    <fo:block text-align="center">
        Page <fo:page-number/>/<fo:page-number-citation-last ref-id="end"/>
    </fo:block>
  <!--...-->
  </fo:page-sequence>

when you reference an object by its ID ( ref-id="end" ), the XSL-FO document is searched for an object with this ID that occurs before the ref-id . 当您通过其ID( ref-id="end" )引用对象时,将在XSL-FO文档中搜索具有此ID的对象,该ID出现在ref-id之前。 As soon as a node with a corresponding ID is found, the search terminates and this node is handed to fo:page-number-citation . 一旦找到具有相应ID的节点,搜索就会终止,并且该节点将被传递给fo:page-number-citation

In this case, the object with a matching ID occurring before ref-id is a page sequence of the first subdocument. 在这种情况下,在ref-id之前出现匹配ID的对象是第一个子文档的页面序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM