Sparql查询返回的值整数,无法对其进行过滤以获取int

问题描述 投票:0回答:1

尽管查询返回的国家名称没有过滤人口,但是我使用过滤器取整数却什么也没返回。

PREFIX  dbo:  <http://dbpedia.org/ontology/>

SELECT DISTINCT  str(?country_label) ?population
WHERE
  { 
?country  dct:subject  <http://dbpedia.org/resource/Category:Countries_in_Europe>.
?country rdfs:label ?country_label.
?country dbo:populationTotal ?population.
FILTER langMatches( lang(?country_label), "EN" )
FILTER datatype((?population) = xsd:integer ) 

 }
xsd integer sparql dbpedia
1个回答
0
投票

我不确定您要在这种FILTER条件下实现什么,但是查询返回零结果的原因是,所有填充值都不是数据类型xsd:integer

此外,查询中还有其他问题:在SELECT子句中使用类似str()的函数而不将该函数的结果绑定到新变量(使用as ?new_var)是不合法的SPARQL。我知道DBPedia端点允许它,但是您应该知道,如果您在其他SPARQL引擎上尝试它,可能会给您带来错误。

如果运行不带数据类型过滤器的查询:

PREFIX  dbo:  <http://dbpedia.org/ontology/>
SELECT DISTINCT  (str(?country_label) as ?cl) ?population
WHERE
  { 
?country  dct:subject  <http://dbpedia.org/resource/Category:Countries_in_Europe>.
?country rdfs:label ?country_label.
?country dbo:populationTotal ?population.
FILTER langMatches( lang(?country_label), "EN" )

}

您将看到result仅包含数据类型xsd:nonNegativeInteger的值。那是一个[[different

数据类型,尽管每个非负整数也是一个整数当然是事实,但是这种格式的数据类型推断并不是大多数SPARQL端点支持的。]也:在原始的人口筛选条件下,括号放置错误。如果您按以下方式修改查询:

PREFIX dbo: <http://dbpedia.org/ontology/> SELECT DISTINCT (str(?country_label) as ?cl) ?population WHERE { ?country dct:subject <http://dbpedia.org/resource/Category:Countries_in_Europe>. ?country rdfs:label ?country_label. ?country dbo:populationTotal ?population. FILTER langMatches( lang(?country_label), "EN" ) FILTER ( datatype(?population) = xsd:nonNegativeInteger ) }

[您将取回该数据类型的所有值-但是,正如我所说,我不确定在这里要实现什么,因为就数据类型检查而言,这与您得到的结果相同我可以告诉。 

如果您的目标是取回数字,但用xsd:integer而不是xsd:nonNegativeInteger表示,则可以强制转换,]:

PREFIX dbo: <http://dbpedia.org/ontology/> SELECT DISTINCT (str(?country_label) as ?cl) (xsd:integer(?population) as ?pop) WHERE { ?country dct:subject <http://dbpedia.org/resource/Category:Countries_in_Europe>. ?country rdfs:label ?country_label. ?country dbo:populationTotal ?population. FILTER langMatches( lang(?country_label), "EN" ) }

© www.soinside.com 2019 - 2024. All rights reserved.