如何将变量绑定到SPARQL中的查询项
在这个简单的 sparql 查询中,我得到一个对象为 42 的主题列表。
SELECT ?v WHERE { ?v ?p 42 }
如果我添加 ?p 作为变量,
SELECT ?v ?p WHERE { ?v ?p 42 }
我将每行得到两个实体,即主语和谓语。 如果我想要三个实体(包括 42 个)怎么办?像这样的东西:
SELECT ?v ?p ?m WHERE { ?v ?p (42 as m) }
In this simple sparql query I get a list of subjects whose object is 42
SELECT ?v WHERE { ?v ?p 42 }
If I add ?p as a variable
SELECT ?v ?p WHERE { ?v ?p 42 }
I will get two entities per row, the subject and the predicate.
What if I wanted three entities, so including the 42? Something like:
SELECT ?v ?p ?m WHERE { ?v ?p (42 as m) }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另一种变体是使用 BIND,例如:
BIND 语句只是添加 ?m 的绑定,然后可以为结果集选择该绑定。
Another variant is to use BIND, e.g.:
The BIND statement simply adds a binding for ?m, which can then be selected for the result set.
在 SPARQL 1.1 中,您可以使用 VALUES 来实现此目的。你会写
In SPARQL 1.1, you can use VALUES for this. You would write
标准 SPARQL 1.0 并不允许这样做。不过,可能有一些特定于实现的扩展可以实现这一点。
作为解决方法,如果数据包含 42 作为对象文字的三元组,您可以这样做,例如:
这相当于
您可以使用逗号对象列表表示法编写共享相同主语和谓词的图形模式,并且
WHERE
关键字是可选的。为了提高效率,您希望使用基本图形模式将工作三元组减少到较小的集合,然后再应用 FILTER 表达式来进一步修剪结果。
Standard SPARQL 1.0 does not really allow that. There may be some implementation-specific extensions for doing it, though.
As a workaround, if the data contains a triple with 42 as an object literal, you can do it e.g. like this:
which is equivalent with
as you can write graph patterns sharing the same subject and predicate with the comma object list notation, and the
WHERE
keyword is optional.For efficiency, you want to use basic graph patterns to reduce the working triple to a smaller set and only then apply FILTER expressions to further prune the results.
您可以使用 BINDINGS 关键字和 FILTER 通过两种方式完成
使用 BINDINGS
使用 FILTER
You can accomplish in two ways using BINDINGS keyword as well as FILTER
Using BINDINGS
Using FILTER
我知道这是迂回的,但我相信这可以通过子查询实现。
这是一个有用的模式,可以帮助您在缩小范围内进行查询,然后再将其释放到整个数据集上:
I know this is round-about, but I believe this is doable with a subquery.
This is a useful pattern to help you work on the query in the narrow, before you let it loose on your entire dataset: