unexpected result when comparing a string with an empty sequence
Added by Thomas Fischer almost 8 years ago
Hi, If I have a sequence of strings, where I have to check the equality of consecutive items.
. . .
When current() is 1 then I compare against an empty sequence @$v[0]@ My expectation was, that @'a' ne ( )@ would result in @true()@, but instead it returns an empty sequence. Only an explicit cast to string returns the expected result:
I wonder if somebody could explain why @ne@ and @eq@ operators can return something different from @boolean@ and why @'a' ne ()@ doesn't return @true@
salute Thomas
Replies (2)
RE: unexpected result when comparing a string with an empty sequence
-
Added by Michael Kay almost 8 years ago
Well the obvious answer is "because the spec says so". XPath §3.5.1 rule 2, "If the atomized operand is an empty sequence, the result of the value comparison is an empty sequence"
Why was it specified that way? Well, there were probably 25 people on the WG at the time, and you'd have to ask each of them - it's like speculating why the UK voted for Brexit, you will get 20 million different answers depending whom you ask.
But my guess/recollection would be that the WG was trying to do something a bit like the 3-valued logic of SQL: if either operand is null, return null.
To expand on this, //x[@z eq 3] is rather like the SQL query "select x from X where x.z = 3". In SQL, if z is null, the row won't be selected, and the same is true if the operator is != rather than =. Similarly in XPath, if the z attribute is absent, both the predicates [@z eq 3] and [@z ne 3] return (), which has an effective boolean value of false, meaning that the x element won't be selected.
RE: unexpected result when comparing a string with an empty sequence
-
Added by Thomas Fischer almost 8 years ago
Sorry for the obvious question and thanks for your nice explanation :)
Next time I swear to RTFM before :‑/
Please register to reply