【问题标题】:Use a numeric value as string value in SPARQL在 SPARQL 中使用数值作为字符串值
【发布时间】:2015-05-15 16:15:07
【问题描述】:

是否可以在 SPARQL 查询中以某种方式使用数值作为字符串值?例如,考虑以下 RDF 数据、查询和所需结果:

知识库

@prefix gr:  <http://purl.org/goodrelations/v1#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.

:o a gr:QuantitativeValueFloat;
     gr:hasMinValueFloat "1,0"^^xsd:float
     gr:hasMaxValueFloat "10,0"^^xsd:float

查询

PREFIX gr: <http://purl.org/goodrelations/v1#>    
SELECT ?o ?v
WHERE {
  ?o a gr:QuantitativeValueFloat;
       gr:hasMinValueFloat ?vMin;
       gr:hasMaxValueFloat ?vMax.
  CONCAT((?vMin, ?vMax) as ?v)
}

理想结果

-----------------
| o  | v        | 
=================
| :o | 1,0-10,0 | 
-----------------

【问题讨论】:

    标签: sparql linked-data


    【解决方案1】:

    在 RDF 中,所有文字都有一个可以通过str 函数获得的词法形式。 SPARQL 还包括某些类型文字的速记。例如,你可以写 1 而不是 "1"^^xsd:integer,但它们是一样的,你可以得到 "1" 通过执行 str(1)str("1"^^xsd:integer)。这意味着你可以用 strconcat:

    做你想做的事
    select ?xy where {
      values ?x { 1   }  #-- short for "1"^^xsd:integer
      values ?y { 2.5 }  #-- short for "2.5"^^xsd:decimal
    
      bind(concat(str(?x),"--",str(?y)) as ?xy)
    }
    

    ------------
    | xy       |
    ============
    | "1--2.5" |
    ------------
    

    这应该可以工作,即使文字的词法形式对于该数据类型是不合法的,例如在你有 "10,0"^^xd:float 的数据中,它应该"10.0"^^xsd:float,使用点 (.) 而不是逗号 (,)。 (我意识到分隔符有不同的约定,但 SPARQL 使用点。)

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      相关资源
      最近更新 更多