SPARQL:获取某个类的子类的所有实体 [英] SPARQL: Get all the entities of subclasses of a certain class
问题描述
我必须在SPARQL中获得C类的所有实例以及C的子类(直接或间接)。
I've to get all the instances of a class C and subclasses (direct or indirect) of C, in SPARQL.
我可以得到所有直接子类。 C的方式如下:
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.
据我所知(我已经对其进行了预先计算),所有子类(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>.
}
感谢大家。
编辑:
我已经解决了,即使不是很优雅。
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 }
}
推荐答案
更好的解决方案是在SPARQL 1.1中使用属性路径表达式
A better solution is to use property path expressions in SPARQL 1.1
这将重写为:
SELECT ?entity
WHERE {
?entity rdf:type ?type.
?type rdfs:subClassOf* :C.
}
这篇关于SPARQL:获取某个类的子类的所有实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!