【发布时间】:2021-01-15 04:03:10
【问题描述】:
我正在使用 Apache FOP 生成 PDF 文档,为了显示某个值,我必须遍历多个节点以确定 total price 值,然后对该值求和。到目前为止,我有一个迭代数组然后检索预期值的函数,但是当我尝试对结果求和时会出现问题。
<xsl:function name="foo:buildTotalValue">
<xsl:param name="items" />
<xsl:variable name="totals">
<xsl:for-each select="$items/charge">
<xsl:call-template name="getTotalPriceNode">
<xsl:with-param name="itemParam" select="." />
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum(exsl:node-set($totals))" />
</xsl:function>
<xsl:template name="getTotalPriceNode">
<xsl:param name="itemParam" />
<xsl:choose>
<xsl:when test="$itemParam/Recurrance = 'OnceOff'">
<xsl:value-of select="$itemParam/TotalValue" />
</xsl:when>
<xsl:when test="$itemParam/Recurrance = 'Monthly'">
<xsl:value-of select="$itemParam/TotalValue * $itemParam/Months"/>
</xsl:when>
<xsl:otherwise><xsl:value-of select="0" /></xsl:otherwise>
</xsl:choose>
</xsl:template>
I'm hoping that when I pass in foo:buildTotalValue with entries like this:
<Charges>
<Charge>
<Recurrance>OnceOff</Recurrance>
<TotalValue>50.00</TotalValue>
</Charge>
<Charge>
<Recurrance>Monthly</Recurrance>
<TotalValue>10.00</TotalValue>
<Months>6</Months>
</Charge>
</Charges>
将返回值 110.00,但我得到了错误:
Cannot convert string "50.0060.00" to double
我尝试在模板中添加<value> 或其他内容,然后将其用作exsl:node-set 函数的选择器,但似乎没有什么不同。
【问题讨论】:
-
您使用的是哪个处理器?您已将此标记为
xslt-1.0,但xsl:function需要 XSLT 2.0+。 OTOH,XSLT 2.0 处理器不需要exsl:node-set()。所以你有一个大杂烩的版本。同样,对于同一个任务,不需要有一个函数和一个命名模板。 -
您确定使用 XSLT 1 处理器吗?
xsl:function仅在 XSLT 2 及更高版本中受支持,您只需使用 XPath 2/3 的表达能力,例如sum(Charge[Recurrance = 'OnceOff']/TotalValue | Charge[Recurrance = 'Monthly']/(TotalValue * Months))你根本不需要任何迭代或函数。 -
我的错误,对版本控制感到困惑。 Apache Fop 2.2,它支持 xsl-1.1。对于实际的转换,我们使用的是支持 xslt 3.0 的 Saxon 9.8
标签: xslt apache-fop