维基数据按ItemIdentifies查询

时间:2017-06-21 08:30:40

标签: sparql wikidata

我想通过其商品标识来查询给定城市的翻译。 这是我在karlsruhe的if上查询属性p31

这是我的查询

 SELECT DISTINCT ?city ?label ?population WHERE {
  ?city (wdt:P31/wdt:P279*) wd:Q515. // is city
  ?city wdt:P31 wd:Q1040. //is karlsruhe
  ?city wdt:P1082 ?population.
  ?city rdfs:label ?label.
  FILTER(((LANG(?label)) = "de") || ((LANG(?label)) = "en") || ((LANG(?label)) = "it") || ((LANG(?label)) = "ru") || ((LANG(?label)) = "ro") || ((LANG(?label)) = "tr") || ((LANG(?label)) = "pl") || ((LANG(?label)) = "fr") || ((LANG(?label)) = "es") || ((LANG(?label)) = "hr"))
}

但我没有得到任何结果。

我做错了什么?

1 个答案:

答案 0 :(得分:4)

简而言之,问题是卡尔斯鲁厄不是 a 卡尔斯鲁厄。请仔细阅读wdt:P31的定义。

您的查询应该是:

SELECT DISTINCT ?city ?label ?population WHERE {
  VALUES (?city) { (wd:Q1040) }      # is Karlsruhe
  ?city (wdt:P31/wdt:P279*) wd:Q515  # is a city
  OPTIONAL { ?city wdt:P1082 ?population } 
  ?city rdfs:label ?label
  FILTER(LANG(?label) IN ("de", "en", "it", "ru", "ro", "tr", "pl", "fr", "es", "hr"))
}

为了更好地理解,可以用这种方式重写上面的查询:

SELECT DISTINCT ?city ?label ?population WHERE {
  FILTER(?city IN (wd:Q1040))        # is Karlsruhe
  ?city (wdt:P31/wdt:P279*) wd:Q515  # is a city
  OPTIONAL { ?city wdt:P1082 ?population } 
  ?city rdfs:label ?label
  FILTER(LANG(?label) IN ("de", "en", "it", "ru", "ro", "tr", "pl", "fr", "es", "hr"))
}

在RDFS ++蕴涵下,人们甚至会写:

SELECT DISTINCT ?city ?label ?population WHERE {
  ?city owl:sameAs wd:Q1040   # is Karlsruhe
  ?city wdt:P31 wd:Q515       # is a city
  OPTIONAL { ?city wdt:P1082 ?population } 
  ?city rdfs:label ?label
  FILTER(LANG(?label) IN ("de", "en", "it", "ru", "ro", "tr", "pl", "fr", "es", "hr"))
}