Why is $node/@attr != 'blue' false when $node is null?
The code below returns 'blue', but I was expecting 'not blue'. The class attribute on an empty node is null; null != blue.
let $node := ()
return
if($node/@class != 'blue')
then 'not blue'
else 'blue'Hi France,
The semantics of general comparisons in XQuery (==, != etc.) is "existential". Each item of a sequence is compared against each item of the other sequence. If a single test returns true, the final result is true (and the remaining comparisons are usually skipped). Otherwise, the result if false. If one of the sequences is empty, there is nothing to compare, and the result will always be false:
1 = 1 → true (2,3) = (1,2) → true 1 = () → false () = () → false () != () → false
Inverting the operator, and inverting the if branches or using the not() function helps you out:
let $node := ()
return
if(not($node/@class = 'blue'))
then 'not blue'
else 'blue'Best, Christian
Quoted text · France Baril · 14 lines
> The code below returns 'blue', but I was expecting 'not blue'. The class > attribute on an empty node is null; null != blue. > > let $node := () > return > if($node/@class != 'blue') > then 'not blue' > else 'blue' > > > -- > France Baril > Architecte documentaire / Documentation architect > france.baril at architextus.com
Hi,
My work around was (not($node) or $node !='blue'), but yours is more elegant.
Thanks!
Quoted text · Christian Grün · 45 lines
On Fri, Jul 10, 2015 at 12:07 AM, Christian Grün <christian.gruen at gmail.com> wrote: > Hi France, > > The semantics of general comparisons in XQuery (==, != etc.) is > "existential". Each item of a sequence is compared against each item > of the other sequence. If a single test returns true, the final result > is true (and the remaining comparisons are usually skipped). > Otherwise, the result if false. If one of the sequences is empty, > there is nothing to compare, and the result will always be false: > > 1 = 1 → true > (2,3) = (1,2) → true > 1 = () → false > () = () → false > () != () → false > > Inverting the operator, and inverting the if branches or using the > not() function helps you out: > > let $node := () > return > if(not($node/@class = 'blue')) > then 'not blue' > else 'blue' > > Best, > Christian > > > > The code below returns 'blue', but I was expecting 'not blue'. The class > > attribute on an empty node is null; null != blue. > > > > let $node := () > > return > > if($node/@class != 'blue') > > then 'not blue' > > else 'blue' > > > > > > -- > > France Baril > > Architecte documentaire / Documentation architect > > france.baril at architextus.com >
Written for BaseX 8.2. BaseX may have changed since: please check the current documentation.