0
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"

IF these are the triples and i want to Find assistant professors who teach Graduate

 Lecturer       Position

 Arthur         Assistant Professor 

how it is possible to extract the above date using SPARQL

4

1 回答 1

4

据我所知,您的数据不在任何合法的 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" |
-----------------------------------------
于 2013-06-20T12:08:17.743 回答