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
的文件:
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
s,但是没有绑定到"Assistant Professor"
的变量。有了数据和查询,我们可以使用Jena的ARQ命令行工具对数据运行查询:
$ arq --query query.sparql --data data.n3
-----------------------------------------
| lecturer | position |
=========================================
| teach1:Arthur | "Assistant Professor" |
-----------------------------------------