List countries from DBpedia

♀尐吖头ヾ 提交于 2019-12-11 01:45:23

问题


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?


回答1:


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 }
}


来源:https://stackoverflow.com/questions/48465075/list-countries-from-dbpedia

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!