过滤两个具有相同值的属性

时间:2016-01-11 20:33:07

标签: sparql

我需要找回首都也是最大城市的南美国家名单。出于某种原因,我可以用他们的首都显示所有南美国家,但我不能使用过滤选项来比较首都和最大城市:

SELECT DISTINCT ?country ?capital
WHERE {
    ?country a dbo:Country .
    ?country a <http://dbpedia.org/class/yago/SouthAmericanCountries>.

    ?country dbp:largestCity|dbo:largestCity ?capital.
}

对于第3个三元组,我使用2个参数,因为并非所有国家都有完整的数据。在dbpedia验证器上运行后,我得到了这个: http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=SELECT+DISTINCT+%3Fcountry+%3Fcapital%0D%0AWHERE+%7B%0D%0A%0D%0A++++%3Fcountry+a+dbo%3ACountry+.%0D%0A++++%3Fcountry+a+%3Chttp%3A%2F%2Fdbpedia.org%2Fclass%2Fyago%2FSouthAmericanCountries%3E.%0D%0A++++%0D%0A++++%3Fcountry+dbp%3AlargestCity%7Cdbo%3AlargestCity+%3Fcapital.%0D%0A%0D%0A++++%0D%0A%0D%0A%7D&format=text%2Fhtml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on

下一步是使用此逻辑过滤国家/地区:largestCity = capital。所以我用过:

FILTER (dbp:largestcity="capital")

但是Viruoso验证器发送了一个超出执行时间的例外。 有什么建议吗?

1 个答案:

答案 0 :(得分:3)

我不确定您希望过滤器做什么。在 FILTER(dbp:largestcity =&#34; capital&#34;),&#34; capital&#34;是一个字符串, dbp:largestcity (顺便说一句, dbp:largestCity 相同)是一个IRI;他们永远不会平等。

现在,我看到有时 dbp:largestCity 的值是字符串&#34; capital&#34; @en ,所以有一些字符串匹配可能会有所帮助。但是,一般而言,DBpedia本体属性比原始信息框属性具有更清晰的数据,因此如果可以,您应该更喜欢 dbo:属性。我想在这里,你会想要两者。

你需要你的查询来提取资本,以及最大的城市,然后你想要过滤他们相等的情况,或者最大的城市是字符串&#34;资本&#34;

SELECT DISTINCT ?country ?capital WHERE {
  ?country a dbo:Country .
  ?country a <http://dbpedia.org/class/yago/SouthAmericanCountries>.
  ?country dbo:capital ?capital .
  ?country dbp:largestCity|dbo:largestCity ?largestCity .
  filter (?largestCity = ?capital || str(?largestCity)= "capital")
}

SPARQL results