string 在 XSLT 1.0 中将字符串转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1265886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:31:04  来源:igfitidea点击:

Convert String to Integer in XSLT 1.0

stringxsltint

提问by Kapil

I want to convert a string value in xslt to an integer value. I am using xslt 1.0, so i can't use those functions supported in xslt 2.0. Please help.

我想将 xslt 中的字符串值转换为整数值。我使用的是 xslt 1.0,所以我不能使用 xslt 2.0 支持的那些功能。请帮忙。

回答by jeffreypriebe

Adding to jelovirt's answer, you can use number() to convert the value to a number, then round(), floor(), or ceiling() to get a whole integer.

添加到 jelovirt 的答案中,您可以使用 number() 将值转换为数字,然后使用 round()、floor() 或天花板 () 来获得整数。

Example

例子

<xsl:variable name="MyValAsText" select="'5.14'"/>
<xsl:value-of select="number($MyValAsText) * 2"/> <!-- This outputs 10.28 -->
<xsl:value-of select="floor($MyValAsText)"/> <!-- outputs 5 -->
<xsl:value-of select="ceiling($MyValAsText)"/> <!-- outputs 6 -->
<xsl:value-of select="round($MyValAsText)"/> <!-- outputs 5 -->

回答by jelovirt

XSLT 1.0 does not have an integer data type, only double. You can use number()to convert a string to a number.

XSLT 1.0 没有整数数据类型,只有双精度。您可以使用number()将字符串转换为数字。