xsl-list
[Top] [All Lists]

Re: [xsl] Cannot compare xs:untypedAtomic to xs:double

2015-07-29 14:16:41
Have you tried > instead of gt?

I didn’t see a formal definition in the XPath 2 spec for > like the
one for gt, http://www.w3.org/TR/xpath-functions/#func-numeric-greater-than

And I didn’t see where it’s mentioned that XPath 1 comparison operators
such as > continue to work in XPath 2 like they are defined in XPath 1.

But what > did in XPath 1 and continues to do in XPath 2 is to try to
convert each of its arguments (or their string values) to a numeric
value using number(). So using > instead of gt might give you the
expressive conciseness that you were looking for.

Another approach is probably to use schema-aware transformations where
you should be able to declare each attribute’s type.

Gerrit


On 29.07.2015 20:54, Jorge . chocolate(_dot_)camera(_at_)gmail(_dot_)com wrote:
When comparing numeric values of node attributes to a double, I have
to explicitly convert those attribute values too into a double or else
Saxon complains that I cannot compare an untyped value to a double. It
gets kind of tiring having to do so on each comparison when you are
comparing lots of values a lot of times, and it makes the code less
readable.

I have this tree of nodes:

    <root width="10">
        <item  x="1.0" width="3.5"/>
        <item  x="6.5" width="1.5"/>
        <item  x="3.5" width="3.0"/>
    </root>

and I want to select items not falling cleanly in either the left or
right half of <root> (the 3rd item being so).

So, being <root> the context node, I try to select those items with:

    <xsl:variable name="WIDTH" select="@width" as="xs:double"/>
    <xsl:sequence select="item[@x lt $WIDTH div 2 and @x + @width gt
$WIDTH div 2]"/>

but Saxon complains:

Warning: on line 9 of stylesheet.xsl:
  Comparison of xs:untypedAtomic? to xs:double will fail unless the first 
operand is empty
Warning: on line 9 of stylesheet.xsl:
  Comparison of xs:untypedAtomic? to xs:double will fail unless the first 
operand is empty
Error on line 9 of stylesheet.xsl:
  XPTY0004: Cannot compare xs:untypedAtomic to xs:double
  in built-in template rule
Transformation failed: Run-time errors were reported

If I instead convert each attribute value into a double before the comparison:

    <xsl:sequence select="item[number(@x) lt $WIDTH div 2 and
number(@x) + number(@width) gt $WIDTH div 2]"/>

I get the expected result.

Considering that the input tree actually is generated some steps above
via an identity copy and stored into a variable, is there a way to
create it so that Saxon does know that those attribute values are
doubles, and therefore not needing to be wrapping each attribute
mention with number() every single time I want to compare numbers?

I am using XSLT 2.0, Saxon-HE 9.2.1.2J.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>