PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix owl: <http://www.w3.org/2002/07/owl#>
SELECT DISTINCT ?term
WHERE {
  { 
    ?s1 ?p1 ?term . 
    FILTER(?p1!=rdf:type)
  }
  UNION
  { 
    ?term ?p2 ?o2 .
    FILTER(?o2!=owl:Ontology) 
  }
  FILTER(isIRI(?term))
}