SQL Server 2005 中 xs:boolean 的比较运算

发布于 2024-09-27 13:11:47 字数 564 浏览 0 评论 0原文

谁能向我解释一下这个查询的结果:

declare @xml xml;
set @xml = '<node attribute="true">Val</node>';

select
    T.c.query('xs:boolean(@attribute[1])') Value,
    T.c.query('xs:boolean(@attribute[1]) = false') ValueEqualsFalse,
    T.c.query('xs:boolean(@attribute[1]) = true') ValueEqualsTrue,
    T.c.query('xs:boolean(@attribute[1]) != false') ValueNotEqualsFalse,
    T.c.query('xs:boolean(@attribute[1]) != true') ValueNotEqualsTrue
from @Xml.nodes('node') T(c);

第一列 Value 返回 true。其余全部返回 false。那么,在设法将值转换为正确的类型后,我如何实际检查它的值呢?

Can anyone explain to me the results of this query:

declare @xml xml;
set @xml = '<node attribute="true">Val</node>';

select
    T.c.query('xs:boolean(@attribute[1])') Value,
    T.c.query('xs:boolean(@attribute[1]) = false') ValueEqualsFalse,
    T.c.query('xs:boolean(@attribute[1]) = true') ValueEqualsTrue,
    T.c.query('xs:boolean(@attribute[1]) != false') ValueNotEqualsFalse,
    T.c.query('xs:boolean(@attribute[1]) != true') ValueNotEqualsTrue
from @Xml.nodes('node') T(c);

The first column, Value, returns true. The rest all return false. So having managed to cast the value to the correct type, how do I actually check it's value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转瞬即逝 2024-10-04 13:11:47

您需要使用 fn:false()false()fn:true()true() > 而不是只写 truefalse

这是一个正确的例子:

T.c.query('xs:boolean(@attribute[1]) = false()') ValueEqualsFalse,
T.c.query('xs:boolean(@attribute[1]) = true()') ValueEqualsTrue,
T.c.query('xs:boolean(@attribute[1]) != false()') ValueNotEqualsFalse,
T.c.query('xs:boolean(@attribute[1]) != true()') ValueNotEqualsTrue

如果不使用函数false()false会被当作路径表达式来处理,即处理器认为您正在查询 元素。因此,只存在常量 truefalse 的函数,因为这是区分布尔常量和路径表达式的唯一方法。

更详细地说,在每个示例中使用否定仍然会返回 false

这不是你想要的(只是为了演示):

T.c.query('not(xs:boolean(@attribute[1])) = false') ValueEqualsFalse,
T.c.query('not(xs:boolean(@attribute[1])) = true') ValueEqualsTrue,
T.c.query('not(xs:boolean(@attribute[1])) != false') ValueNotEqualsFalse,
T.c.query('not(xs:boolean(@attribute[1])) != true') ValueNotEqualsTrue

文字 falsetrue 都被评估为一个空序列,与布尔值都不匹配值 false() 也不是布尔值 true()

you need to use fn:false() or false() and fn:true() or true() instead of just writing true or false.

This is a correct example:

T.c.query('xs:boolean(@attribute[1]) = false()') ValueEqualsFalse,
T.c.query('xs:boolean(@attribute[1]) = true()') ValueEqualsTrue,
T.c.query('xs:boolean(@attribute[1]) != false()') ValueNotEqualsFalse,
T.c.query('xs:boolean(@attribute[1]) != true()') ValueNotEqualsTrue

If you don't use the function false(), false is processed as a path expression, i.e. the processor thinks you are querying for a <false /> element. Therefore, there only exists a function for the constants true and false, because this is the only way to distinguish between the boolean constants and a path expression.

More in detail, using the negation would still return false in each example.

This is not what you want (just to demonstrate):

T.c.query('not(xs:boolean(@attribute[1])) = false') ValueEqualsFalse,
T.c.query('not(xs:boolean(@attribute[1])) = true') ValueEqualsTrue,
T.c.query('not(xs:boolean(@attribute[1])) != false') ValueNotEqualsFalse,
T.c.query('not(xs:boolean(@attribute[1])) != true') ValueNotEqualsTrue

the literals false and true both are evaluated to an empty sequence which neither matches the boolean value false() nor the boolean value true().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文