Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

equals - XPath operator "!=". How does it work?

XML document:

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

    <B>     
        <Node/>
    </B>  

    <C>
    </C>

    <D/>
</doc>

How would you evaluate the following XPath queries?

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

I would expect ALL of these to evaluate to true.

However, here are the results:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

By definition the expression:

$node-set != $value

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.

Using this definition:

$empty-nodeset != $value 

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

Solution:

Use:

not($node-set = $value)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...