我正在嘗試使用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