SPARQL: Get all the entities of subclasses of a certain class

只愿长相守 提交于 2019-12-22 04:00:13

问题


I've to get all the instances of a class C and subclasses (direct or indirect) of C, in SPARQL.

I can get all the direct subclasses of C in this way:

SELECT ?entity
WHERE {
  ?subclass rdfs:subClassOf :C .
  ?entity rdf:type ?subclass .
}

But I can't get the instances of an indirect subclass and neither any instance of C.

As I know (I've pre-calculated them) all the subclasses (direct and indirect of C), and I can build a dynamic query, is it possible build a query like the following one?

SELECT ?entity
WHERE {
  ?entity rdf:type in <list>.
}

Thanks to everyone.

EDIT:

I've just solved it, even if in a not elegant way.

SELECT ?entity
WHERE {
  { ?entity rdf:type :C }
  UNION { ?entity rdf:type :SubClass1 }
  UNION { ?entity rdf:type :SubClass2 }
  UNION { ?entity rdf:type :SubClass3 }
}

回答1:


A better solution is to use property path expressions in SPARQL 1.1

This would be rewritten as:

SELECT ?entity
WHERE {
  ?entity rdf:type ?type.
  ?type rdfs:subClassOf* :C.
}



回答2:


Based on the SPARQL 1.1 specification the proper way to do it would be:

SELECT ?entity
WHERE {
    ?entity rdf:type/rdfs:subClassOf* :C
}

Without support for property paths there is no way of expressing class hierarchies of arbitrary length.



来源:https://stackoverflow.com/questions/9209577/sparql-get-all-the-entities-of-subclasses-of-a-certain-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!