查找包含集合中所有值的列表? [英] Find lists containing ALL values in a set?

查看:48
本文介绍了查找包含集合中所有值的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 SPARQL 中找到包含一组项目中的每一个的列表?假设我有这些数据:

How can I find lists that contain each of a set of items in SPARQL? Let's say I have this data:

<http://foo.org/test> <http://foo.org/name> ( "new" "fangled" "thing" )  . 
<http://foo.org/test2> <http://foo.org/name> ( "new" "york" "city" )  . 

如何找到列表中同时包含new"和york"的项目?下面的 SPARQL 不起作用,因为 filter 作用于 ?t 的每个绑定,而不是它们的集合.

How can I find the items whose lists contain both "new" and "york"? The following SPARQL doesn't work, since filter works on each binding of ?t, not the set of all of them.

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?s ?p WHERE {       
    ?s <http://foo.org/name>/rdf:rest*/rdf:first ?t 
    FILTER( ?t = "new" && ?t = "york")   
}

推荐答案

查找具有多个必需值的列表

如果您要查找包含all 多个值的列表,则需要使用更复杂的查询.此查询查找所有具有 ?list 值的 ?s 值,然后过滤掉那些不是一个词是 不在列表中.单词列表是使用 values 块指定的.

Finding lists with multiple required values

If you're looking for lists that contain all of a number of values, you'll need to use a more complicated query. This query finds all the ?s values that have a ?list value, and then filters out those where there is not a word that is not in the list. The list of words is specified using a values block.

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

select ?s {
  ?s <http://foo.org/name> ?list .
  filter not exists {                   #-- It's not the case
     values ?word { "new" "york" }      #-- that any of the words
     filter not exists {                #-- are not
       ?list rdf:rest*/rdf:first ?word  #-- in the list.
     }
  }
}

--------------------------
| s                      |
==========================
| <http://foo.org/test2> |
--------------------------

在列表中查找替代字符串

另一方面,如果您只是想搜索多个选项中的一个,那么您使用的是正确的属性路径,但您得到了错误的过滤器表达式.您希望字符串等于new"york".没有字符串同时等于new"york".您只需要执行 filter(?t = "new" || ?t = "york") 或更好地使用 in: filter(?t in ("new", "york")).这是一个带有结果的完整示例:

Find alternative strings in a list

On the other hand, if you're just trying to search for one of a number of options, you're using the right property path, but you've got the wrong filter expression. You want strings equals to "new" or "york". No string is equal to both "new" and "york". You just need to do filter(?t = "new" || ?t = "york") or better yet use in: filter(?t in ("new", "york")). Here's a full example with results:

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

select ?s ?t {
  ?s <http://foo.org/name>/rdf:rest*/rdf:first ?t .
  filter(?t in ("new","york"))
}

-----------------------------------
| s                      | t      |
===================================
| <http://foo.org/test2> | "new"  |
| <http://foo.org/test2> | "york" |
| <http://foo.org/test>  | "new"  |
-----------------------------------

这篇关于查找包含集合中所有值的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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