我正在尝试使用SPARQL查询对DBPedia进行相当复杂的调用。我想获得一些关于城市的信息(地区,联邦州/联邦德国,邮政编码,坐标和地理相关城市)。简化SPARQL查询
SELECT * WHERE {
#input
?x rdfs:label "Bentzin"@de.
#district
OPTIONAL {
?x dbpedia-owl:district ?district
# ?x dbpprop:landkreis ?district
{ SELECT * WHERE {
?district rdfs:label ?districtName
FILTER(lang(?districtName) = "de")
?district dbpprop:capital ?districtCapital
{ SELECT * WHERE {
?districtCapital rdfs:label ?districtCapitalName
FILTER(lang(?districtCapitalName) = "de")
}}
}}
}
#federal state
OPTIONAL {
# ?x dbpprop:bundesland ?land
?x dbpedia-owl:federalState ?land
{ SELECT * WHERE {
?land rdfs:label ?landName
FILTER(lang(?landName) = "de")
}}
}
#postal codes
?x dbpedia-owl:postalCode ?zip.
#coordinates
?x geo:lat ?lat.
?x geo:long ?long
#cities in the south
OPTIONAL {
?x dbpprop:south ?south
{SELECT * WHERE {
?south rdfs:label ?southName
FILTER(lang(?southName) = "de")
}}
}
#cities in the north
OPTIONAL {
?x dbpprop:north ?north
{ SELECT * WHERE {
?north rdfs:label ?northName
FILTER(lang(?northName) = "de")
}}
}
#cities in the west
...
}
这工作在某些情况下,但是,有几个重大问题。
有几个不同的属性可能包含联邦州或地区的价值。有时是
dbpprop:landkreis
(用于区域德语单词,在其他情况下,它的dbpedia-owl:district
。是否有可能合并的情况下这两个在那里只有一个设置?此外,我想读出的名字。在北方城市,西北,......有时,这些城市在
dbpprop:north
等引用的每个方向的基本查询是相同的:OPTIONAL { ?x dbpprop:north ?north { SELECT * WHERE { ?north rdfs:label ?northName FILTER(lang(?northName) = "de") }} }
我真的不想再重复八次,每方向,有什么办法可以简化这个吗?
有时,还有其他多个城市参考(example)。在这些情况下,返回多个数据集。是否有可能在单个数据集中获得这些城市的名称列表?
+---+---+---------------------------------------------------------------+ | x | … | southName | +---+---+---------------------------------------------------------------+ | … | … | "Darmstadt"@de, "Stuttgart"@de, "Karlsruhe"@de, "Mannheim"@de | +---+---+---------------------------------------------------------------+
您的反馈和你的想法是非常感谢!
直到
这工作都非常好!非常感谢。我正在使用'(group_concat(?directionPlaceName; separator =“,”)as?directionPlaceNames)'来获取北部,南部等地方的列表(参见示例)(http://bit.ly/1NYfSuO) )。 但是,这不会获得地方的方向。 (这对我来说没有任何问题,但是我对这样一个解决方案感兴趣,它会为每个方向创建几个列,每个列都包含一个特定方向的地方列表。并且还没有想出如何将'group_by'和'group_concat'组合起来 – Till 2015-04-06 16:19:53
@Till你可以在group_concat中使用任意表达式,为什么不能这样:'group_concat(concat(str(?directionProp),“:”,directionPlaceName ); separator =“;”)'得到类似**的东西:Place1; north:Place2; west:Place3 **? – 2015-04-06 16:31:19