simple sparql query from dbpedia
Asked Answered
G

2

9

I have a question about a SPARQL query that I'm trying to build from a tutorial. I want to generate triples that return a list of band members and the bands that they are in using a DBPedia endpoint.

my query

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?bandname where {
?person foaf:name ?name .
?band dbo:bandMember ?person .
?band dbo:genre dbpedia:Punk_rock .
?band dbp:name ?bandname .
}

I'm also using a [SPARQL query validator][2] to try to figure out my problem and it seems I'm using an incorrect prefix for "dbp:name ?bandname"I just want the triples returned in JSON if possible.

Once I can get this to run, I'd like to add another prefix, from GeoNames, to see the places associated with the bands, if possible (but that part is in the future). Any insights would be greatly appreciated!

Generality answered 24/2, 2016 at 7:25 Comment(0)
B
5

The problem is with undefined prefix dbpedia. Also I think you have to replace dbp:name with foaf:name:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?bandname where {
   ?person foaf:name ?name .
   ?band dbo:bandMember ?person .
   ?band dbo:genre dbp:Punk_rock .
   ?band foaf:name ?bandname .
}

Alas, with SELECT you won't get triples, but just the names (or other stuff) in tabular results. Much like SQL. If you want triples, you need CONSTRUCT

Boschbok answered 24/2, 2016 at 10:4 Comment(0)
B
7

There are some issues with your query.

  • The "name" property for a band has the following URI : http://dbpedia.org/property/name. It is a property, and not a resource, but you defined it as such in the prefixes. You shoud define it as follows : PREFIX dbp: <http://dbpedia.org/property/>
  • I just had a quick check at what a band page has as properties, and saw that, apart from the dbo:bandMember one you are using, there is another property, currentMembers, that seems to retrieve more information. This seems logical though, in so far as dbo:bandMember only retrieves entities (members URIs), whereas dbp:currentMembers also retrieves literals. It depends on your use case here...
  • You use the dbpedia:prefix in your query, that does not seem to have been defined beforehand.

Here is the query I used to retrieve a list of bands associated with their members :

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?members ?bandName where {
 ?band dbo:genre dbr:Punk_rock .
 ?band dbp:currentMembers ?members.
 ?band foaf:name ?bandName
 FILTER(langMatches(lang(?bandName), "en"))
}

The filter part allows us to avoid duplicates in case the literals are also defined in other languages.

But if you still want to use the dbo:bandMember property, this query also does the job :

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?personName ?bandName where {
 ?band dbo:bandMember ?person .
 ?person foaf:name ?personName .
 ?band dbo:genre dbr:Punk_rock .
 ?band foaf:name ?bandName
 FILTER(langMatches(lang(?bandName), "en"))
 FILTER(langMatches(lang(?personName), "en"))
}

Hope that helps !

Beal answered 24/2, 2016 at 10:28 Comment(0)
B
5

The problem is with undefined prefix dbpedia. Also I think you have to replace dbp:name with foaf:name:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?bandname where {
   ?person foaf:name ?name .
   ?band dbo:bandMember ?person .
   ?band dbo:genre dbp:Punk_rock .
   ?band foaf:name ?bandname .
}

Alas, with SELECT you won't get triples, but just the names (or other stuff) in tabular results. Much like SQL. If you want triples, you need CONSTRUCT

Boschbok answered 24/2, 2016 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.