SPARQL仅搜索xsd:integer,无小数

时间:2015-04-20 19:05:53

标签: rdf sparql jena owl pellet

使用以下查询:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX uni: <http://localhost/SemanticSearch/semanticsearch.owl#>

SELECT
    DISTINCT * 
WHERE {
    ?uri uni:altLabel "5"^^xsd:integer.
    ?uri rdf:type ?type
}

还返回altLabel xsd:decimal 5.x的URI 我真的需要它只返回?uri altLabel xsd:integer。反正有没有实现这个目标?

1 个答案:

答案 0 :(得分:5)

如果您能提供我们可以查询的实际数据,那么它总是更容易。将来,请提供我们可以查询的数据。因为那时我们实际上可以测试它的查询。在任何情况下,这是一个非常简单的数据集,有两个资源,一个具有xsd:decimal值,另一个具有xsd:integer值。

@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix uni: <http://localhost/SemanticSearch/semanticsearch.owl#>.
@prefix : <urn:ex:>.

:a uni:altLabel "5"^^xsd:integer ; a :somethingWithAnInteger .
:b uni:altLabel "5"^^xsd:decimal ; a :somethingWithADecimal .

您可以使用数据类型功能过滤所需的特定数据类型:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX uni: <http://localhost/SemanticSearch/semanticsearch.owl#>

SELECT DISTINCT * WHERE {
  ?uri uni:altLabel ?altLabel .
  ?uri rdf:type ?type
  filter(?altLabel = "5"^^xsd:integer && datatype(?altLabel) = xsd:integer)
}

-----------------------------------------------------------
| uri        | altLabel | type                            |
===========================================================
| <urn:ex:a> | 5        | <urn:ex:somethingWithAnInteger> |
-----------------------------------------------------------