如何串联sparql中的值列表? [英] How to concatenate a list of values in sparql?

查看:79
本文介绍了如何串联sparql中的值列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个uri http://dbpedia.org/page/Manmohan_Singh



当我编写类似



<$ p的查询时,他现在有一个标记为dbpprop:years的年份列表。 $ p> PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:< http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia:< http://dbpedia.org/resource/> PREFIX dcterms:< http://purl.org/dc/terms/>
PREFIX dbpedia-owl:< http://dbpedia.org/ontology/> PREFIX类别:< http://dbpedia.org/resource/Category:>
PREFIX xsd:< http://www.w3.org/2001/XMLSchema#> PREFIX foaf:< http://xmlns.com/foaf/0.1/> PREFIX dbpprop:< http ://dbpedia.org/property/>
PREFIX dbprop:< http://dbpedia.org/property/> PREFIX grs:< http://www.georss.org/georss/>
PREFIX类别:< http://dbpedia.org/resource/Category:>
PREFIX猫头鹰:< http://www.w3.org/2002/07/owl#>
PREFIX dbpprop:< http://dbpedia.org/property/>
PREFIX泡沫:< http://xmlns.com/foaf/0.1/>
选择DISTINCT?x?name?abs?birthDate?birthplace?year?party?office?wiki在哪里{
?x owl:sameAs? dbpedia:Manmohan_Singh。
?x dbpprop:name?名称。
?x dbpedia-owl:birthDate?birthDate。
?x dbpedia-owl:birthPlace?出生地。
?x dbpprop:years?year。
?x dbpprop:party?party。
?x dbpedia-owl:office?office。
?x foaf:isPrimaryTopicOf?wiki。
?x rdfs:评论?abs。
FILTER(lang(?abs)='en')


}

我在不同的行中得到每年的结果..因此重复其他列的数据。



道具dbpedia-Similary-是否有办法像所有年来一样在一个柱状逗号中以列表的形式列出它? owl:office

解决方案

这类似于汇总来自SPARQL查询的结果,但是实际上这个问题要复杂得多,因为存在多个具有多个结果的变量。 ?名称?办公室?birthPlace



您可以使用 group_concat 解决此问题,但是您需要使用<$ c $还有c> distinct ,以免在连接字符串中多次重复获得相同的?year group by 减少了解决方案中的行数,但是在每一行中,您都有变量的 set 个值您没有分组的依据。例如,由于?year 不在的组中,因此您有一个 set ?year 的值,您必须对它们进行一些处理。例如,您可以 select(样本(?year)作为?aYear)从集合中仅获取一个,也可以按照我们在此处所做的操作,然后 select(group_concat(distinct?year; separator =,作为?years))将不同的值连接到字符串中。



您将需要如下查询,该查询将产生一行:

  SELECT?x 
(group_concat(distinct?name; separator =;)作为?names)
?abs
?birthDate
(group_concat(distinct?birthplace; separator =,)作为?birthPlaces
(group_concat(distinct?year; separator =,)as?years)
?party
(group_concat(distinct?office; separator =,)as?offices)
?wiki
在哪里{
?x owl:sameAs? dbpedia:Manmohan_Singh。
?x dbpprop:name?名称。
?x dbpedia-owl:birthDate?birthDate。
?x dbpedia-owl:birthPlace?出生地。
?x dbpprop:years?year。
?x dbpprop:party?party。
?x dbpedia-owl:office?office。
?x foaf:isPrimaryTopicOf?wiki。
?x rdfs:评论?abs。
FILTER(langMatches(lang(?abs), en))
}
group by?x?abs?birthDate?party?wiki

SPARQL结果


Suppose I have a uri http://dbpedia.org/page/Manmohan_Singh now he has a list of years in his tag dbpprop:years.

When I write a query like

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX dcterms: <http://purl.org/dc/terms/>
            PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>PREFIX category: <http://dbpedia.org/resource/Category:>
            PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dbpprop: <http://dbpedia.org/property/>
            PREFIX dbprop: <http://dbpedia.org/property/>PREFIX grs: <http://www.georss.org/georss/>
            PREFIX category: <http://dbpedia.org/resource/Category:>
            PREFIX owl: <http://www.w3.org/2002/07/owl#>
            PREFIX dbpprop: <http://dbpedia.org/property/>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            SELECT DISTINCT ?x ?name ?abs ?birthDate ?birthplace ?year ?party ?office ?wiki WHERE {
            ?x owl:sameAs? dbpedia:Manmohan_Singh.
            ?x dbpprop:name ?name.
            ?x dbpedia-owl:birthDate ?birthDate.
            ?x dbpedia-owl:birthPlace ?birthplace.
            ?x dbpprop:years ?year.
            ?x dbpprop:party ?party.
            ?x dbpedia-owl:office ?office.
            ?x foaf:isPrimaryTopicOf ?wiki.
            ?x rdfs:comment ?abs.
            FILTER(lang(?abs) = 'en')


            }

I get result of each year in different row .. and hence repeating data for other collumns. Is there a way I can get it as a list in just one collumn like all the years in one collumn comma separated or smthng like that?

Similary for the prop dbpedia-owl:office

解决方案

This is similar to Aggregating results from SPARQL query, but the problem is actually a bit more complex, because there are multiple variables that have more than one result. ?name, ?office, and ?birthPlace have the same issue.

You can work around this using group_concat, but you'll need to use distinct as well, to keep from getting, e.g., the same ?year repeated multiple times in your concatenated string. group by reduces the number of rows that you have in a solution, but in each of those rows, you have a set of values for the variables that you didn't group by. E.g., since ?year isn't in the group by, you have a set of values for ?year, and you have to do something with them. You could, e.g., select (sample(?year) as ?aYear) to grab just one from the set, or you could do as we've done here, and select (group_concat(distinct ?year;separator=", ") as ?years) to concatenate the distinct values into a string.

You'll want a query like the following, which produces one row:

SELECT ?x
       (group_concat(distinct ?name;separator="; ") as ?names)
       ?abs
       ?birthDate
       (group_concat(distinct ?birthplace;separator=", ") as ?birthPlaces)
       (group_concat(distinct ?year;separator=", ") as ?years)
       ?party
       (group_concat(distinct ?office;separator=", ") as ?offices)
       ?wiki
WHERE {
  ?x owl:sameAs? dbpedia:Manmohan_Singh.
  ?x dbpprop:name ?name.
  ?x dbpedia-owl:birthDate ?birthDate.
  ?x dbpedia-owl:birthPlace ?birthplace.
  ?x dbpprop:years ?year.
  ?x dbpprop:party ?party.
  ?x dbpedia-owl:office ?office.
  ?x foaf:isPrimaryTopicOf ?wiki.
  ?x rdfs:comment ?abs.
  FILTER(langMatches(lang(?abs),"en"))
}
group by ?x ?abs ?birthDate ?party ?wiki

SPARQL results

这篇关于如何串联sparql中的值列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆