Interesting query submitted by a Saxon-SA XQuery user yesterday. They were reporting a bug in validating QNames in element content, but the thing that interested me was that they had written a rather complex function to determine whether two atomic values were equal, without failing if the values aren't comparable.
If you test [@x eq 6], and @x is defined with a union type that allows both strings and numbers, then you'll get a type error if @x happens to contain a string. (With strict static typing, the query won't even compile). The same is true if you use the "=" operator. So this user had coded a complex set of nested typeswitch expressions to ensure that comparing two non-comparable values returned false rather than an error.
In fact there's an easier way. In fact, there are at least two easier ways. Both the following expressions achieve the required aim:
count(distinct-values((@x, @y)) eq 1
exists(index-of(@x, @y))
That's because in both distinct-values() and index-of(), we explicitly decided that non-comparable values should be treated as distinct, rather than causing an error.
Why the difference? It's classic symptom of committee design. For "eq" and "=", the advocates of strong typing won the argument, for distinct-values() and index-of() the weak typists (probably me in this case) swayed the vote. Perhaps it just depended on who was paying attention when the matter came up for discussion.
There are arguments both ways. There's even an argument for making different language features inconsistent with each other, so users can pick and choose! It doesn't feel like a very elegant piece of design, but it's nice to discover that there's a way of doing what's needed in this particular case.
|
|
||||||||
Comparability
Comments
Re: Comparability
by
Dimitre Novatchev
on Mon 20 Feb 2006 00:46 GMT | Profile | Permanent Link
Thanks for these "easy ways".
Why on Earth is it necessary to raise an error when comparing *for equality* items of incompatible type? If they are of incompatible type, this already means that there's *no* way they can be equal. It seems to me, that whoever imposed raising eror in this case tried to enforce a kind of religion on the developers using XPath. This is a specific example where static type-checking is overdone and clearly does not help (but obstructs). To what result? So that now we have to rely on word of mouth (sorry, Mike), even if this comes from Michael Kay himself, and subconsciously "feel guilty" -- not knowing even for what... :( Yes, having a sequence of incompatibly typed items is not a good practice, like having an array of Object, but maybe sometimes there are cases when we are forced to accept that we do not know the type in advance. XPath 2.0 does not have Haskell's "type class" facility, so one is forced to use item()* in this case or not to specify a type at all. It is good to have a backdoor and I thank Mike for achieving this. However, should I "feel guilty" for using this backdoor? FXSL has a set of type-processing functions that can determine dynamically the type of an item and return it as a string or return the constructor function for this type. This could be used to determine type (in)compatibility in case the backdoors Mike points out did not exist. Cheers, Dimitre Novatchev Re: Comparability
by
Michael Kay
on Mon 20 Feb 2006 19:41 GMT | Profile | Permanent Link
>Why on Earth is it necessary to raise an error when comparing *for equality* items of incompatible type?
Well, it's not necessary. But it's one of those areas where the right answer isn't obvious. The argument is that if someone compares a date to a string then they've made a mistake, just as if they try and add two dates, and it's better to tell them so than to return false. I'm actually rather torn on this one. There are too many cases where XPath 1.0 makes it difficult for the user to diagnose errors because of the "never fail" philosophy. On the other hand, with an existential operator like "=", I feel the right answer would be to return true if any pair is equal, and ignore any pairs that are non-comparable (this isn't what the spec says, though). Re: Comparability
by
Kurt Cagle
on Tue 21 Feb 2006 02:38 GMT | Profile | Permanent Link
I could see where this becomes a question in those cases where schema is assigned to an XML document in a manner that's beyond the control of the transformation writer (or in those cases where type compatiblity is needed but the XML itself is potentially dirty with regard to type). Admittedly its an edge case that probably wouldn't appear if you know more about XSLT 2 and XPath 2 programming, but at least for a while I suspect that you'll see a lot of these cases in XSLT that's being upgraded from 1.0.
|
Search
Recent Comments
Recent Articles
Month Archive
|
|||||||