How to transform string equation to number?

前端 未结 2 1941
情书的邮戳
情书的邮戳 2021-01-26 12:03

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 2

2条回答
  •  醉梦人生
    2021-01-26 12:09

    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.

提交回复
热议问题