在 XQuery/XSLT/XPath 中格式化 sum() 函数的输出 [英] Formatting the output of the sum() function in XQuery/XSLT/XPath
问题描述
我有一个关于在 XQuery/XSLT/XPath 中格式化 sum()
函数的输出的问题.我有一个 XSLT 样式表和一个 XQuery.我能够在 XSLT 中获得正确的格式,但不能在 XQuery 中获得正确的格式.
I have a question about formatting the output of the sum()
function in XQuery/XSLT/XPath. I have both an XSLT stylesheet and also an XQuery. I'm able to get the correct formatting in the XSLT, but not in the XQuery.
这是一个示例输入文件:
Here is an example input file:
<userTotals>
<user cost="138764.63" hours="1506.51"/>
<user cost="329555.76" hours="3577.85"/>
<user cost="213909.81" hours="2322.33"/>
<user cost="22684.85" hours="246.28"/>
<user cost="6147.42" hours="66.74"/>
<user cost="1269.27" hours="13.78"/>
<user cost="181.45" hours="1.97"/>
<user cost="755.30" hours="8.2"/>
</userTotals>
这是一个示例样式表:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/userTotals">
<results>
<cost><xsl:value-of select="sum(user/@cost)"/></cost>
<hours><xsl:value-of select="sum(user/@hours)"/></hours>
<hours-formatted>
<xsl:value-of select="format-number(sum(user/@hours),'###.##')"/>
</hours-formatted>
</results>
</xsl:template>
</xsl:stylesheet>
输出如下:
<results>
<cost>713268.49</cost>
<hours>7743.659999999999</hours>
<hours-formatted>7743.66</hours-formatted>
</results>
注意
和
的输出.
的内容是 sum()
未修改/未格式化的输出.
的内容是我希望在 XSLT 和 XQuery 中格式化数字的方式.
Notice the output of <hours>
and <hours-formatted>
. The content of <hours>
is the unmodified/unformatted output of sum()
. The content of <hours-formatted>
is the way I want the number formatted in both the XSLT and the XQuery.
问题是在 XQuery 中,format-number()
不可用.如何在 XSLT 和 XQuery 中以相同的方式格式化 sum()
的结果,以便结果看起来像
?
The problem is that in XQuery, format-number()
is not available. How can I format the results of sum()
the same way in both XSLT and XQuery so that the results will appear like <hours-formatted>
?
我是否需要创建一个将 sum()
的输出作为字符串并自己处理小数的函数?在这种情况下,我需要将 .659
舍入到 .66
.我希望有更好的方法来做到这一点(也许将结果转换为不同的类型然后 ??).
Will I need to create a function that takes the output of sum()
as a string and process the decimal myself? In this case I would need to round .659
to .66
. I'm hoping there is a better way to do this (maybe cast the results as a different type and then ??).
感谢您提供的任何信息/指导.
Thank you for any info/guidance you can provide.
推荐答案
Saxon-PE 及更高版本在 XQuery 中提供 format-number() 作为扩展功能(或通过启用 XQuery 3.0 支持).
Saxon-PE and upwards offers format-number() in XQuery as an extension function (or by enabling XQuery 3.0 support).
但是您可能可以使用 round-half-to-even() 函数实现您想要的效果.
But you can probably achieve what you want using the round-half-to-even() function.
这篇关于在 XQuery/XSLT/XPath 中格式化 sum() 函数的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!