【问题标题】:Set value in xsl-region-before foblock from xml child tag value从 xml 子标签值设置 xsl-region-before foblock 中的值
【发布时间】:2019-11-12 16:38:37
【问题描述】:

我正在使用 apache fop 通过 XSL 和 XML 文件生成 PDF 文件,我将从数据库中获取地图列表并将其作为 XML 标记值和 PDF 模板写入 XSL 文件中,我需要逐页创建 PDF使用 XML 标记值。我尝试使用 fo:flow 元素,但输出值未在 xsl-region-before 中设置,请检查我的文件..

我的 XSL

<?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:template match="pages">
            <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="simple"
                        page-height="8.5in" page-width="11in" margin-top=".5in"
                        margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
                        <fo:region-body margin-top="2cm" margin-bottom="2cm" />
                        <fo:region-before extent="2cm" overflow="hidden" />
                        <fo:region-after extent="1cm" overflow="hidden" />
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="simple"
                    initial-page-number="1">
                    <fo:static-content flow-name="xsl-region-before">
                        <fo:block font-size="13.0pt" font-family="serif"
                            padding-after="2.0pt" space-before="4.0pt" text-align="center"
                            border-bottom-style="solid" border-bottom-width="1.0pt">
                            <fo:block>
                            <xsl:value-of select="page-header"/>
                            </fo:block>
                        </fo:block>
                    </fo:static-content>
                    <fo:static-content flow-name="xsl-region-after">
                        <fo:block font-size="12.0pt" font-family="sans-serif"
                            padding-after="2.0pt" space-before="2.0pt" text-align="center"
                            border-top-style="solid" border-bottom-width="1.0pt">
                            <xsl:text>Page</xsl:text>
                            <fo:page-number />
                        </fo:block>
                    </fo:static-content>
                    <fo:flow flow-name="xsl-region-body">
                        <xsl:apply-templates select="page-body" />
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
        <xsl:template match="page-body">
            <fo:block text-align="center" break-before="page">
                <fo:table table-layout="fixed" width="100%"
                    border-style="dashed">
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-header>
                        <xsl:apply-templates select="table-header" />
                    </fo:table-header>
                    <fo:table-body>
                        <xsl:apply-templates select="table-data" />
                    </fo:table-body>
                </fo:table>
            </fo:block>
        </xsl:template>
        <xsl:template match="table-header">
            <fo:table-row keep-together.within-page="always"
                border-style="solid">
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-one"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-two"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-three"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
        <xsl:template match="table-data">
            <fo:table-row keep-together.within-page="always"
                border-style="solid">
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-one"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:if test="number(column-two) = number(column-two)">
                        <xsl:value-of select="format-number(translate(column-two, ',','.'), '#,###.##')"></xsl:value-of>
                        </xsl:if>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-three"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
    </xsl:stylesheet>

我的 XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<pages>
<page-body>
    <page-header>
    Page Sample 1
    </page-header>
    <table-header>
        <column-one>Column One</column-one>
        <column-two>Column Two</column-two>
        <column-three>Column Three</column-three>
    </table-header>
    <table-data>
        <column-one>One</column-one>
        <column-two>5000</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>5000</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>0</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>0</column-two>
        <column-three>Four</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>2000</column-two>
        <column-three>Four</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>1234</column-two>
        <column-three>Five</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>5666</column-two>
        <column-three>Five</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>5666</column-two>
        <column-three>Five</column-three>
    </table-data>
     </page-body>
     
     <page-body>
     <page-header>
    Page Sample 2
    </page-header>
    <table-header>
        <column-one>Column One</column-one>
        <column-two>Column Two</column-two>
        <column-three>Column Three</column-three>
    </table-header>
    <table-data>
        <column-one>One</column-one>
        <column-two>8000</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>8000</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>100</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>100</column-two>
        <column-three>Four</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>7000</column-two>
        <column-three>Four</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>7000</column-two>
        <column-three>Five</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>6666</column-two>
        <column-three>Five</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>6666</column-two>
        <column-three>Five</column-three>
    </table-data>
     </page-body>
</pages>

帮助我从每个 page-body 标签内的 xml 标签 page-header 获取和设置 xsl-region-before 块的值。

期望输出是

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="simple"
            page-height="8.5in" page-width="11in" margin-top=".5in"
            margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
            <fo:region-body margin-top="2cm" margin-bottom="2cm" />
            <fo:region-before extent="2cm" overflow="hidden" />
            <fo:region-after extent="1cm" overflow="hidden" />
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="simple"
        initial-page-number="1">
        <fo:static-content flow-name="xsl-region-before">
            <fo:block font-size="13.0pt" font-family="serif"
                padding-after="2.0pt" space-before="4.0pt" text-align="center"
                border-bottom-style="solid" border-bottom-width="1.0pt">
                <fo:block>
                    Page Sample 1
<!--As "Page Sample 2" from xml page-body/page-header value for next page -->
                </fo:block>
            </fo:block>
        </fo:static-content>
        <fo:static-content flow-name="xsl-region-after">
            <fo:block font-size="12.0pt" font-family="sans-serif"
                padding-after="2.0pt" space-before="2.0pt" text-align="center"
                border-top-style="solid" border-bottom-width="1.0pt">
                Page
                <fo:page-number />
            </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
            <fo:block text-align="center" break-before="page">
                <fo:table table-layout="fixed" width="100%"
                    border-style="dashed">
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-header>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Column One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Column Two</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Column Three</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-header>
                    <fo:table-body>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">5,000.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Three</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">5,000.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Three</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Three</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Four</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">2,000.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Four</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">1,234.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Five</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">5,666.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Five</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">5,666.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Five</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:block>
            <fo:block text-align="center" break-before="page">
                <fo:table table-layout="fixed" width="100%"
                    border-style="dashed">
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-header>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Column One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Column Two</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Column Three</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-header>
                    <fo:table-body>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">8,000.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Three</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">8,000.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Three</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">100.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Three</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">100.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Four</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">7,000.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Four</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">7,000.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Five</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">6,666.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Five</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row keep-together.within-page="always"
                            border-style="solid">
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">One</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">6,666.0</fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                                <fo:block font-size="10pt" font-family="sans-serif"
                                    padding-top="3pt">Five</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

【问题讨论】:

  • 请将预期的输出添加到您的问题中。
  • 添加图片参考@michael.hor257k
  • Page-header 不是您在模板中所期望的页面的子页面
  • 请将预期的输出添加到您的问题中。作为代码,而不是图像。
  • @KevinBrown 是的,它不能是一个孩子,但是有没有任何选项可以通过读取 xml 标签在页眉中设置不同的值。

标签: xml pdf xslt xslt-1.0 apache-fop


【解决方案1】:

这是使用 FOP 和标记的第二个答案。只需对您的 XSL 稍作修改。这在 region-before 中使用了retrieve-marker,并在每次遇到页面正文时设置该标记:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="pages">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="simple"
                page-height="8.5in" page-width="11in" margin-top=".5in"
                margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
                <fo:region-body margin-top="2cm" margin-bottom="2cm" />
                <fo:region-before extent="2cm" overflow="hidden" />
                <fo:region-after extent="1cm" overflow="hidden" />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="simple"
            initial-page-number="1">
            <fo:static-content flow-name="xsl-region-before">
                <fo:block font-size="13.0pt" font-family="serif"
                    padding-after="2.0pt" space-before="4.0pt" text-align="center"
                    border-bottom-style="solid" border-bottom-width="1.0pt">
                    <fo:retrieve-marker retrieve-class-name="header" retrieve-position="first-including-carryover"/>
                </fo:block>
            </fo:static-content>
            <fo:static-content flow-name="xsl-region-after">
                <fo:block font-size="12.0pt" font-family="sans-serif"
                    padding-after="2.0pt" space-before="2.0pt" text-align="center"
                    border-top-style="solid" border-bottom-width="1.0pt">
                    <xsl:text>Page</xsl:text>
                    <fo:page-number />
                </fo:block>
            </fo:static-content>
            <fo:flow flow-name="xsl-region-body">
                <xsl:apply-templates select="page-body" />
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>
<xsl:template match="page-body">
    <fo:block text-align="center" break-before="page">
        <fo:marker marker-class-name="header">
            <fo:block>
                <xsl:value-of select="page-header"/>
            </fo:block>
        </fo:marker>
        <fo:table table-layout="fixed" width="100%"
            border-style="dashed">
            <fo:table-column border-style="solid" />
            <fo:table-column border-style="solid" />
            <fo:table-column border-style="solid" />
            <fo:table-header>
                <xsl:apply-templates select="table-header" />
            </fo:table-header>
            <fo:table-body>
                <xsl:apply-templates select="table-data" />
            </fo:table-body>
        </fo:table>
    </fo:block>
</xsl:template>
<xsl:template match="table-header">
    <fo:table-row keep-together.within-page="always"
        border-style="solid">
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-one"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-two"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-three"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
    </fo:table-row>
</xsl:template>
<xsl:template match="table-data">
    <fo:table-row keep-together.within-page="always"
        border-style="solid">
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-one"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:if test="number(column-two) = number(column-two)">
                    <xsl:value-of select="format-number(translate(column-two, ',','.'), '#,###.##')"></xsl:value-of>
                </xsl:if>
            </fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-three"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
    </fo:table-row>
</xsl:template>
</xsl:stylesheet>

这会产生以下结果:

【讨论】:

  • 是的,这个解决方案完全有效,感谢您的理解,感谢您的解决方案??
【解决方案2】:

根据您的设计,您可以使用不同的页面序列,因为每个页面序列的静态内容都不同,并且您在每个页面的开头都破坏了页面。您可能不想重新启动页码等,但也许您愿意,我不知道。

你也可以使用marker/retrieve-marker将内容拉入区域——之前,见第二个答案。

也许像这样重组:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="pages">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="simple"
                page-height="8.5in" page-width="11in" margin-top=".5in"
                margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
                <fo:region-body margin-top="2cm" margin-bottom="2cm" />
                <fo:region-before extent="2cm" overflow="hidden" />
                <fo:region-after extent="1cm" overflow="hidden" />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <xsl:apply-templates/>
    </fo:root>
</xsl:template>
<xsl:template match="page-body">

    <fo:page-sequence master-reference="simple"
        initial-page-number="1">
        <fo:static-content flow-name="xsl-region-before">
            <fo:block font-size="13.0pt" font-family="serif"
                padding-after="2.0pt" space-before="4.0pt" text-align="center"
                border-bottom-style="solid" border-bottom-width="1.0pt">
                <fo:block>
                    <xsl:value-of select="page-header"/>
                </fo:block>
            </fo:block>
        </fo:static-content>
        <fo:static-content flow-name="xsl-region-after">
            <fo:block font-size="12.0pt" font-family="sans-serif"
                padding-after="2.0pt" space-before="2.0pt" text-align="center"
                border-top-style="solid" border-bottom-width="1.0pt">
                <xsl:text>Page</xsl:text>
                <fo:page-number />
            </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
            <fo:block text-align="center" break-before="page">
                <fo:table table-layout="fixed" width="100%"
                    border-style="dashed">
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-header>
                        <xsl:apply-templates select="table-header" />
                    </fo:table-header>
                    <fo:table-body>
                        <xsl:apply-templates select="table-data" />
                    </fo:table-body>
                </fo:table>
            </fo:block>
        </fo:flow>
    </fo:page-sequence>




</xsl:template>
<xsl:template match="table-header">
    <fo:table-row keep-together.within-page="always"
        border-style="solid">
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-one"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-two"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-three"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
    </fo:table-row>
</xsl:template>
<xsl:template match="table-data">
    <fo:table-row keep-together.within-page="always"
        border-style="solid">
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-one"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:if test="number(column-two) = number(column-two)">
                    <xsl:value-of select="format-number(translate(column-two, ',','.'), '#,###.##')"></xsl:value-of>
                </xsl:if>
            </fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block font-size="10pt" font-family="sans-serif"
                padding-top="3pt">
                <xsl:value-of select="column-three"></xsl:value-of>
            </fo:block>
        </fo:table-cell>
    </fo:table-row>
</xsl:template>
</xsl:stylesheet>

这应该给出以下输出:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
    <fo:simple-page-master master-name="simple" page-height="8.5in" page-width="11in" margin-top=".5in" margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
        <fo:region-body margin-top="2cm" margin-bottom="2cm"></fo:region-body>
        <fo:region-before extent="2cm" overflow="hidden"></fo:region-before>
        <fo:region-after extent="1cm" overflow="hidden"></fo:region-after>
    </fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple" initial-page-number="1">
    <fo:static-content flow-name="xsl-region-before">
        <fo:block font-size="13.0pt" font-family="serif" padding-after="2.0pt" space-before="4.0pt" text-align="center" border-bottom-style="solid" border-bottom-width="1.0pt">
            <fo:block> Page Sample 1 </fo:block>
        </fo:block>
    </fo:static-content>
    <fo:static-content flow-name="xsl-region-after">
        <fo:block font-size="12.0pt" font-family="sans-serif" padding-after="2.0pt" space-before="2.0pt" text-align="center" border-top-style="solid" border-bottom-width="1.0pt">Page<fo:page-number></fo:page-number></fo:block>
    </fo:static-content>
    <fo:flow flow-name="xsl-region-body">
        <fo:block text-align="center" break-before="page">
            <fo:table table-layout="fixed" width="100%" border-style="dashed">
                <fo:table-column border-style="solid"></fo:table-column>
                <fo:table-column border-style="solid"></fo:table-column>
                <fo:table-column border-style="solid"></fo:table-column>
                <fo:table-header>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Column One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Column Two</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Column Three</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-header>
                <fo:table-body>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">5,000.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Three</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">5,000.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Three</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Three</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Four</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">2,000.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Four</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">1,234.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Five</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">5,666.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Five</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">5,666.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Five</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:block>
    </fo:flow>
</fo:page-sequence>

<fo:page-sequence master-reference="simple" initial-page-number="1">
    <fo:static-content flow-name="xsl-region-before">
        <fo:block font-size="13.0pt" font-family="serif" padding-after="2.0pt" space-before="4.0pt" text-align="center" border-bottom-style="solid" border-bottom-width="1.0pt">
            <fo:block> Page Sample 2 </fo:block>
        </fo:block>
    </fo:static-content>
    <fo:static-content flow-name="xsl-region-after">
        <fo:block font-size="12.0pt" font-family="sans-serif" padding-after="2.0pt" space-before="2.0pt" text-align="center" border-top-style="solid" border-bottom-width="1.0pt">Page<fo:page-number></fo:page-number></fo:block>
    </fo:static-content>
    <fo:flow flow-name="xsl-region-body">
        <fo:block text-align="center" break-before="page">
            <fo:table table-layout="fixed" width="100%" border-style="dashed">
                <fo:table-column border-style="solid"></fo:table-column>
                <fo:table-column border-style="solid"></fo:table-column>
                <fo:table-column border-style="solid"></fo:table-column>
                <fo:table-header>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Column One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Column Two</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Column Three</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-header>
                <fo:table-body>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">8,000.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Three</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">8,000.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Three</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">100.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Three</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">100.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Four</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">7,000.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Four</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">7,000.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Five</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">6,666.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Five</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row keep-together.within-page="always" border-style="solid">
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">One</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">6,666.0</fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block font-size="10pt" font-family="sans-serif" padding-top="3pt">Five</fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:block>
    </fo:flow>
</fo:page-sequence>

如果您删除初始页码(不确定您想要什么)并删除您正在创建的分页符,则会产生以下结果:

【讨论】:

  • 抱歉,它在所有页面中打印相同的“页面示例 1”,我需要在 XML page-body/page-header 标签的 header 中显示值。
  • 参见上面的编辑,不会根据需要产生两个页面序列
  • 如果您真的只想要一个流动的页面序列,该页面序列具有不同的页面标题但所有页面都在一起,那么您需要使用标记/检索标记将内容拉入标题中。你的问题并不清楚你的预期应该是什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多