简体   繁体   中英

SPARQL filter and union

I am trying to list all entries whose names start with the letter 'R' and are older than 20 but It is not working - Can you please give me a hint?

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?givenname ?Age ?firstName
WHERE {
    ?Person foaf:givenname ?firstName
    {FILTER (?Age > '20')}
    UNION
    {FILTER regex(?givename, "^(R)")}
}

To find names that start with "R", you can use STRSTARTS() :

FILTER( STRSTARTS(?givename, "R") ) .

To filter based on the age, you first have to add/bind this variable in a triple pattern, eg:

?Person ex:yourAgeProperty ?Age .

Your age FILTER compares strings (as you use quotation marks and don't specify a datatype, which defaults to xsd:string ). In case the age is given as xsd:integer in your data, you could use:

FILTER( ?Age > "20"^^xsd:integer ) .

And if both filters need to apply, simply list them one after the other (without {} and without UNION ). Or you could combine them:

FILTER( STRSTARTS(?givename, "R") && ?Age > "20"^^xsd:integer ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM