如何在 XSLT 中添加时间?

发布于 2024-08-08 17:56:41 字数 1135 浏览 6 评论 0原文

我有一些来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒儿 2024-08-15 17:56:41

使用 XSLT 的 translate 函数,如下所示:

sum(translate(//testsuite/@time,',',''))

Use XSLT's translate function like this:

sum(translate(//testsuite/@time,',',''))

七颜 2024-08-15 17:56:41

使用此递归模板将所有时间属性添加在一起:

<xsl:template name="sumoftime">
  <xsl:param name="node"/>
  <xsl:param name="sum" />
  <xsl:choose>
    <xsl:when test="$node">
      <xsl:call-template name="sumoftime">
        <xsl:with-param name="node" select="$node/following-sibling::testcase[1]"/>
        <xsl:with-param name="sum" select="$sum + number(translate($node/@time, ',', ''))"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$sum"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

然后在您的 testsuite 元素中,调用上述模板

<xsl:element name="testsuite">

            ...

    <xsl:attribute name="{'time'}">
        <xsl:call-template name="sumoftime">
            <xsl:with-param name="node" select="testcase[1]"/>
            <xsl:with-param name="sum" select="0" />
        </xsl:call-template>
    </xsl:attribute>
</xsl:element>

相当复杂,但如果您仅限于 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:

<xsl:template name="sumoftime">
  <xsl:param name="node"/>
  <xsl:param name="sum" />
  <xsl:choose>
    <xsl:when test="$node">
      <xsl:call-template name="sumoftime">
        <xsl:with-param name="node" select="$node/following-sibling::testcase[1]"/>
        <xsl:with-param name="sum" select="$sum + number(translate($node/@time, ',', ''))"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$sum"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

And then in your testsuite element, make a call to the above template

<xsl:element name="testsuite">

            ...

    <xsl:attribute name="{'time'}">
        <xsl:call-template name="sumoftime">
            <xsl:with-param name="node" select="testcase[1]"/>
            <xsl:with-param name="sum" select="0" />
        </xsl:call-template>
    </xsl:attribute>
</xsl:element>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文