SPARQL 查找孙子的数量 [英] SPARQL to find count of grandchildren
问题描述
我正在使用 schema.org 本体.我想返回
的子类以及每个返回子类的子类数.我的查询当前返回一个子类.
I am using the schema.org ontology. I want to return the subclasses of <http://schema.org/Event>
with the number of children for each returned subclass. My query is currently returning one subclass.
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT ?uri (count(?children) as ?childrenCount) WHERE
{
?uri rdfs:subClassOf <http://schema.org/Event>.
?children rdfs:subClassOf ?uri
}
GROUP BY ?uri
我想要
的每个子类及其拥有的子类数量.
I want every subclass of <http://schema.org/Event>
with the number of children it has.
推荐答案
您没有提到其子项的错误计数"是什么.当我在 schema.org OWL 文档 上运行您的查询时,我得到以下结果:>
You don't mention what "the incorrect count of its children" is. When I run your query on the schema.org OWL document, I get these results:
-------------------------------------------------------
| uri | childrenCount |
=======================================================
| <http://schema.org/UserInteraction> | 9 |
-------------------------------------------------------
我认为这个计数是正确的.如果您期望不同的计数,请更新问题以说明您期望的什么计数,以及为什么.根据 schema.org Full Hierarchy 页面显示了这个层次结构,在该层次结构下,只有Event
是 UserInteraction
的子元素,一共有九个.
I think that this count is correct. If you expected a different count, please update the question to state what count you expected, and why. According to the schema.org Full Hierarchy page shows this hierarchy, under which the only grandchildren classes of Event
are children of UserInteraction
, and there are nine of them.
无论如何,您没有在此列表中看到更多结果的原因是查询现在只查找实际有 自己孩子的孩子,这极大地限制了您的结果.如果您想选择 Event
本身没有子代的子代,您可以在 optional
模式中选择 Event
的孙代.举例说明:
At any rate, the reason you're not seeing more results in this list is that the query now only finds children that actually have their own children, and this restricts your results significantly. You can select the grandchildren of Event
inside of an optional
pattern if you want to select the children of Event
that themselves have no children. To illustrate:
prefix schema: <http://schema.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select * where {
?child rdfs:subClassOf schema:Event .
optional {
?grandchild rdfs:subClassOf ?child
}
}
产生:
$ arq --data schemaorg.owl --query query.sparql
--------------------------------------------------
| child | grandchild |
==================================================
| schema:FoodEvent | |
| schema:Festival | |
| schema:SportsEvent | |
| schema:SaleEvent | |
| schema:EducationEvent | |
| schema:ChildrensEvent | |
| schema:DanceEvent | |
| schema:BusinessEvent | |
| schema:TheaterEvent | |
| schema:SocialEvent | |
| schema:VisualArtsEvent | |
| schema:MusicEvent | |
| schema:ComedyEvent | |
| schema:LiteraryEvent | |
| schema:UserInteraction | schema:UserCheckins |
| schema:UserInteraction | schema:UserTweets |
| schema:UserInteraction | schema:UserLikes |
| schema:UserInteraction | schema:UserPlays |
| schema:UserInteraction | schema:UserDownloads |
| schema:UserInteraction | schema:UserPlusOnes |
| schema:UserInteraction | schema:UserComments |
| schema:UserInteraction | schema:UserBlocks |
| schema:UserInteraction | schema:UserPageVisits |
--------------------------------------------------
Event
的大多数子类没有任何子类;只有 UserInteraction
可以.也就是说,既然我们看到了如何找到这些类,即使它们没有子类,计数也应该与您的原始查询非常相似:
Most subclasses of Event
don't have any subclasses; only UserInteraction
does. That said, now that we see how to find these classes, even if they don't have subclasses, the counting should fall into place with a query very much like your original:
prefix schema: <http://schema.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?child (count(?grandchild) as ?nGrandchildren) where {
?child rdfs:subClassOf schema:Event .
optional {
?grandchild rdfs:subClassOf ?child
}
}
group by ?child
当然,结果显示大多数类的计数为零,UserInteraction
的计数为 9.
The results, of course, show a count of zero for most of the classes, and nine for UserInteraction
.
$ arq --data schemaorg.owl --query query.sparql
-------------------------------------------
| child | nGrandchildren |
===========================================
| schema:ComedyEvent | 0 |
| schema:ChildrensEvent | 0 |
| schema:SportsEvent | 0 |
| schema:FoodEvent | 0 |
| schema:BusinessEvent | 0 |
| schema:Festival | 0 |
| schema:EducationEvent | 0 |
| schema:LiteraryEvent | 0 |
| schema:SaleEvent | 0 |
| schema:TheaterEvent | 0 |
| schema:SocialEvent | 0 |
| schema:UserInteraction | 9 |
| schema:MusicEvent | 0 |
| schema:DanceEvent | 0 |
| schema:VisualArtsEvent | 0 |
-------------------------------------------
这篇关于SPARQL 查找孙子的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!