Re: [xsl] Problem with "except" operator

Subject: Re: [xsl] Problem with "except" operator
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 11 Feb 2009 10:19:24 -0500
At 2009-02-11 15:06 +0000, Andrew Welch wrote:
>> So the above are equivalent.
>
> I think not, Andrew ... Martin is correct.

yep... I've kind of blogged about this a while back:

http://ajwelch.blogspot.com/2007/08/when-b-and-b-both-return-true.html

...but it still always catches me out.

The rule I mention there is "never use != where both sides are
sequences of more than one item" which is why I thought ". != x" would
be ok... but it looks like that should be:

"never use != where one side is a sequence of more than one item"

How about "never use != where one side in a sequence could be empty" instead? Because the same problem will exist if one of the two arguments is absent. In this case there is no difference if the address is for a singleton or a set, if the result is empty, the comparison will be false, which may not be what you want.


For example:

<xsl:if test="value != 3">

... even when there may only be one <value> child, will still be false if that one child is absent. The stylesheet writer may want it to be true because there is no value that is equal to 3, which is to say:

<xsl:if test="not( value = 3 )">

In XSLT 2 I've heard some pundits say:

  Never use "=" because of this problem, always use "eq" and "ne" when
  dealing with singletons and always use "some ... in" and "every ... in"
  when dealing with sets.

... but I'm still a fan of "=" and "!=", even if I mistype when rushing a volunteer answer on a mail list. You aren't the only one who gets caught out.

I hope this helps.

. . . . . . . . . . Ken

--
Upcoming hands-on XSLT, UBL & code list hands-on training classes:
Brussels, BE 2009-03;  Prague, CZ 2009-03, http://www.xmlprague.cz
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread