首页 > 解决方案 > Sparql 查询返回值整数,无法过滤它以获取 int

问题描述

尽管查询返回国家/地区名称而不过滤人口,但我使用过滤器获取整数时什么也不返回。

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 ) 

 }

标签: xsdintegersparqldbpedia

解决方案


我不确定你想用这个 FILTER 条件实现什么,但你的查询返回零结果的原因是没有一个总体值是 datatype xsd:integer

顺便说一句,您的查询中还有其他问题:使用str()SELECT 子句中的函数而不将该函数的结果绑定到新变量(使用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" )

}

您将看到结果仅包含 datatype 的值xsd:nonNegativeInteger。这是一种不同的数据类型,虽然每个非负整数当然也是整数,但这种形式的数据类型推断并不是大多数 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 ) 

 }

您将取回属于该数据类型的所有值 - 不过,如前所述,我不确定您在这里实现了什么,因为这与您在没有数据类型检查的情况下得到的结果相同,尽我所能告诉。

如果您的目标是取回数字,但表示为 axsd:integer而不是 a 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" )
 }

推荐阅读