#276 Spurious type errors in general comparisons

v8.2
closed
Michael Kay
5
2012-10-08
2004-12-31
Michael Kay
No

A spurious type error (for example "Cannot compare
xs:string to xs:integer") may occur when using a
general comparison of the form ($a = 3), or say ($a <
0), of the static type of $a is anyAtomicType - that
is, if it may contain either typed or untyped values,
but if the cardinality is known statically to be
"exactly one". In this situation Saxon is incorrectly
rewriting the expression as ($a eq 3), or say ($a lt
0), which is not an equivalent expression because it
does not perform conversion of untyped atomic values to
the type of the other operand.

The problem was present in earlier releases but is
likely to occur more often with Saxon 8.2 because Saxon
is now better at determining that the cardinality of an
expression is "exactly one".

The problem is less likely to occur with Saxon-SA,
which uses a different (schema-aware) algorithm for
evaluating general comparisons.

Source fix: in net.sf.saxon.expr.GeneralComparison,
around line 97, change the code

    if (c0 == StaticProperty.EXACTLY_ONE &&
            c1 == StaticProperty.EXACTLY_ONE) {

to read:

    if (c0 == StaticProperty.EXACTLY_ONE &&
            c1 == StaticProperty.EXACTLY_ONE &&
            t0 != Type.ANY_ATOMIC_TYPE &&
            t1 != Type.ANY_ATOMIC_TYPE) {

Test case added: qxmp202

Michael Kay

Discussion