Skip to end of metadata
Go to start of metadata

Example SPARQL queries that can help you list the classes in an ontology.

List all classes


List all classes
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 
SELECT DISTINCT ?type
WHERE {
  ?s a ?type.
}

Note: The SPARQL keyword a is a shortcut for the common predicate rdf:type, giving the class of a resource.

List all classes with a given prefix


List all classes with a given prefix
PREFIX bc: <http://base22.com/ont/bc#>

SELECT DISTINCT ?type
WHERE {
  ?subject a ?type.
  FILTER( STRSTARTS(STR(?type),str(bc:)) )
}


List class hierarchy


List all classes with a given prefix
SELECT DISTINCT ?subject ?label ?supertype
WHERE {
    { ?subject a owl:Class . } UNION { ?individual a ?subject . } .
    OPTIONAL { ?subject rdfs:subClassOf ?supertype } .
    OPTIONAL { ?subject rdfs:label ?label }
} ORDER BY ?subject