我是SPARQL的新手,我正在尝试运行SPARQL查询,以便返回属性的结果,并针对相关属性的值列出。
示例代码为:
SELECT ?player ?position ?club ?goals WHERE {
?player a <http://dbpedia.org/ontology/SoccerManager> . filter (contains (str(?player), "Alan_Shearer")) .
?player <http://dbpedia.org/ontology/position> ?position .
?player <http://dbpedia.org/property/clubs> ?club .
?player <http://dbpedia.org/property/goals> ?goals .
}
结果是每个俱乐部都复制了所有目标:
player position club goals
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Southampton_F.C. 23
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Southampton_F.C. 112
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Southampton_F.C. 148
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Newcastle_United_F.C. 23
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Newcastle_United_F.C. 112
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Newcastle_United_F.C. 148
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Blackburn_Rovers_F.C. 23
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Blackburn_Rovers_F.C. 112
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Blackburn_Rovers_F.C. 148
每个俱乐部的目标在数据集中正确关联,因此我想要获得的只是各自俱乐部的目标:
player position club goals
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Southampton_F.C. 23
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Newcastle_United_F.C. 112
http://dbpedia.org/resource/Alan_Shearer http://dbpedia.org/resource/Forward_(association_football) http://dbpedia.org/resource/Blackburn_Rovers_F.C. 148
但是,我不会在SPARQL中关注如何执行此操作,我们非常感谢您的帮助。
答案 0 :(得分:2)
请注意数据中有一些dbpedia-owl:careerStation属性值:
dbpedia-owl:careerStation dbpedia:Alan_Shearer__1, dbpedia:Alan_Shearer__2, ...
如果您查看这些属性的值,例如http://dbpedia.org/page/Alan_Shearer__3,您可以看到中的某些具有多个目标属性。这意味着你可以这样做:
select ?player ?position ?team ?goals {
values ?player { dbpedia:Alan_Shearer }
?player dbpedia-owl:position ?position ;
dbpedia-owl:careerStation [ dbpedia-owl:team ?team ;
dbpedia-owl:numberOfGoals ?goals ] .
}
由于并非所有电台都有目标信息,您可能需要在此处使用可选项来获取电台,然后在 时使用目标
select ?player ?position ?team ?goals {
values ?player { dbpedia:Alan_Shearer }
?player dbpedia-owl:position ?position ;
dbpedia-owl:careerStation ?station .
?station dbpedia-owl:team ?team .
optional { ?station dbpedia-owl:numberOfGoals ?goals }
}