XSL-FO fo:repeatable-page-master-alternatives 无法正常工作

XSL-FO fo:repeatable-page-master-alternatives not working properly

如果页数超过一页,我想在页脚中添加页码,但如果只有一页,则不会。

我尝试了以下代码,但它在所有情况下都显示页码:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="singlePage" page-height="800pt" page-width="612pt" margin-top="0pt" margin-bottom="46pt" margin-left="26pt" margin-right="26pt">
                <fo:region-body margin-top="110pt" margin-bottom="65pt" />
                <fo:region-before extent="72pt" />
                <fo:region-after region-name="xsl-region-after-single" extent="75pt" />
            </fo:simple-page-master>

            <fo:simple-page-master master-name="multiPage" page-height="800pt" page-width="612pt" margin-top="0pt" margin-bottom="46pt" margin-left="26pt" margin-right="26pt">
                <fo:region-body margin-top="110pt" margin-bottom="65pt" />
                <fo:region-before extent="72pt" />
                <fo:region-after region-name="xsl-region-after-multi" extent="75pt" />
            </fo:simple-page-master>

            <fo:page-sequence-master master-name="allPages">
                <fo:repeatable-page-master-alternatives>
                   <fo:conditional-page-master-reference page-position="any" master-reference="multiPage"/>
                   <fo:conditional-page-master-reference page-position="only" master-reference="singlePage"/>
                </fo:repeatable-page-master-alternatives>
            </fo:page-sequence-master>
        </fo:layout-master-set>

        <fo:page-sequence master-reference="allPages">
            <fo:static-content flow-name="xsl-region-before">
                <fo:block>content</fo:block>
            </fo:static-content>
            <fo:static-content flow-name="xsl-region-after-single">
                <fo:block>content</fo:block>
            </fo:static-content>
            <fo:static-content flow-name="xsl-region-after-multi">
                    <fo:block>content</fo:block>
                    <fo:block text-align="right">
                        <fo:inline><fo:page-number font-weight="normal"/>/<fo:page-number-citation ref-id = "lastPage"/></fo:inline>
                    </fo:block>
            </fo:static-content>
            <fo:flow flow-name="xsl-region-body" font-size="12pt" line-height="11pt">
                <fo:block>content</fo:block>
                <fo:block id = "lastPage"/>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>

如果我更改替代项的顺序,则永远不会显示页码:

<fo:page-sequence-master master-name="allPages">
    <fo:repeatable-page-master-alternatives>
        <fo:conditional-page-master-reference page-position="only" master-reference="singlePage"/>
        <fo:conditional-page-master-reference page-position="any" master-reference="multiPage"/>
    </fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>

我正在使用 FOP 2.0

感谢您的回答。

你的第二种选择对我来说适用于 FOP 2.0 和 FOP 2.2:

<fo:page-sequence-master master-name="allPages">
    <fo:repeatable-page-master-alternatives>
        <fo:conditional-page-master-reference page-position="only" master-reference="singlePage"/>
        <fo:conditional-page-master-reference page-position="any" master-reference="multiPage"/>
    </fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>

你试过有足够的内容制作第二页吗?例如,添加 <fo:block break-before="page">content</fo:block> 以强制显示第二页。

如果您将 fo:static-content 中的 'content' 文本更改为 fo:region-after,那么您会更好地了解正在使用哪个 fo:conditional-page-master-reference;例如:

<fo:static-content flow-name="xsl-region-after-single">
   <fo:block>after single</fo:block>
</fo:static-content>

fo:conditional-page-master-reference (https://www.w3.org/TR/xsl11/#fo_conditional-page-master-reference) 的工作方式是,如果它是所有子条件都为真的第一个备选方案,它就会被选中。如果有足够的内容制作第二页,则 page-position="only" 子条件不再成立,因此格式化程序应使用其他替代方案重试。

格式化程序应该重试,因为如果没有,则 fo:repeatable-page-master-alternatives (https://www.w3.org/TR/xsl11/#fo_repeatable-page-master-alternatives) 不满足其约束(我的重点):

The sub-sequence of pages mapped to this sub-sequence-specifier satisfies the constraints of this sub-sequence-specifier if (a) the sub-sequence of pages consists of zero or more pages, (b) each page is generated using the fo:simple-page-master referenced by the one of the alternatives that are the children of the fo:repeatable-page-master-alternatives, (c) the conditions on that alternative are true, (d) that alternative is the first alternative in the sequence of children for which all the conditions are true, and (e) the length of the sub-sequence is less than or equal to the value of 'maximum-repeats'.