查找实数和小数
我使用 xslt1.0 进行 html 到 xml 的转换。
<xsl:variable name="margin">0</xsl:variable>
<xsl:if test="number($margin)">
<xsl:message>Its number</xsl:message>
</xsl:if>
这是我的模板,它满足条件并显示消息。如果变量边距的值为0.5,则条件变为假。
如何编写也匹配小数的条件?
I am using xslt1.0 for my html to xml conversion.
<xsl:variable name="margin">0</xsl:variable>
<xsl:if test="number($margin)">
<xsl:message>Its number</xsl:message>
</xsl:if>
This is my template which satisfying the condition and displaying message.If the variable margin's value is 0.5, then the condition becomes false.
How to write the condition which matches for decimals too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这根本不是真的。使用 9 个不同的 xslt 处理器(XSLT 1.0 和 2.0)进行测试,上面的代码不会产生任何消息。另一方面,任何不同于 0 的数值都会产生消息。
解释:
由于
number(0)
为0
,而boolean(0)
为false()
,测试不成功。解决方案:
使用此 XPath 表达式:
的计算结果恰好为
true()
,然后它相当于:当
$x
是数字时,此表达式$x
不是数字,上面的 XPath 表达式的计算结果为false()
,因为它相当于:并且根据定义
NaN
不等于任何其他值,包括 NaN 本身。This is simply not true. Tested with 9 different xslt processors (both XSLT 1.0 and 2.0), the above code doesn't produce any message. On the other side, Any numeric value, different from 0 produces the message.
Explanation:
Due to the fact that
number(0)
is0
, andboolean(0)
isfalse()
, the test doesn't succeed .Solution:
Use this XPath expression:
This expression evaluates to
true()
exactly when$x
is a number and then it is equivalent to:When
$x
is not a number, the XPath expression above evaluates tofalse()
because it is equivalent to:and by definition
NaN
is not equal to any other value, includingNaN
itself.我尝试了您的代码(使用 MSXSL.EXE,它需要在消息上终止=“yes”才能查看其输出)并发现,正如我所料,事实恰恰相反。当 $margin 为零时,“if”不会发生,但如果它非零(例如,0.5),则“if”执行。这是因为测试表达式使用与“boolean()”函数相同的语义进行计算,该函数将数值零解释为 false,将任何非零数值解释为 true。
I tried your code (using MSXSL.EXE, which requires terminate="yes" on the message in order to see its output) and found, as I expected, the opposite to be true. When $margin is zero, the "if" doesn't happen, but if it is non-zero (e.g., 0.5), the "if" executes. This is because the test expression is evaluated with the same semantics as the "boolean()" function, which interprets the numeric value zero as false and any non-zero numeric value as true.