如何创建布尔值?
我对 XSLT 完全陌生,无法弄清楚以下代码哪里出了问题。
<xsl:variable name="var" select="boolean('false')"/>
<xsl:if test="$var'">variable is true</xsl:if>
当它本应为假时,它总是返回真。 为什么?
I am totally new to XSLT and can't work out where I am going wrong with the following code.
<xsl:variable name="var" select="boolean('false')"/>
<xsl:if test="$var'">variable is true</xsl:if>
It is always returning true when it is meant to be false. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$var 变量的值定义如下:
是
true()
这是因为在 XPath 中“
false
”是一个普通字符串,与false() 相反
,它是boolean
值false()
的构造函数XPath 中的两个布尔值是(注意它们是构造的!):
true()
和false()
转换任何值的详细信息布尔值在 XPath 规范 中详细说明:
“boolean 函数将其参数转换为布尔值,如下所示:
一个数字为真当且仅当
既不是正零也不是负零
也不是 NaN
节点集为真当且仅当
非空
字符串为真当且仅当其
长度非零
四种基本类型以外的类型的对象以依赖于该类型的方式转换为布尔值
"
在您的情况下,字符串“false”不是数字 0 并且具有正长度,因此应用上面第三个项目符号中的规则,产生
true()
。因此,要定义XSLT 1.0 中的变量,其值为
false( )
,定义如下:或者,如果您不太记得这一点,您可以随时编写:
(指定任何计算结果为
false()
的表达式),XSLT 处理器将为您完成工作在 XSLT 2.0 中,最好显式指定类型 。变量的 :
The value of the $var variable as defined in:
<xsl:variable name="var" select="boolean('false')"/>
is
true()
This is because in XPath "
false
" is an ordinary string, as opposed tofalse()
, which is the constructor for theboolean
valuefalse()
The two boolean values in XPath are (note that they are constructed!):
true()
andfalse()
The detail of converting any value to boolean are spelled outin the XPath Spec.:
"The boolean function converts its argument to a boolean as follows:
a number is true if and only if it
is neither positive or negative zero
nor NaN
a node-set is true if and only if it
is non-empty
a string is true if and only if its
length is non-zero
an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type
"
In your case the string "false" is not the number 0 and has a positive length, so the rule in the 3rd bullet above is applied, yielding
true()
.Therefore, to define a variable in XSLT 1.0, whose value is
false()
, one needs to write the definition as the following:<xsl:variable name="vMyVar" select="false()"/>
or, if you don't exactly remember this, you could always write:
<xsl:variable name="vMyVar" select="1 = 0"/>
(specify any expression that evaluates to
false()
) and the XSLT processor will do the work for you.In XSLT 2.0 it is always better to explicitly specify the type of the variable:
<xsl:variable name="vMyVar" as="xs:boolean" select="false()"/>
也许在这个阶段有点晚了,但在我看来,处理布尔值是不值得的。 这是我处理从数据库返回的布尔值(强制)的方式:
希望这对某人有帮助
A bit late at this stage perhaps but imo dealing with booleans is just not worth the effort. Heres how I dealt with a boolean (Mandatory) coming back from the DB:
Hope this helps someone
您正在使用的 boolean() 函数确实在完成它的工作。 要使用明确的 true 和 false 值,您应该使用以下函数:
仅供参考,根据 MSDN 文档,boolean() 返回以下内容:
The boolean() function you are using is indeed doing it's job. For using explicit true and false values you should use the following functions:
Just FYI, per the MSDN documentation, boolean() returns the following: