在 SPARQL 中使用数值作为字符串值
Use a numeric value as string value in SPARQL
是否可以在 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 |
-----------------
在 RDF 中,所有文字都具有可以通过 str function. SPARQL also includes some shorthand for certain kinds of literals. E.g., you can write 1 instead of "1"^^xsd:integer, but they're the same thing, and you can get "1" by doing either str(1) or str("1"^^xsd:integer). That means that you can do what you're trying to do with str and concat:
获得的词法形式
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 使用点。)
是否可以在 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 |
-----------------
在 RDF 中,所有文字都具有可以通过 str function. SPARQL also includes some shorthand for certain kinds of literals. E.g., you can write 1 instead of "1"^^xsd:integer, but they're the same thing, and you can get "1" by doing either str(1) or str("1"^^xsd:integer). That means that you can do what you're trying to do with str and concat:
获得的词法形式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 使用点。)