How to specify a specific class in SPARQL?

Viewed 31

I am trying to learn ontologies and SPARQL using Protégé.

I'm trying to figure out SPARQL code to show subclasses of a given class:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>               
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX untitled-ontology-10: <http://www.semanticweb.org/chris/ontologies/2022/8/untitled-ontology-10#>

SELECT DISTINCT ?subclass 
WHERE {
  ?subclass rdfs:subClassOf Class.Thing  .
} 

In the example here I'm trying to find all subclasses of the class Thing (or of any other defined class, e.g. Professions, in my project).

Class.Thing doesn't work of course. Neither does :Thing nor ns:Thing nor #Thing nor any of the other variants I've found in examples.

Of course if I use a variable in place of Class.Thing, it'll show me all classes, but I would really like to narrow it down to subclasses of a specific class.

Can anyone tell me how to do this?

1 Answers

You have to provide the URI of the class.

Either as an absolute URI between < and >:

?subclass rdfs:subClassOf <http://www.semanticweb.org/chris/ontologies/2022/8/untitled-ontology-10#YourClass> .

Or as a prefixed name (in the same way you already use rdfs:subClassOf), using the prefixes you define at the top of your query:

?subclass rdfs:subClassOf untitled-ontology-10:YourClass .

You could also use the empty prefix, if you define it as such in your SPARQL query:

PREFIX : <http://www.semanticweb.org/chris/ontologies/2022/8/untitled-ontology-10#>

# […]

?subclass rdfs:subClassOf :YourClass .

As default top-class, Protégé uses owl:Thing (http://www.w3.org/2002/07/owl#Thing), so that class is not defined under your own ontology’s URI namespace.

(You can change the URI of your ontology in Protégé. If you intend to publish your ontology and/or data, you should use a URI namespace under your control, e.g., under your own domain name.)

Related