【问题标题】:SPARQL limiting the query result by a variable instead of the number of rowsSPARQL 通过变量而不是行数限制查询结果
【发布时间】:2015-08-31 11:06:54
【问题描述】:

假设我有以下数据集:

: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

我想查询数据集以返回: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

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

【问题讨论】:

    标签: rdf sparql semantic-web owl sesame


    【解决方案1】:

    使用子查询选择这两个东西,然后在外部查询中获取其余数据。它总是有助于显示我们可以测试的合法工作数据。您显示的数据实际上不是合法的 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 .
    
    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
    }
    
    --------------------------------------------------------------------
    | 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     |
    --------------------------------------------------------------------
    

    【讨论】:

    • 我已经尝试过这个解决方案,但它不起作用。它按行限制结果。
    • 除非我在您的问题中遗漏了某些内容,否则这似乎就是您想要的。这表示选择两个属于 a 类的实例,然后在选择这两个之后,获取有关它们的所有信息。如果您没有获得这些结果,您能否发布您尝试过的确切查询以及您获得的结果?
    • @whitefang1993 我添加了一个完整的工作示例。我建议的查询确实得到了您要求的结果。如果不适合您,您能告诉我们您使用的是什么 SPARQL 引擎和版本吗?可能实现中存在错误。
    • 这听起来越来越像芝麻虫了,所以你现在最好的选择可能是在他们的邮件列表中询问
    • OP 在此处添加了有关 Sesame 中潜在错误的后续问题:stackoverflow.com/questions/32346595/…。结果是它是 Workbench 客户端显示结果的错误,而不是查询引擎本身的错误。
    猜你喜欢
    • 2012-02-12
    • 2023-03-10
    • 2022-11-04
    • 2011-01-06
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多