我正在学习如何创建SPARQL查询.目前,我正在使用Dbpedia数据集.
我尝试使用以下查询查询"加拿大有哪些机场":
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#>
SELECT ?name ?country WHERE {
?name rdf:type <http://dbpedia.org/ontology/Airport>;
?name rdf:type <http://dbpedia.org/ontology/Country>
}
LIMIT 20
Run Code Online (Sandbox Code Playgroud)
我仍然对构建SPARQL查询感到困惑,特别是对于资源和RDF图.
我需要的是上述查询的错误是什么?
谢谢
您正在寻找的查询类似于:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?airport ?label WHERE {
?airport rdf:type <http://dbpedia.org/ontology/Airport>;
rdfs:label ?label;
dbpedia-owl:location <http://dbpedia.org/resource/Canada> .
}
Run Code Online (Sandbox Code Playgroud)
然而,这个查询不会返回太多结果,你会更喜欢这样的东西:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?airport ?label WHERE {
?airport rdf:type <http://dbpedia.org/class/yago/AirportsInOntario> ;
rdfs:label ?label .
}
Run Code Online (Sandbox Code Playgroud)
您的初始查询中存在各种错误,这意味着您应该更好地了解SPARQL.您需要修改构建三重模式的方式.我建议你看看下面的教程:
http://www.cambridgesemantics.com/2008/09/sparql-by-example/
您还会发现探索性的SPARQL查询非常有用: