Sparql 查询返回值整数,无法过滤它以获取 int

Sparql query returned value integer, cant filter it to get the int

虽然查询returns国名没有过滤人口,而我使用过滤器取整数returns什么都没有。

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 ) 

 }

我不确定你想用这个 FILTER 条件实现什么,但你的查询 returns 零结果的原因是 none 的总体值是数据类型 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 的值。这是一个 不同的 数据类型,虽然每个非负整数当然也是一个整数,但大多数 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" )
 }