XPath运算符“!=".它是如何工作的? [英] XPath operator "!=". How does it work?

查看:77
本文介绍了XPath运算符“!=".它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML文档:

<doc>
    <A>   
        <Node>Hello!</Node>   
    </A> 

    <B>     
        <Node/>
    </B>  

    <C>
    </C>

    <D/>
</doc>

您如何评估以下XPath查询?

How would you evaluate the following XPath queries?

/doc/A/Node != 'abcd'  
/doc/B/Node != 'abcd'  
/doc/C/Node != 'abcd'  
/doc/D/Node != 'abcd'  

我希望其中的 ALL 个评估为 true .

I would expect ALL of these to evaluate to true.

但是,结果如下:

/doc/A/Node != 'abcd'     true
/doc/B/Node != 'abcd'     true
/doc/C/Node != 'abcd'     false
/doc/D/Node != 'abcd'     false

这是预期的行为吗?还是我的XPath提供程序(jaxen)的错误?

Is this expected behavior? Or is it a bug with my XPath provider (jaxen)?

推荐答案

推荐:切勿使用!=运算符比较其中一个或两个参数均为节点集的不等式.

Recommendation: Never use the != operator to compare inequality where one or both arguments are node-sets.

通过定义表达式 strong>:

By definition the expression:

$node-set != $value

$node-set中至少有一个节点,使得其字符串值不等于$value的字符串值时,

精确地评估为true().

evaluates to true() exactly when there is at least one node in $node-set such that its string value is not equal to the string value of $value.

使用此定义:

$empty-nodeset != $value 

始终为false(),因为$empty-nodeset中甚至没有一个不等式成立的节点.

is always false(), because there isn't even a single node in $empty-nodeset for which the inequality holds.

解决方案:

使用:

not($node-set = $value)

然后您将根据需要获得所有结果true() .

Then you get all results true(), as wanted.

这篇关于XPath运算符“!=".它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆