源XML:
<Root>
<Data>
<Value>10</Value>
</Data>
<Data>
<Value>10</Value>
</Data>
<Data>
<Value>15</Value>
</Data>
<Data>
<Value>2</Value>
</Data>
<Data>
<Value>32</Value>
</Data>
<Data>
<Value>5</Value>
</Data>
<Data>
<Value>40</Value>
</Data>
<Data>
<Value>18</Value>
</Data>
<Data>
<Value>50</Value>
</Data>
<Data>
<Value>1</Value>
</Data>
<Data>
<Value>43</Value>
</Data>
<Data>
<Value>5</Value>
</Data>
<Data>
<Value>21</Value>
</Data>
<Data>
<Value>87</Value>
</Data>
<Data>
<Value>8</Value>
</Data>
....
</Root>XSL-FO代码:我的代码包含一个表行和一个表脚。
<fo:table>
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-footer>
<fo:table-row>
<fo:table-cell>
<fo:block>Subtotal: </fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<fo:retrieve-table-marker retrieve-class-name="Subtotal" retrieve-position-within-table="last-starting"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-footer>
<fo:table-body>
<xsl:for-each select="/Root/Data">
<fo:table-row>
<fo:table-cell>
<fo:block>Value:</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="Value"/>
<fo:marker marker-class-name="Subtotal">
<xsl:variable name="position" select="position()"/>
<xsl:value-of xpath="sum(../Data[position() <= $position]/Value)"/>
</fo:marker>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>我正在从上表中生成PDF输出。在PDF中,该表每页最多有15行表。在每个页面上,都需要通过只汇总当前页上显示的值来计算小计。
上面的表(我找到的唯一解决方案是使用表标记)计算Page1的正确小计,但Page2失败等等。它将以前所有页面的小计附加到当前页面。
例如:如果显示在第1页上的值元素小计为10,而在第2页上显示的值小计小计为20,则我的表将显示在Page1上,小计为10,但在Page2上,小计将为30,而不是20。有人能帮助解决这个问题吗?
提前谢谢
发布于 2021-12-07 12:47:15
在XSLT 1中使用“位置分组”,如
<xsl:for-each select="Root/Data[position() mod 15 = 1]">
<xsl:variable name="data-of-page" select=". | following-sibling::Data[position() < 15]"/>
<xsl:for-each select="$data-of-page">
<fo:table-row>
<fo:table-cell>
<fo:block>Value:</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="Value"/>
<xsl:if test="position() = last()">
<fo:marker marker-class-name="Subtotal">
<xsl:value-of xpath="sum($data-of-page/Value)"/>
</fo:marker>
</xsl:if>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</xsl:for-each>发布于 2021-12-08 10:31:58
该版本的唯一优点是,如果第一页的行数少于15行,它将给出正确的小计;例如,如果在表开始前有标题或文本。
<xsl:stylesheet
version="1.0"
xmlns:axf="http://www.antennahouse.com/names/XSL/Extensions"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" />
<xsl:variable name="rows-per-page" select="15" />
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:axf="http://www.antennahouse.com/names/XSL/Extensions"
xml:lang="en">
<fo:layout-master-set>
<fo:simple-page-master master-name="spm" size="A7">
<fo:region-body margin="20mm" margin-top="15mm" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="spm">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates />
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="Root">
<fo:block />
<fo:table space-before="3lh">
<fo:table-column column-width="50%"/>
<fo:table-column column-width="50%"/>
<fo:table-footer>
<fo:table-row>
<fo:table-cell>
<fo:block>Subtotal: </fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block-container>
<fo:retrieve-table-marker retrieve-class-name="Remainder" retrieve-position-within-table="first-starting"/>
<fo:retrieve-table-marker retrieve-class-name="Subtotal" retrieve-position-within-table="last-starting"/>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
</fo:table-footer>
<fo:table-body>
<xsl:apply-templates />
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="Data">
<fo:table-row>
<fo:table-cell>
<fo:block>Value:</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<fo:marker marker-class-name="Subtotal">
<xsl:if test="position() != last()">
<fo:block-container width="4em" background-color="white">
<fo:block>
<xsl:value-of
select="sum(preceding-sibling::Data[position() < $rows-per-page]) + Value"/>
</fo:block>
</fo:block-container>
</xsl:if>
</fo:marker>
<fo:marker marker-class-name="Remainder">
<fo:block-container width="4em" z-index="-1" absolute-position="absolute">
<fo:block>
<xsl:value-of
select="sum(following-sibling::Data) + Value"/>
</fo:block>
</fo:block-container>
</fo:marker>
<xsl:value-of select="Value"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/70250383
复制相似问题