PREFIX rdf: PREFIX rdfs: PREFIX foaf: PREFIX dc: PREFIX dcterms: SELECT DISTINCT ?title WHERE { ?class rdfs:subClassOf foaf:Document . ?doc rdf:type ?class . ?doc dc:title ?title . ?bag2 ?member2 ?doc . ?doc2 dcterms:references ?bag2 OPTIONAL { ?class3 rdfs:subClassOf foaf:Document . ?doc3 rdf:type ?class3 . ?doc3 dcterms:references ?bag3 . ?bag3 ?member3 ?doc OPTIONAL { ?class4 rdfs:subClassOf foaf:Document . ?doc4 rdf:type ?class4 . ?doc4 dcterms:references ?bag4 . ?bag4 ?member4 ?doc3 } FILTER (!bound(?doc4)) } FILTER (!bound(?doc3)) }