Gabriela M
Gabriela M

Reputation: 615

Get birthplace from DBpedia people in SPARQL

I have the following code to retrieve all people that was born in Barcelona

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?person ?birthPlace
WHERE {

   ?person rdfs:label ?label.
   ?person rdf:type dbo:Person.
   ?person <http://dbpedia.org/property/birthPlace> 
<http://dbpedia.org/resource/Barcelona>.

}

However, I do not know how to get the birthPlace. I want a variable that says next to each name that Barcelona is the place of birth. Any ideas?

Upvotes: 2

Views: 984

Answers (1)

cygri
cygri

Reputation: 9472

How about this:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?person ?birthPlace
WHERE {
   ?person rdfs:label ?label.
   ?person rdf:type dbo:Person.
   ?person <http://dbpedia.org/property/birthPlace> ?birthPlace.
   FILTER (?birthPlace = <http://dbpedia.org/resource/Barcelona>)
}

Note that your query has a pattern to match labels, but the labels are not returned. That leads to duplicate results because some people have multiple labels (in different languages). Remove the pattern, or add ?label to the SELECT clause.

You can abbreviate <http://dbpedia.org/property/birthPlace> to dbp:birthPlace.

Upvotes: 1

Related Questions