2017-05-24 50 views
0

我對語義網和sparql非常陌生。我有一個內部本體,它使用SmartLogic來管理數據。Sparql - 條件輸出

我在寫一些簡單的查詢來開始。


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 

這個查詢看起來像返回結果:

enter image description here

我試圖合併?o and ?oL領域,使之將取代?o場,當且僅當有是一個有效的?oL字段

我還沒有弄明白。如果有任何建議,請讓我知道。

回答

1

有點困難,而不用於測試查詢數據,但在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 
+0

謝謝,這個工作,教我一個新的關鍵機制:) – Busturdust