How to get a single name for multiple name properties in DBPedia with SPARQL?

182 views Asked by At

I need to get persons' names from DBPedia, but the problem is that not every person has the dbpprop:fullname property, sometimes it only has the dbpprop:name or the rdfs:label property.

To clarify: I want to query for the name with a preference for the first property, if that doesn't exist get the second property, and if that doesn't exist get the third property, etc.

So how could I get person's name from the

   dbpprop:name (person name = [dbpprop:name] if no [dbpprop:fullname])

and from

  rdfs:name (person name = [rdfs:name] if no both [dbpprop:fullname] and [dbpprop:name])
1

There are 1 answers

4
Jeen Broekstra On BEST ANSWER

You can do this using a COALESCE function:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpprop: <http://dbpedia.org/property/>

SELECT DISTINCT ?x (COALESCE(?dbpfn, ?dbpn, ?label) as ?name) 
WHERE { 
      ?x a foaf:Person .
      OPTIONAL { ?x dbpprop:fullname ?dbpfn }
      OPTIONAL { ?x dbpprop:name ?dbpn }
      OPTIONAL { ?x rdfs:label ?label }
}
LIMIT 100

Result

COALESCE takes a list of arguments as input, and outputs the first of those arguments that does not correspond to an error. Since an unbound variable corresponds to an error, this will return the full name if it exists, otherwise the name if it exists, otherwise the label if it exists.