Cannot get this SPARQL query to work.

Ok, I just learned to use SPARQL to query data from dbpedia.org, and I use dbpedia http://dbpedia.org/snorql/ to run my queries. I am trying to get a list of MusicalArtists based on finding the same string over three fields, for example:

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 request another MusicalArtist that does not have all three (e.g. Depeche Mode), I get no results.

I tried various things like BIND (COALESCE (? Field, ..., ...) AS? ArtistName) to filter? artistName, and I also tried UNION, but nothing works. Can anyone point out the error of my SPARQL methods? :)

Thanks! Jason

+6
source share
1 answer

Well, I missed that “Depeche Mode” could not be selected using the rdf type: 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" ) } } 
+7
source

Source: https://habr.com/ru/post/917284/


All Articles