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
Both of these languages give the user access to create, combine, and consume structured data. SQL does this by accessing tables in relational databases, and SPARQL does this by accessing a web of Linked Data.
SPARQL (“SPARQL Protocol And RDF Query Language”) is a W3C standard for querying RDF and can be used to express queries across diverse data sources, whether the data is stored natively as RDF or viewed as RDF via middleware.
SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions. SPARQL also supports extensible value testing and constraining queries by source RDF graph. The results of SPARQL queries can be results sets or RDF graphs.
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" )
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With