【问题标题】:Sparql optional propertiesSparql 可选属性
【发布时间】:2015-08-04 12:18:35
【问题描述】:

有没有更好的方法来表达以下使用多个可选的查询?当有很多属性时,这很快就会变得容易出错。

SELECT * WHERE {
  BIND(:London AS ?source)
  OPTIONAL{ ?source rdfs:label ?o .}
  OPTIONAL{?source rdfs:comment ?i . }
}

【问题讨论】:

    标签: rdf sparql


    【解决方案1】:

    实际上,你不能做得比这更好。我唯一不同的是使用 values 来指定 ?source 的值,而不是 bind

    select * where {
      values ?source { dbpedia:London }
      optional { ?source rdfs:label   ?o }
      optional { ?source rdfs:comment ?i }
    }
    

    如果您愿意以不同的方式处理数据,您也可以使用使用值指定属性的查询:

    select * where {
      values ?source { dbpedia:London }
      values ?property { rdfs:label rdfs:comment }
      ?source ?property ?value
    }
    

    当然,如果你这样做,那么你用列数换取行数,但如果任何属性有多个值,你仍然可能会更好。例如,在第一种情况下,如果 London 只有一个标签和一个评论,您会得到:

    source    label        commment
    ------------------------------------
    London    "the label"  "the comment"
    

    但如果您有两个标签和三个 cmets,您将有六(= 二 × 三)行:

    source    label     commment
    ------------------------------------
    London    "label1"  "comment1"
    London    "label1"  "comment2"
    London    "label1"  "comment3"
    London    "label2"  "comment1"
    London    "label2"  "comment2"
    London    "label2"  "comment3"
    

    在第二种情况下,如果你有一个标签和一个评论,你就有两行:

    source    property      value 
    ------------------------------------
    London    rdfs:label    "label1"
    London    rdfs:comment  "comment1"
    

    但是,如果您有两个标签和三个 cmets,则最终只有五 (= 2 + 3) 行:

    source    property      value 
    ------------------------------------
    London    rdfs:label    "label1"
    London    rdfs:label    "label2"
    London    rdfs:comment  "comment1"
    London    rdfs:comment  "comment2"
    London    rdfs:comment  "comment3"
    

    因此,如果属性可以有多个值(在 DBpedia 数据中很常见),使用 枚举您想要的属性可能不太容易出错(没有 可选 块,并且您可以将所有属性都写在同一个地方),并且可能会让您担心的行数更少。缺点是您必须处理输出并查看每一行中的属性;每个属性值都没有单独的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多