18

我正在使用 XSLT 生成 PDF 报告。我的要求是在报告的页脚以第 N 页(例如第 1 页,共 3 页)的格式显示页码。对于静态值,它工作正常,并且在每一页上重复。由于报告中的总页数未知并且它改变了运行时间,所以我将如何完成这项任务。

我的 XSLT 代码片段

<xsl:template name="footerall">
<xsl:variable name="maxwidth" select="7.07000" />
<fo:static-content flow-name="xsl-region-after">
<fo:block>
<xsl:variable name="tablewidth29" select="$maxwidth * 1.00000" />
<xsl:variable name="sumcolumnwidths29" select="0.04167 + 1.56250 + 0.04167" />
<xsl:variable name="factor29">
<xsl:choose>
<xsl:when
test="$sumcolumnwidths29 &gt; 0.00000 and $sumcolumnwidths29 &gt; $tablewidth29">
<xsl:value-of select="$tablewidth29 div $sumcolumnwidths29" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="1.000" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="defaultcolumns29" select="1" />
<xsl:variable name="defaultcolumnwidth29">
<xsl:choose>
<xsl:when test="$factor29 &lt; 1.000">
<xsl:value-of select="0.000" />
</xsl:when>
<xsl:when test="$defaultcolumns29 &gt; 0">
<xsl:value-of
select="($tablewidth29 - $sumcolumnwidths29) div $defaultcolumns29" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0.000" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="columnwidth29_0" select="$defaultcolumnwidth29" />
<xsl:variable name="columnwidth29_1" select="1.56250 * $factor29" />
<fo:table width="{$tablewidth29}in" border-collapse="separate"
border-separation="0.04167in" color="black" display-align="center">
<fo:table-column column-width="{$columnwidth29_0}in" />
<fo:table-column column-width="{$columnwidth29_1}in" />
<fo:table-body>
<fo:table-row>
<fo:table-cell number-columns-spanned="2"
padding-top="0.00000in" padding-bottom="0.00000in" padding-left="0.00000in"
padding-right="0.00000in">
<fo:block padding-top="1pt" padding-bottom="1pt">
<fo:block text-align="center" space-before.optimum="-8pt">
<fo:leader leader-length="100%" leader-pattern="rule"
rule-thickness="1pt" color="black" />
</fo:block>
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell
font-size="inherited-property-value(&apos;font-size&apos;) - 2pt"
text-align="left" padding-top="0.00000in" padding-bottom="0.00000in"
padding-left="0.00000in" padding-right="0.00000in">
<fo:block padding-top="1pt" padding-bottom="1pt">
<fo:inline font-family="Courier" font-size="10px">
<xsl:value-of select="$My XPath to varaible" />
</fo:inline>
<fo:inline font-family="Courier" font-size="10px">
<xsl:text> - </xsl:text>
<xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</xsl:text>
<xsl:text>Page 1 of 1</xsl:text>
</fo:inline>
<fo:inline font-family="Courier" font-size="10px">
<xsl:value-of select="$My XPath to varaible" />
</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell
font-size="inherited-property-value(&apos;font-size&apos;) - 2pt"
text-align="right" padding-top="0.00000in" padding-bottom="0.00000in"
padding-left="0.00000in" padding-right="0.00000in">
<fo:block padding-top="1pt" padding-bottom="1pt" />
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:block>
</fo:static-content>
</xsl:template>

我将用变量替换字符串(第 1 页,共 1 页),但如何处理。

请帮我。

4

2 回答 2

28

我按照以下说明解决了我的问题。

在区域末尾放置一个带有 id 的格式化对象。然后,您可以对文档最后一页上显示的标记块执行操作。以下是标记的外观:

<fo:flow flow-name="xsl-region-body">
... Lots and lots of content here
<fo:block id="TheVeryLastPage"> </fo:block>
</fo:flow>

代码创建了一个 id 为 TheVeryLastPage(任何人都不太可能使用的值)的块,现在您可以参考该 id 来获取文档最后一页的页码。以下是该区域内容的外观:

<fo:block text-align="end">
Page <fo:page-number/> of 
<fo:page-number-citation 
ref-id="TheVeryLastPage"/>
</fo:block>

当 FOP 格式化此标记时,它会生成类似于“第 2 页,共 5 页”的内容。

我的参考网址是:http ://www.ibm.com/developerworks/xml/tutorials/x-xslfo2/section4.html

于 2013-10-09T13:14:39.467 回答
8

您应该为元素添加一个id属性fo:page-sequence,然后使用page-number-citation-last.

<fo:page-sequence id="my-sequence-id">
  ...
  <xsl:text>Page </xsl:text>
  <fo:page-number-citation />
  <xsl:text> of </xsl:text>
  <fo:page-number-citation-last page-citation-strategy="all" ref-id="my-sequence-id"/>
  ...
</fo:page-sequence>

请参阅规格:http ://www.w3.org/TR/xslfo20/#fo_page-number-citation和http://www.w3.org/TR/xslfo20/#fo_page-number-citation-last

于 2013-10-09T10:23:09.230 回答