[英]how to extract subject (Name) in comparison with predicate from a RDF Triple using sparql
http://www.example.com/teach.rdfs/John http://www.example.com/teach.rdfs#position "Full Professor"
http://www.example.com/teach.rdfs/John http://www.example.com/teach.rdfs#course"Math"
http://www.example.com/teach.rdfs/John http://www.example.com/teach.rdfs#student"Undergraduate"
http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#position "Assistant Professor"
http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#course"Web Engineering"
http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#student"Graduate"
如果這些是三元組,我想找到教授研究生的助理教授
Lecturer Position
Arthur Assistant Professor
如何使用SPARQL提取上述日期
據我所知,您的數據沒有采用任何合法的RDF序列化方式,但是將其導入N3序列化相當容易。 將http://.../teach.rdfs#
和http://.../teach.rdfs/
用作同一文檔中的前綴是很不尋常的。 經常看到一個或另一個,但不能同時看到兩個。 但這不是非法的,因此我們可以使用它。 在N3格式中,這是您的數據文件data.n3
:
@prefix teach1: <http://www.example.com/teach.rdfs/> .
@prefix teach2: <http://www.example.com/teach.rdfs#> .
teach1:John teach2:position "Full Professor" .
teach1:John teach2:course "Math" .
teach1:John teach2:student "Undergraduate" .
teach1:Arthur teach2:position "Assistant Professor" .
teach1:Arthur teach2:course "Web Engineering" .
teach1:Arthur teach2:student "Graduate" .
查詢也非常簡單。 就像一個名為query.sparql
的文件query.sparql
:
PREFIX teach1: <http://www.example.com/teach.rdfs/>
PREFIX teach2: <http://www.example.com/teach.rdfs#>
SELECT ?lecturer ?position WHERE {
VALUES ?position { "Assistant Professor" }
?lecturer teach2:position ?position ;
teach2:student "Graduate" .
}
此查詢唯一與眾不同的地方是使用VALUES ?position { "Assistant Professor" }
。 我使用VALUES
表格的原因是,您想要的結果在輸出中包括"Assistant Professor"
。 如果我們排除VALUES ...
部分,則可以將模式重寫為
?lecturer teach2:position "Assistant Professor" ;
teach2:student "Graduate" .
並且仍然找到相同的?lecturer
,但"Assistant Professor"
沒有變數。 有了數據和查詢,我們可以使用Jena的ARQ命令行工具針對數據運行查詢:
$ arq --query query.sparql --data data.n3
-----------------------------------------
| lecturer | position |
=========================================
| teach1:Arthur | "Assistant Professor" |
-----------------------------------------
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.