繁体   English   中英

使 UNION 在 SPARQL 中返回单个结果

[英]Make a UNION return a single result in SPARQL

我正在尝试查询属性形状的sh:description ,如果没有,我想按照属性的路径获取rdfs:comment (也适用于sh:namerdfs:label )。 我的数据如下所示:

ex:File
  a owl:Class ;
  a sh:NodeShape ;
  sh:property ex:File-name ;

ex:File-name
  a sh:PropertyShape ;
  sh:path ex:filename ;
  sh:datatype xsd:string ;
  sh:description "The name of the file" ;
  sh:maxCount 1 ;

ex:filename
  a rdf:Property ;
  rdfs:label "Filename" ;
  rdfs:comment "The file name" ;
.

我有下面的查询,但它返回两个结果。 如何修改它以仅返回一个结果(更喜欢sh:description而不是rdfs:comment )?

PREFIX : <http://www.example.org/#>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?propertyShape ?property ?name ?description where {
    :File sh:property ?propertyShape .
    ?propertyShape sh:path ?property .
    { ?propertyShape sh:name ?name } UNION { ?property rdfs:label ?name } .
    { ?propertyShape sh:description ?description } UNION { ?property rdfs:comment ?description } .

上面返回的是这样的:

属性形状 财产 姓名 描述
:文件名 例如:文件名 “文件名” “文件名”
:文件名 例如:文件名 “文件名” “文件名”

我希望它返回如下内容:

属性形状 财产 姓名 描述
:文件名 例如:文件名 “文件名” “文件名”

您应该能够使用一系列OPTIONAL块来执行此操作:

SELECT ?propertyShape ?property ?name ?description WHERE {
    :File sh:property ?propertyShape .
    ?propertyShape sh:path ?property .

    OPTIONAL { ?propertyShape sh:name ?name }
    OPTIONAL { ?property rdfs:label ?name }

    OPTIONAL { ?propertyShape sh:description ?description }
    OPTIONAL { ?property rdfs:comment ?description }
}

您仍然需要考虑每个属性有多个名称/描述的情况(这不能保证只返回一个结果),但它应该为您提供“首选 sh:description 而不是 rdfs:comment”行为。

你不能只使用路径表达式,例如

...
?propertyShape sh:name|(sh:path/rdfs:label) ?name .
?propertyShape sh:description|(sh:path/rdfs:comment) ?description .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM