我正在使用ARC2库for php来发布sparql查询,但我在这个问题上遇到了麻烦(我认为这与lib无关)。
这个查询在我的应用程序和dbpedia snorql:中运行得很好
PREFIX dbo:<http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.geonames.org/ontology#>
SELECT * WHERE {
?c rdf:type dbo:Country;
foaf:name "someCountryName"@en.
}
另一方面,这个查询不起作用:
SELECT * WHERE {
?c rdf:type dbo:Country;
foaf:name "someCountryName"@en;
geo:lat ?lat.
}
注意:查询是使用与上面列出的前缀相同的前缀完成的。我只需要服用lat&一个国家的漫长。我也可以试试Freebase,但我真的需要让它在这里发挥作用。第二个查询在snorql中工作,不明白为什么它在我的应用程序中也不工作?非常感谢您的帮助!
注意:此文本由@IrinaM提供,但由于八小时的限制,将其作为修订版放入问题中。现在已经晚了很多,关于@IrinaM发帖作为答案的建议还没有得到落实。这是一个经过轻微编辑的CW答案,包含该文本
问题中的代码有几个错误。
-
2nd查询(不起作用的查询)将返回几个结果。如果我用
foaf:name "Romania"
搜索国家,它会返回"Communist Romania"
、"Wallachian Romania"
、"Romania"
等。在这些结果中,只有一个会有geo:lat
和geo:long
。基本上,当所有结果都不满足所需的属性时,就不会返回任何结果。 -
geo
使用了错误的本体;它应该是CCD_ 8。 -
其他奇怪的结果需要过滤掉,只保留所需的唯一结果。在这种情况下,在
dbpedia-owl:dissolutionDate
的不存在之后进行过滤。使用FILTER (?name="someCountryName"^^xsd:string)
的foaf:name的完全匹配不起作用。
以下是成功检索给定国家/地区的纬度和经度值的代码(注意,在搜索正确的国家/地区时可能需要一些其他过滤器):
//setup prefixes
$prefixes = 'PREFIX dbo:<http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
';
//query no. 1 - find out "right" country - hoping for a unique result
$q1 = $prefixes.'SELECT ?c WHERE {
?c rdf:type dbo:Country;
foaf:name "'.$userPreferences->country.'"@en.
OPTIONAL {?c dbo:dissolutionDate ?x}
FILTER(!bound(?x))}';
//note: $userPreferences->country represents a country entered by the user in an input
//query no. 2 - find out long & lat
$q2 = $prefixes.'SELECT * WHERE {
<'.$countryReturned.'> geo:lat ?lat;
geo:long ?long.
}';
//note: $countryReturned represents the URI of the "unique" country my 1st query
//retrieved, we use it directly.
对于那些热衷于ARC2 PHP库的人来说,以下是进行查询的方法(注意:不要忘记包括ARC2.PHP文件):
$dbpediaEndpoint = array('remote_store_endpoint' =>'http://dbpedia.org/sparql');
$dbpediaStore = ARC2::getRemoteStore($dbpediaEndpoint);
$rows1 = $dbpediaStore->query($q1,'rows');
var_dump($rows1); //to see quick results