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

Ari*_*usp 6 rdf sparql

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

<http://foo.org/test> <http://foo.org/name> ( "new" "fangled" "thing" )  . 
<http://foo.org/test2> <http://foo.org/name> ( "new" "york" "city" )  . 
Run Code Online (Sandbox Code Playgroud)

如何查找列表中包含"new"和"york"的项目?以下SPARQL不起作用,因为过滤器适用于?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")   
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*lor 9

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

如果您要查找包含所有值的列表,则需要使用更复杂的查询.此查询查找具有?列表值的所有?s值,然后筛选出没有列表中没有的单词的值.使用块指定单词列表.

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.
     }
  }
}
Run Code Online (Sandbox Code Playgroud)
--------------------------
| s                      |
==========================
| <http://foo.org/test2> |
--------------------------
Run Code Online (Sandbox Code Playgroud)

在列表中查找备用字符串

另一方面,如果您只是尝试搜索多个选项中的一个,则使用正确的属性路径,但是您的错误过滤器表达式.你希望字符串等于"new" "york".没有字符串等于"new" "york".你只需要做的过滤器(T = "新" || T = "纽约"?)或者更好的使用:过滤器(在( "新", "纽约")ΔT) .以下是结果的完整示例:

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"))
}
Run Code Online (Sandbox Code Playgroud)
-----------------------------------
| s                      | t      |
===================================
| <http://foo.org/test2> | "new"  |
| <http://foo.org/test2> | "york" |
| <http://foo.org/test>  | "new"  |
-----------------------------------
Run Code Online (Sandbox Code Playgroud)