如何在 XSLT 中添加时间?
我有一些来自 3rd 方软件的输出:
来自软件的示例 XML:
<testsuite name="XYZ">
<testcase name="ABC" status="0" time="12.001">Some stuff</testcase>
<testcase name="DEF" status="0" time="12,345.001">Some stuff</testcase>
<testcase name="GHI" status="0" time="4,321.001">Some stuff</testcase>
</testsuite>
我需要编写一个 XSLT 将其转换为以下内容:
<testsuite name="XYZ" time="16678.003">
<testcase name="ABC" time="12.001">Some stuff</testcase>
<testcase name="DEF" time="12,345.001">Some stuff</testcase>
<testcase name="GHI" time="4,321.001">Some stuff</testcase>
</testsuite>
我几乎已经到达那里,除了 time 属性testsuite 元素。我得到的不是总数,而是 NaN。 我用来获取此值的 XPath 表达式是 sum(//testsuite/@time)
请注意,当所有时间都 sum(//testsuite/@time) 时,不会发生错误。 1000。这可能是因为 XSLT 在遇到逗号时不解析该数字。 (我无法从输入中删除这些逗号,因为它来自第 3 方软件。)
那么如何对这些时间值求和呢?是否可以修改 sum(//testsuite/@time) 以便能够动态删除逗号?
谢谢!
I have some output from 3rd party software:
Sample XML from the software:
<testsuite name="XYZ">
<testcase name="ABC" status="0" time="12.001">Some stuff</testcase>
<testcase name="DEF" status="0" time="12,345.001">Some stuff</testcase>
<testcase name="GHI" status="0" time="4,321.001">Some stuff</testcase>
</testsuite>
I need to write an XSLT that transforms this into the following:
<testsuite name="XYZ" time="16678.003">
<testcase name="ABC" time="12.001">Some stuff</testcase>
<testcase name="DEF" time="12,345.001">Some stuff</testcase>
<testcase name="GHI" time="4,321.001">Some stuff</testcase>
</testsuite>
And I have almost got there, with the exception of the time attribute of the testsuite element. Instead of getting the total, I am getting NaN.
The XPath expression I am using to get this is sum(//testsuite/@time)
Note that the error does not occur when there all the times are < 1000. This is probably because XSLT doesn't parse the number when it encounters commas. (I cannot get rid of these commas from the input because it comes from 3rd party software.)
So how do I sum these values for time? Is there away to modify sum(//testsuite/@time) such that it is able to strip commas on the fly?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 XSLT 的
translate
函数,如下所示:Use XSLT's
translate
function like this:使用此递归模板将所有时间属性添加在一起:
然后在您的 testsuite 元素中,调用上述模板
相当复杂,但如果您仅限于 XSLT 1.0,我认为这将是可行的方法。
该模板的功劳应该归功于 Jelovirt,因为它只是他对另一个问题的回答的一个轻微修改:stackoverflow.com/questions/647991/summing-numbers-with-comma-as-decimal-separator-in-xslt
Use this recursive template to add all the time attributes together:
And then in your testsuite element, make a call to the above template
Rather complicated, but if you're restricted to XSLT 1.0, I reckon this'd be the way to go.
Credit for the template should go to Jelovirt, as it is merely a slight mod from his answer to another question: stackoverflow.com/questions/647991/summing-numbers-with-comma-as-decimal-separator-in-xslt