Alex King
Alex King

Reputation: 200

How to filter a SPARQL query by a property's class

Is it possible to query a filter a SPARQL query by the class of one of its properties? I have an ontology which describes films, and I wish to display all films which were filmed in Europe.

The current SPARQL query is as below:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX cinema: <http://example.com/ontologies/cinemachain#>
SELECT ?film ?location
    WHERE { 
        ?film rdf:type cinema:Film .
        ?film cinema:wasFilmedAt ?location .
        FILTER(?location rdfs:subClassOf cinema:Europe) .

}

The first two lines of the where clause give me a list of all films and their locations. How can I use the filter to return me results where the location is a subclass of cinema:Europe?

Upvotes: 1

Views: 3636

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85913

In this particular ontology, cinema:Europe is a subclass of cinema:Location. This is so that we can easily identify particular locations by continent. For this filter, I want to find all films who have a location which is a member of Europe. Do you know how I can do this using SPARQL?

OK, the naming convention is a little bit unusual, but we can work with it. You just need to require that the ?location has a type that is a subclass of Europe (including Europe itself):

select ?film ?location where {
  ?film rdf:type cinema:Film ;
        cinema:wasFilmedAt ?location .
  ?location rdf:type/rdfs:subClassOf* cinema:Europe .
}

Upvotes: 2

Related Questions