[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Cannot compare xs:untypedAtomic to xs:double

Subject: Re: Cannot compare xs:untypedAtomic to xs:double
From: "Imsieke, Gerrit, le-tex gerrit.imsieke@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 29 Jul 2015 19:16:28 -0000
Re:  Cannot compare xs:untypedAtomic to xs:double
Have you tried &gt; instead of gt?

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

And I didnbt see where itbs 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 attributebs type.

Gerrit


On 29.07.2015 20:54, Jorge . chocolate.camera@xxxxxxxxx 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.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.