SPARQL which is the path connecting two objects?

半世苍凉 提交于 2019-12-06 05:49:51
Joshua Taylor

The approaches in those questions do work, although the explanations in Is it possible to get the position of an element in an RDF Collection in SPARQL? and Finding all steps in property path might help even more in understanding the solution. Here's some data to test with, if I've understood your example correctly:

@prefix : <urn:ex:>

:uri1 :a :uri2, :uri3 ;
      :b :uri4, :uri5 .

:uri2 :c :uri6 ;
      :d :uri7 .

:uri7 :a :uri8 .

Here's the query. The idea is to follow a wildcard path from the beginning (:uri1) to some point ?x. Then we take a link from ?x to its object ?o, and then find a wildcard path from ?o to the ?end (:uri8). (Note that I'm using (:|!:) as a property wildcard; it matches everything since every IRI is either : or not :.)

prefix : <urn:ex:>

select ?x ?p where {
  :uri1 (:|!:)* ?x .
  ?x ?p ?o .
  ?o (:|!:)* :uri8 .
}

The results are just like what you asked for:

--------------
| x     | p  |
==============
| :uri1 | :a |
| :uri2 | :d |
| :uri7 | :a |
--------------
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!