Can't get this SPARQL query to work
Posted
by
Jason
on Stack Overflow
See other posts from Stack Overflow
or by Jason
Published on 2012-06-03T09:06:08Z
Indexed on
2012/06/03
16:40 UTC
Read the original article
Hit count: 305
Okay, I'm just learning to use SPARQL to query data from dbpedia.org and I'm using dbpedia's http://dbpedia.org/snorql/ to run my queries in. I am trying to get a list of MusicalArtists based on searching for the same string over three fields like so:
SELECT ?subject
?artistRdfsLabel
?artistFoafName
?artistDbpedia2Name
WHERE {
?subject rdf:type <http://dbpedia.org/ontology/MusicalArtist> .
OPTIONAL { ?subject rdfs:label ?artistRdfsLabel . }
OPTIONAL { ?subject foaf:name ?artistFoafName . }
OPTIONAL { ?subject dbpedia2:name ?artistDbpedia2Name . }
FILTER ( str(?artistRdfsLabel) = "Stevie Nicks" ||
str(?artistFoafName) = "Stevie Nicks" ||
str(?artistDbpedia2Name) = "Stevie Nicks" )
}
LIMIT 10
This works because "Stevie Nicks" has all three fields (rdfs:label, foaf:name, dbpedia2:name). But when I try to query by another MusicalArtist that doesn't have all three ("Depeche Mode" for example) I get no results.
I have tried various things like BIND(COALESCE(?field,...,...) AS ?artistName) to filter by ?artistName and I also tried UNION but nothing seems to work. Can someone point out the error of my SPARQL ways? :)
Thanks! Jason
© Stack Overflow or respective owner