3

我是语义网概念的新手,我有一个基于语义网的文凭作品。在我的 OWL 本体中,我以下列方式定义了个体:

<Announce rdf:ID="Ann1" ...>
  <...>
  <...>
  <requiredTechnologies>
    <Technology rdfs:resource="tech1"/>
  </requiredTechnologies>
  <requiredTechnologies>
    <Technology rdfs:resource="tech2"/>
  </requiredTechnologies>
</Announce>
...

基本上,有些属性对于给定的个人只出现一次,但属性“所需技术”可以在一个记录中多次使用(对于一个人)。因此,当我运行 SPARQL 查询并选择所有数据(我使用 Jena)时,我得到以下输出:

=====================================
| "Ann1"      | ... | ... | "tech1" |
| "Ann1"      | ... | ... | "tech2" |
| "Ann2"      | ... | ... | "tech3" |
| "Ann3"      | ... | ... | "tech4" |
| "Ann3"      | ... | ... | "tech5" |
| "Ann3"      | ... | ... | "tech6" |
| ...         | ... | ... | ...     |
=====================================

存在多个“requiredTechnologies”属性的记录不止一次出现。我的问题是如何在一行中获取属于给定个人的所有技术(类似这样):

===================================================
| "Ann1"      | ... | ... | "tech1, tech2"        |
| "Ann2"      | ... | ... | "tech2"               |
| "Ann3"      | ... | ... | "tech4, tech5, tech6" |
| ...         | ... | ... | ...                   |
===================================================

SPARQL 中有没有办法在列表中获取多个属性?或任何其他解决方法?

4

1 回答 1

3

Yes, use the GROUP BY clause in conjunction with the GROUP_CONCAT aggregate.

Here's a generic example, you should easily be able to adapt to your data model:

SELECT ?s (GROUP_CONCAT(?value ; SEPARATOR = ",") AS ?values)
WHERE
{
  ?s <http://somePredicate> ?value .
}
GROUP BY ?s
于 2013-08-23T16:30:54.587 回答