将字符串类型编号转换为int编号,并在XSLT 1.0中汇总

时间:2021-01-19 16:31:41

I have below XML,

我有以下XML,

    <Answers>
     <Entry key="total" type="System.String">
      <value>50,000</value>
     </Entry>
    </Answers>
    <Answers>
     <Entry key="total" type="System.String">
      <value>2,000</value>
     </Entry>
    </Answers>
    <PerPersonTotal>1000</PerPersonTotal>

In XSLT 1.0 i have to sum up all the values and calculate percentage.

在XSLT 1.0中,我必须总结所有值并计算百分比。

Note that the amount value has comma.

请注意,金额值为逗号。

<xsl:value-of select="(PerPersonTotal div sum(number(Answers/Entry[@key='total']/value))) * 100

and am getting below error

并且我正在低于错误

Argument 1 of function 'sum()' cannot be converted to a node-set.

函数'sum()'的参数1无法转换为节点集。

if i remove number function i am getting NaN as result.

如果我删除数字函数我得到NaN作为结果。

1 个解决方案

#1


0  

Note that the amount value has comma.

请注意,金额值为逗号。

And that's exactly the problem here: a string containing a comma is not a number. To solve this in XSLT 1.0, you will need to do something like:

这就是问题所在:包含逗号的字符串不是数字。要在XSLT 1.0中解决这个问题,您需要执行以下操作:

<xsl:variable name="values">
    <xsl:for-each select="Answers/Entry[@key='total']">
        <value>
            <xsl:value-of select="translate(value, ',', '')"/>
        </value>
    </xsl:for-each>
</xsl:variable>
<result>
    <xsl:value-of select="PerPersonTotal div sum(exsl:node-set($values)/value)"/>
</result>

after declaring xmlns:exsl="http://exslt.org/common" - see: http://exslt.org/exsl/index.html

声明xmlns之后:exsl =“http://exslt.org/common” - 请参阅:http://exslt.org/exsl/index.html

Note:

  1. This assumes the comma here is used as a thousands separator;
  2. 这假设这里的逗号用作千位分隔符;

  3. To format a number as percentage, you should use the format-number() function, not multiply by 100.
  4. 要将数字格式化为百分比,您应该使用format-number()函数,而不是乘以100。

#1


0  

Note that the amount value has comma.

请注意,金额值为逗号。

And that's exactly the problem here: a string containing a comma is not a number. To solve this in XSLT 1.0, you will need to do something like:

这就是问题所在:包含逗号的字符串不是数字。要在XSLT 1.0中解决这个问题,您需要执行以下操作:

<xsl:variable name="values">
    <xsl:for-each select="Answers/Entry[@key='total']">
        <value>
            <xsl:value-of select="translate(value, ',', '')"/>
        </value>
    </xsl:for-each>
</xsl:variable>
<result>
    <xsl:value-of select="PerPersonTotal div sum(exsl:node-set($values)/value)"/>
</result>

after declaring xmlns:exsl="http://exslt.org/common" - see: http://exslt.org/exsl/index.html

声明xmlns之后:exsl =“http://exslt.org/common” - 请参阅:http://exslt.org/exsl/index.html

Note:

  1. This assumes the comma here is used as a thousands separator;
  2. 这假设这里的逗号用作千位分隔符;

  3. To format a number as percentage, you should use the format-number() function, not multiply by 100.
  4. 要将数字格式化为百分比,您应该使用format-number()函数,而不是乘以100。