【发布时间】:2014-08-14 19:44:25
【问题描述】:
我想使用 SPARQL 查询检测所有满足属性值条件的值。
例如,假设我想检测rdfs:label 的值类型为xsd:string 的所有资源。
逻辑上的定义可以是:
∀x(stringLabel(x) ≡ ∀y(rdfs:label(x,y)→xsd:string(y)))
我在 SPARQL 中找到了一种方法:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix : <http://example.org/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
select ?s where {
# Count arcs with property rdfs:label
{ SELECT ?s (COUNT(*) AS ?c0) {
?s rdfs:label ?o .
} GROUP BY ?s
}
# Count arcs with property rdfs:label and value xsd:string
{ SELECT ?s (COUNT(*) AS ?c1) {
?s rdfs:label ?o . FILTER ((isLiteral(?o) && datatype(?o) = xsd:string))
} GROUP BY ?s
}
# filter out those resources that have rdfs:label
# with values not in xsd:string
FILTER (?c0 = ?c1)
}
这似乎适用于以下数据:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://example.org/> .
:peter rdfs:label "Peter" ; foaf:knows :anna .
:anna rdfs:label "Anna" .
:r22 rdfs:label 22 .
:x foaf:knows :r22 .
但是,它不返回值 :x,因为它没有 rdfs:label,并且它不返回 0 作为计数 c0。
对于没有该属性的资源,有没有办法计算具有某些属性返回 0 的资源?
【问题讨论】: