您可能会发现这个描述 SPARQL 的答案group_concat
很有用:
在 SPARQL 中,当您拥有一组查询解决方案的结果集时,您可以group
基于一个或多个变量,合并具有这些共同变量的解决方案。例如,考虑数据
@prefix : <http://example.org/people/>.
:person1 :hasChild :child1, :child2, :child3 .
:person2 :hasChild :child4, :child5 .
:person3 :hasChild :child6 .
如果您对其运行以下查询
prefix : <http://example.org/people/>
select ?person ?child where {
?person :hasChild ?child .
}
你会得到这样的结果:
$ arq --data data.n3 --query query.sparql
----------------------
| person | child |
======================
| :person3 | :child6 |
| :person2 | :child5 |
| :person2 | :child4 |
| :person1 | :child3 |
| :person1 | :child2 |
| :person1 | :child1 |
----------------------
像您在问题中那样遍历结果将产生您当前获得的输出类型。我们想要做的是实际得到如下结果:
$ arq --data data.n3 --query query.sparql
----------------------------------------
| person | child |
========================================
| :person3 | :child6 |
| :person2 | :child4, :child5 |
| :person1 | :child1, :child2, :child3 |
----------------------------------------
这正是我们group_by
可以做的。像这样的查询:
prefix : <http://example.org/people/>
select ?person (group_concat(?child;separator=' and ') as ?children) where {
?person :hasChild ?child .
}
group by ?person
产生(注意结果中的变量是?children
,而不是?child
,因为我们已经用来group_concat(...) as ?children
创建新变量?children
):
$ arq --data data.n3 --query query.sparql
---------------------------------------------------------------------------------------------------------------------------
| person | children |
===========================================================================================================================
| :person3 | "http://example.org/people/child6" |
| :person1 | "http://example.org/people/child3 and http://example.org/people/child2 and http://example.org/people/child1" |
| :person2 | "http://example.org/people/child5 and http://example.org/people/child4" |
---------------------------------------------------------------------------------------------------------------------------
如果您使用这样的查询并遍历结果并按原样打印它们,您将获得所需的输出。如果您确实想http://example.org/people/
从人和孩子身上剥离前导,您将需要更多的字符串处理。例如,使用STRAFTER删除http://example.org/people/
前缀,您可以使用如下查询:
prefix : <http://example.org/people/>
select
(strafter(str(?personX),"http://example.org/people/") as ?person)
(group_concat(strafter(str(?child),"http://example.org/people/");separator=' and ') as ?children)
where {
?personX :hasChild ?child .
}
group by ?personX
得到如下结果:
$ arq --data data.n3 --query query.sparql
----------------------------------------------
| person | children |
==============================================
| "person3" | "child6" |
| "person2" | "child5 and child4" |
| "person1" | "child3 and child2 and child1" |
----------------------------------------------
当您进行打印时,它会给您带来类似的结果
person3 has children child6
person2 has children child5 and child4
person1 has children child3 and child2 and child1