W3C home > Mailing lists > Public > public-rdf-dawg@w3.org > April to June 2007

Re: Casting

From: Seaborne, Andy <andy.seaborne@hp.com>
Date: Mon, 23 Apr 2007 14:48:58 +0100
Message-ID: <462CB94A.1020609@hp.com>
To: Steve Harris <steve.harris@garlik.com>
CC: dawg mailing list <public-rdf-dawg@w3.org>



Steve Harris wrote:
> Sorry, this is a bit late in the proceedings to be asking questions  
> like this, but I've just hit something I don't follow.
> 
> I have a bunch of data that has latitude and longitude values encoded  
> as plain literals, like:
> 
> [] :name "The Red Lion" ;
>     :lat "-1.03" ;
>     :long "51.2" .
> 
> Yes, I know that's not ideal, but such is life.
> 
> Now, if you want to FILTER on those values to narrow down, like:
> 
> SELECT ?name
> WHERE {
>    ?place :name ?name ;
>           :lat ?lat ;
>           :long ?long .
>    FILTER(xsd:integer(?lat) = -1 && xsd:integer(?long) = 51)
> }
> 
> it doesn't work. From my reading of http://www.w3.org/TR/xpath- 
> functions/#casting-from-strings (which may well be flawed) it's an  
> error to cast from "-1.03" to "-1"^^xsd:integer. I think you can do  
> xsd:integer(xsd:double(?lat))

ARQ will fail the cast to xsd:integer because "1.3" is not a valid literal 
form for an integer - it's the same for xsd:integer() and 
xsd:integer(xsd:double()).

ARQ casting is probably too liberal - it takes the lexical form of a literal, 
does not take into account any existing datatype and attempts to force it to 
the requested type.  However, this rather direct way of doing it does cover 
the casting table in 11.5 as all the "N"s are for incompatible lexical forms.

The real work of parsing and testing the lexical form is done by Xerces.

 > OK, but I don't think it will be
> obvious to many people why one is legal, and one is not. If that is  
> the case.

Maybe use fn:floor:

FILTER (fn:floor(xsd:double(?lat)) == -1 && fn:floor(xsd:double(?long) = 51)

or done explicitly:

    FILTER ( ( xsd:double(?lat) >= -1 && xsd:double(?lat) < 0 ) &&
             ( xsd:double(?long) >= 51 && xsd:double(?lat) < 52 ) &&

> 
> I'd be interested to know what other people's implementations do.  
> Obviously the "right" solution is to fix the data.

Great if it's possible - data is often as provided.

	Andy

> 
> - Steve
> 

-- 
Hewlett-Packard Limited
Registered Office: Cain Road, Bracknell, Berks RG12 1HN
Registered No: 690597 England
Received on Monday, 23 April 2007 13:49:17 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 16:15:36 GMT