How to exclude resources with a specific rdf:type from SPARQL results?
Asked Answered
F

1

7

I have this query SPARQL that I ran on it.dbpedia.org/sparql:

select ?resource where {
  ?resource rdfs:label "Piemonte"@it
}

I get this result:

http://it.dbpedia.org/resource/Categoria:Piemonte
http://it.dbpedia.org/resource/Piemonte

I would like to have only http://it.dbpedia.org/resource/Piemonte as a result. I am trying to write this query SPARQL to delete http://it.dbpedia.org/resource/Categoria:Piemonte from the results:

select ?resource where {
  ?resource rdfs:label "Piemonte"@it
  FILTER (rdf:type != skos:Concept)
}

because I noticed that http://it.dbpedia.org/resource/Categoria:Piemonte has the object skos:Concept whereas the http://it.dbpedia.org/resource/Piemonte doesn't, but I get the same result. Why? What am I doing wrong here?

I also tries adding LIMIT 1, but the result was http://it.dbpedia.org/resource/Categoria:Piemonte, since the results aren't guaranteed to be in the same order.

Frink answered 11/6, 2014 at 15:46 Comment(0)
B
10

With a filter like FILTER (rdf:type != skos:Concept) you're just asking whether two constants are unequal. The URIs rdf:type and skos:Concept are, of course, different.

What you want is a resource that doesn't have the value skos:Concept for the property rdf:type. You'd indicate that it does have that by ?resource rdf:type skos:Concept. So your query just needs a filter that ensures that that triple does not exist in the data. On the Italian DBpedia, you can ask the following and get just one result back.

select ?resource where {
  ?resource rdfs:label "Piemonte"@it
  filter not exists { ?resource rdf:type skos:Concept }
}

SPARQL results

Barron answered 11/6, 2014 at 17:58 Comment(3)
It works! I have another question. If I write this query: prefix rdfs: <w3.org/2000/01/rdf-schema#> prefix skos: <w3.org/2009/08/skos-reference/skos.html#> prefix rdf: <w3.org/1999/02/22-rdf-syntax-ns#> select ?resource where { ?resource rdfs:label "Piemonte"@it filter not exists { ?resource rdf:type skos:Concept } } I get this SPARQL result: it.dbpedia.org/resource/Categoria:Piemonte it.dbpedia.org/resource/Piemonte Why? What is the difference between two queries? I have added only the prefixes!Frink
I have resolved in this way: prefix rdfs: <w3.org/2000/01/rdf-schema#> prefix skos: <w3.org/2004/02/skos/core#> prefix rdf: <w3.org/1999/02/22-rdf-syntax-ns#> select ?resource where { ?resource rdfs:label "Piemonte"@it filter not exists { ?resource rdf:type skos:Concept } } I had wrong the skos prefix.Frink
@Frink Comments are for requesting clarification on a question/answer, if you have new questions ask them as new questions linking back to your original question as a reference where relevantAbarca

© 2022 - 2024 — McMap. All rights reserved.