简体   繁体   中英

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.

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/"))
}

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 .
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM