查找包含集合中所有值的列表?

Find lists containing ALL values in a set?

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

<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 的每个绑定,而不是所有绑定的集合。

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")   
}

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

如果您要查找包含 全部 多个值的列表,则需要使用更复杂的查询。该查询查找所有具有 ?list 值的 ?s 值,然后过滤掉那些 not 列表中 的单词。单词列表使用 values 块指定。

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"))。这是一个带有结果的完整示例:

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"  |
-----------------------------------