SPARQL通过变量而不是行数限制查询结果

whi*_*993 1 rdf sesame semantic-web owl sparql

假设我有以下数据集:

:a  rdf:type      :AClass
:a  :hasName      "a"^^xsd:string
:a  :hasProperty  :xa
:a  :hasProperty  :ya
:a  :hasProperty  :za

:b  rdf:type      :AClass
:b  :hasName      "b"^^xsd:string
:b  :hasProperty  :xb
:b  :hasProperty  :yb

:c  rdf:type      :AClass
:c  :hasName      "c"^^xsd:string
:c  :hasProperty  :xc
Run Code Online (Sandbox Code Playgroud)

我想查询数据集以返回实例的所有内容:AClass,但仅限于两个实例.我知道我必须使用LIMIT关键字,我已经尝试了很多查询但没有成功.

换句话说,我想回到这个:

:a  :hasName      "a"^^xsd:string
:a  :hasProperty  :xa
:a  :hasProperty  :ya
:a  :hasProperty  :za

:b  :hasName      "b"^^xsd:string
:b  :hasProperty  :xb
:b  :hasProperty  :yb
Run Code Online (Sandbox Code Playgroud)

如何将结果限制为2个实例的数量而不是2个数量?

Jos*_*lor 7

使用子查询选择两个东西,然后在外部查询中获取其余数据.显示我们可以测试的合法工作数据总是有帮助的.您显示的数据实际上并不是合法的RDF(因为它在行的末尾缺少一些句点),但我们可以轻松地创建一个工作示例.这是工作数据,查询和结果:

@prefix : <urn:ex:>

:a a :AClass .
:a :hasName "a" .
:a :hasProperty :xa .
:a :hasProperty :ya .
:a :hasProperty :za .

:b a :AClass .
:b :hasName "b" .
:b :hasProperty :xb .
:b :hasProperty :yb .

:c a :AClass .
:c :hasName "c" .
:c :hasProperty :xc .
Run Code Online (Sandbox Code Playgroud)
prefix : <urn:ex:>

select ?s ?p ?o {
  #-- first, select two instance of :AClass
  { select ?s { ?s a :AClass } limit 2 }

  #-- then, select all the triples of
  #-- which they are subjects
  ?s ?p ?o
}
Run Code Online (Sandbox Code Playgroud)
--------------------------------------------------------------------
| s  | p                                                 | o       |
====================================================================
| :a | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | :AClass |
| :a | :hasName                                          | "a"     |
| :a | :hasProperty                                      | :xa     |
| :a | :hasProperty                                      | :ya     |
| :a | :hasProperty                                      | :za     |
| :b | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | :AClass |
| :b | :hasName                                          | "b"     |
| :b | :hasProperty                                      | :xb     |
| :b | :hasProperty                                      | :yb     |
--------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

  • OP在这里添加了有关芝麻潜在错误的后续问题:http://stackoverflow.com/questions/32346595/sesame-2-8-4-subquery-limit-bug-fix.结果是,它是Workbench客户端显示结果的错误,而不是查询引擎本身. (2认同)