XSLT 编号测试
我遇到一个问题,$current 变量的值是“6E144270003”。我最初的目标是只测试一个数字,但“6E144270003”通过了 number() 测试,因为它是一个有效的“科学记数法”(正如这里向我指出的那样)。
我需要一个有效的测试来允许仅包含整数(可以包括小数和减号)的数据等于 true,而任何其他数据等于 false。
应通:1234567890
应通过:123.45
应通过:123.5
应通过:-123.45
<xsl:if test="number($current) = number($current)">
<xsl:value-of select="$current"/>
</xsl:if>
I am having a problem where the value of the $current variable came in as '6E144270003'. My original goal was to just test for a number, but '6E144270003' passed the number() test because it is a valid 'scientific notation' (as was pointed out to me here).
I need a valid test to allow data containing only integers (can include the decimal and minus sign) to equate to true and any other data to equate to false.
Should pass: 1234567890
Should pass: 123.45
Should pass: 123.5
Should pass: -123.45
<xsl:if test="number($current) = number($current)">
<xsl:value-of select="$current"/>
</xsl:if>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法重现这个问题。
此转换:
当与 SAXON 9.0.0.4 一起运行时,会产生:
字符串
“6E144270003”
可以在XPath 2.0中用作数字,因为在XPath 2.0中所谓的“科学记数法”是表示数字的有效方式。这是一个有趣的示例,其中 XSLT 1.0 和 XSLT 2.0 的行为是不同的。
更新:OP 表示他想要一个测试,如果字符串包含除数字以外的任何内容,则该测试的结果将为
false()
。这可以通过正则表达式来最好地实现,甚至:
UPDATE2:OP再次改变了他的问题!
对于最新的问题,这里有答案:
使用:
此转换演示了此方法的正确性:
当此转换应用于任何 XML 文档时(未使用) ,产生正确的结果:
I cannot repro this problem.
This transformation:
when run with SAXON 9.0.0.4, produces:
The string
"6E144270003"
can be used as a number in XPath 2.0, because in XPath 2.0 the so called *scientific notation` is a valid way to represent a number.This is one interesting example where the behavior of XSLT 1.0 and XSLT 2.0 is different.
UPDATE: The OP has indicated that he wants a test that will evaluate to
false()
if the string contains anything but digits.This can be achieved best by a regular expression, or even:
UPDATE2: The OP changed his question again!!!
For the latest question here is the answer:
Use:
This transformation demonstrates the correctness of this method:
when this transformation is applied on any XML document (not used), the correct result is produced: