SPARQL query to construct sub-graph with select paths (paths have different lengths)

ぐ巨炮叔叔 提交于 2019-12-05 21:37:02

It's much easier to work with data if you provide it in a legal RDF serialization, so that we can easily run queries against it. Here's your data in a form that we can actually query against:

@prefix : <http://stackoverflow.com/q/20840883/1281433/> .

:A :p :B .
:A :q :C .
:A :r :D .
:A :s :E . :E :t :F .
:A :u :G . :G :v :H .

If you just want the subgraph induced by the nodes A, B, C, E, and F, you can simply ask for all triples where both the subject and object are taken from that set. E.g.:

prefix : <http://stackoverflow.com/q/20840883/1281433/>

construct {
  ?s ?p ?o
} 
where { 
    values ?s { :A :B :C :E :F }
    values ?o { :A :B :C :E :F }
    ?s ?p ?o 
}

This produces the following result (in your notation, in N3, and in RDF/XML):

A-p-B
A-q-C
A-s-E E-t-F
@prefix :      <http://stackoverflow.com/q/20840883/1281433/> .

:E      :t      :F .

:A      :p      :B ;
        :q      :C ;
        :s      :E .
<rdf:RDF
    xmlns="http://stackoverflow.com/q/20840883/1281433/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://stackoverflow.com/q/20840883/1281433/A">
    <s>
      <rdf:Description rdf:about="http://stackoverflow.com/q/20840883/1281433/E">
        <t rdf:resource="http://stackoverflow.com/q/20840883/1281433/F"/>
      </rdf:Description>
    </s>
    <q rdf:resource="http://stackoverflow.com/q/20840883/1281433/C"/>
    <p rdf:resource="http://stackoverflow.com/q/20840883/1281433/B"/>
  </rdf:Description>
</rdf:RDF>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!