Why is $node/@attr != 'blue' false when $node is null?

Friday, 10 July 2015 · 3 messages
FB
France Baril10 Jul 2015 02:42 #
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
CG
Christian Grün10 Jul 2015 09:07 #
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
FB
France Baril10 Jul 2015 11:28 #
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 >
-- France Baril Architecte documentaire / Documentation architect france.baril at architextus.com

⚡Generated with XQuery