Sparql - 条件输出

Sparql - Conditional Output

我对语义网和 sparql 还很陌生。我有一个内部 ontology 使用 SmartLogic 来管理数据。

我正在编写一些简单的查询以开始使用。


PREFIX skos: <http://www.w3.org/2004/02/skos/core#> # Simple Knowledge Organization System - https://www.w3.org/2004/02/skos/
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <https://www.w3.org/TR/rdf-schema/>
prefix owl: <http://www.w3.org/2002/07/owl#> 
prefix ap: <http://cv.ap.org/ns>

SELECT DISTINCT
           ?subjectPrefLabel  ?p ?o ?oL

         WHERE {

           {
             ?subject skosxl:prefLabel ?subjectLabel .
             ?subjectLabel skosxl:literalForm ?subjectPrefLabel .
             ?subject ?p ?o .
             OPTIONAL {?o skos:prefLabel ?oL} 

           }

           FILTER regex(?subjectPrefLabel, "Trump", 'i')


         } order by ?subjectPrefLabel

此查询 returns 结果如下:

我正在尝试合并 ?o and ?oL 字段,以便它将替换 ?o 字段,当且仅当存在有效的 ?oL 字段时

我还没弄明白。如果有任何建议,请告诉我。

没有数据测试查询有点困难,但在 SPARQL 1.1 中你可以使用 BIND(IF(condition,then,else) as ?result ):

PREFIX  skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX  rdfs: <https://www.w3.org/TR/rdf-schema/>
PREFIX  ap:   <http://cv.ap.org/ns>

SELECT DISTINCT  ?subjectPrefLabel ?p ?o
WHERE
  { ?subject  skosxl:prefLabel    ?subjectLabel .
    ?subjectLabel
              skosxl:literalForm  ?subjectPrefLabel .
    ?subject  ?p                  ?o_tmp
    OPTIONAL
      { ?o_tmp  skos:prefLabel  ?oL }
    BIND(if(bound(?oL), ?oL, ?o_tmp) AS ?o)
    FILTER regex(?subjectPrefLabel, "Trump", "i")
  }
ORDER BY ?subjectPrefLabel