如何将字符串方程转换为数字? [英] How to transform string equation to number?

查看:39
本文介绍了如何将字符串方程转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为以下每个字段生成了一个:214.2+428.4+结束

I generated by a for each the following field: 214.2+428.4+end

我使用过 substring-before(prices,'+end') 但这是一个字符串.

I have used substring-before(prices,'+end') but this is a string.

有什么想法可以对 214.2+428.4 进行总结吗?

Any ideas how I can take the 214.2+428.4 and sum it up?

输入:xml:

<items>
  <item>
    <price>12.50</price>
    <quantity>2</quantity>
  </item>
  <item>
    <price>13.20</price>
    <quantity>3</quantity>
  </item>
</items>

xsl:

<xsl:variable name="total"><xsl:for-each select="item"><xsl:value-of select="price*quantity"></xsl:value-of><xsl:text>+</xsl:text></xsl:for-each>end
</xsl:variable>

输出:25,39.6

先谢谢你.

推荐答案

我建议你这样试试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/items">
    <xsl:variable name="subtotals">
        <xsl:for-each select="item">
            <amount>
                <xsl:value-of select="price * quantity"/>
            </amount>
        </xsl:for-each>
    </xsl:variable>
    <total>
        <xsl:value-of select="sum(exsl:node-set($subtotals)/amount)"/>
    </total>
</xsl:template>

</xsl:stylesheet>

应用于您的示例输入,结果将是:

Applied to your example input, the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<total>64.6</total>

这篇关于如何将字符串方程转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆