示例XML:
<ResultSet>
<WC>
<Id>1</Id>
</WC>
<WC>
<Id>2</Id>
</WC>
<WC>
<Id>3</Id>
</WC>
</ResultSet>
我试图从1开始页码,只计算每个WC元素的页数。
例如,如果第一个WC元素有2个页面,第一个页面是1/2,第二个是2/2,第二个WC元素有3个页面,它将从1开始计数,第一页将是1/3,依此类推。
这有可能实现吗?
我正在使用Apache FOP。
任何帮助将不胜感激。
编辑:
<xsl:stylesheet version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-height="279.5mm" page-width="216mm">
<fo:region-body region-name="xsl-region-body" margin="62mm 10.4mm 16mm 10.4mm" />
<fo:region-after region-name="xsl-region-after" extent="16mm"/>
<fo:region-start region-name="xsl-region-start" extent="10mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<xsl:for-each select="ResultSet">
<fo:page-sequence master-reference="A4" >
<fo:static-content flow-name="xsl-region-after" >
<fo:block font-size="9pt" font-weight="normal" font-family="Helvetica" margin="0mm 10.4mm 0mm 0mm"></fo:block>
<fo:block>Page <fo:page-number/> of <fo:page-number-citation ref-id="last-page" /></fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-start">
<fo:block-container border="1px solid black" height="256mm" width="195mm" margin-top="6.5mm" margin-left="10mm">
<fo:block> </fo:block>
</fo:block-container>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block-container font-size="9pt" font-weight="normal" font-family="Helvetica">
<fo:block id="last-page"> </fo:block>
</fo:block-container>
</fo:flow>
</fo:page-sequence>
</xsl:for-each>
</fo:root>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:2)
基本上,您需要为每个<page-sequence>
元素开始一个新的WC
,这样您就可以从1重新开始页码编号。
然后,您需要每个数字引用的唯一标识符,以检索每个部分的页码。我假设可以使用Id
标记。
这应该可以完成这项工作(请注意,在某些情况下,FOP在检索最后页码时非常错误 - 像Antenna House Formatter这样的商业产品效果更好):
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-height="279.5mm" page-width="216mm">
<fo:region-body region-name="xsl-region-body" margin="16mm 10.4mm 16mm 10.4mm" />
<fo:region-after region-name="xsl-region-after" extent="16mm"/>
<fo:region-start region-name="xsl-region-start" extent="10mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<xsl:apply-templates />
</fo:root>
</xsl:template>
<xsl:template match="WC">
<xsl:variable name="id.wc">id_<xsl:value-of select="normalize-space(Id/text())" /></xsl:variable>
<fo:page-sequence master-reference="A4" initial-page-number="1">
<fo:static-content flow-name="xsl-region-after">
<fo:block font-size="9pt" font-weight="normal" font-family="Helvetica"
margin="0mm 10.4mm 0mm 0mm">
<fo:block>Page <fo:page-number/> of <fo:page-number-citation ref-id="{$id.wc}"
/></fo:block>
</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-start">
<fo:block-container border="1px solid black" height="256mm" width="195mm"
margin-top="6.5mm" margin-left="10mm">
<fo:block> </fo:block>
</fo:block-container>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block-container font-size="9pt" font-weight="normal" font-family="Helvetica">
<!-- Process the content of the WC here... -->
<xsl:apply-templates />
<!-- This empty block is used to retrieve the last page number of the current section -->
<fo:block id="{$id.wc}" />
</fo:block-container>
</fo:flow>
</fo:page-sequence>
</xsl:template>