grajkowski
grajkowski

Reputation: 369

SPARQL DBpedia filter out specific results

I am working on a small part where I receive all types of a resource. The thing is: I don't want to have all types, only the "http://dbpedia.org/ontology"-types. How do I filter them within a SPARQL query? I don't really care as long I receive only the ontologies.

In this query I need only the dbpedia-Ontologies "Country", "Location" "PopulatedPlace" and "Place".

SPARQL endpoint: http://de.dbpedia.org/sparql

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?type WHERE {
?i rdfs:label "Deutschland"@de ; a ?type .
}

I set up a FILTER which filters out the Ontologies. But that's not the solution as it is static and only works for this example. It also duplicates. But that's a minor problem.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?type WHERE {
?i rdfs:label "Deutschland"@de ; a ?type .
FILTER (?type = <http://dbpedia.org/ontology/Country> ||
?type = <http://dbpedia.org/ontology/PopulatedPlace> ||
?type = <http://dbpedia.org/ontology/Place> ||
?type = <http://dbpedia.org/ontology/Location>)
}

Need some suggestions or help. Thx in advance.

Upvotes: 1

Views: 1259

Answers (2)

AndyFaizan
AndyFaizan

Reputation: 1903

Well what you did is fine but probably not the correct way of doing it. What you're looking for are called owl classes. So you just need to check if the type that you're looking for is an owl:Class or not.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?type WHERE {
?i rdfs:label "Deutschland"@de ; a ?type .
?type a owl:Class .
}

Upvotes: 0

grajkowski
grajkowski

Reputation: 369

Okay i thought i shouldn't have asked, but this took me some time to realize... There is a function to filter when a string starts with the same letters...

 strstarts

Solution. Hope i could at least help someone.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?type WHERE {
?i rdfs:label "Deutschland"@de ; a ?type .
FILTER (strstarts(str(?type), "http://dbpedia.org/ontology/"))
}

Upvotes: 2

Related Questions