List countries from DBpedia
Asked Answered
I

1

2

Trying to query DBpedia for a list of all countries with the dbo:longName property and the capital of each country listed but get 0 results returned. Can't see whats wrong with the query.

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?country ?capital
WHERE {
 ?country a dbo:longName ;
    dbo:capital ?capital .
}

What's missing?

Ipa answered 26/1, 2018 at 16:5 Comment(1)
Your first triple pattern is ?country rdf:type dbo:longName - does this make sense to you?Tranquilize
S
5

You are missing that ?country has a rdf:type of dbo:Country and not dbo:longName. The right query should be:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?country ?capital
WHERE {
 ?x a dbo:Country.
 ?x dbo:longName ?country.
 ?x dbp:capital ?capital
}

Update

Based on your comment you want the URI's of the country and their capital. Therefore, you don't need dbo:longName, because you don't want the country label name. You will select the instances:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?country ?capital
WHERE {
 ?country a dbo:Country.
 ?country dbo:capital ?capital
}

Note that results will bring countries that are extinct. If you want to filter countries that have ended, you should get this result with:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?country ?capital
WHERE {
 ?country a dbo:Country.
 ?country dbo:capital ?capital.
 FILTER NOT EXISTS { ?country dbo:dissolutionYear ?yearEnd }
}
Seldun answered 26/1, 2018 at 16:19 Comment(2)
The query gets a list, but the list is not what i'm looking for. Trying to list ?x and ?y with one column of countries and the other their capitals. With each column containing URIs to the countries and their capitals. Kind of what this will return: SELECT ?s ?x WHERE { ?s a dbo:City ; dbo:country ?x }Ipa
Your question does not make this clear. Anyway, I updated the answer to suits what you are looking for.Seldun

© 2022 - 2024 — McMap. All rights reserved.