How to transform string equation to number?

这一生的挚爱 提交于 2019-12-02 22:08:00

问题


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

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

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

input: 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>

output: 25,39.6

Thank you in advanced.


回答1:


I would suggest you try it this way:

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>



回答2:


If the string contains a valid XPath expression (as it does in your example) then you need the ability to dynamically evaluate XPath expressions. This is not a standard feature of XSLT 1.0, however:

  • It's available in XSLT 3.0 with the xsl:evaluate instruction
  • It's available in many XSLT processors as a vendor extension (typically named xx:eval() or xx:evaluate()
  • You might be able to implement it as an extension function yourself using the mechanisms provided by your chosen processor for calling out to extensions.

Alternatively, if you know that the string contains a sequence of numbers separated by plus signs, then you could write a recursive template to extract the tokens, convert them to numbers, and sum them. Or if it's always two numbers, then you don't even need recursion.

As so often happens, it's not enough to have one example of the input your program has to handle; we need to know what the set of all possible inputs is.



来源:https://stackoverflow.com/questions/40843119/how-to-transform-string-equation-to-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!