簡體   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