I want to detect all values that satisfy a condition on the value of a property using a SPARQL query.
For example, imagine that I want to detect all resources in which the value of rdfs:label
has type xsd:string
.
A definition in logic could be:
∀x(stringLabel(x) ≡ ∀y(rdfs:label(x,y)→xsd:string(y)))
I have found a way to do it in SPARQL as:
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)
}
which seems to work ok with the following data:
@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 .
However, it does not return the value :x
because it does not have rdfs:label
and it does not return 0 as the count c0
.
Is there a way to count the resources that have some property returning 0 for the resources that don't have that property?