Search code examples
rdfsparqlsemantic-webdbpedia

Can't get this SPARQL query to work


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


Solution

  • Okay, I was missing that "Depeche Mode" couldn't be selected using an rdf:type of http://dbpedia.org/ontology/MusicalArtist. This seems to work:

    SELECT ?subject 
           ?artistName 
    WHERE {
      { 
        ?subject rdf:type <http://dbpedia.org/ontology/MusicalArtist> .
        ?subject rdfs:label ?artistName .
        FILTER ( str(?artistName) = "Depeche Mode" )
      } 
      UNION 
      {
        ?subject rdf:type <http://dbpedia.org/ontology/Band> .
        ?subject rdfs:label ?artistName .
        FILTER ( str(?artistName) = "Depeche Mode" )
      }
    }