SPARQL query rdf container (rdf: Bag)

﹥>﹥吖頭↗ 提交于 2019-12-06 22:50:06

问题


I have this RDF

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:com="http://www.example.com/com#">
  <rdf:Description rdf:about="http://www.example.com/com#1">
    <com:cities>
      <rdf:Bag>
        <rdf:li>A</rdf:li>
        <rdf:li>B</rdf:li>
        <rdf:li>D</rdf:li>
      </rdf:Bag>
    </com:cities>
    <com:name>1</com:name>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/com#2">
    <com:cities>
      <rdf:Bag>
        <rdf:li>C</rdf:li>
        <rdf:li>B</rdf:li>
      </rdf:Bag>
    </com:cities>
    <com:name>2</com:name>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/com#3">
    <com:cities>
      <rdf:Bag>
        <rdf:li>C</rdf:li>
        <rdf:li>B</rdf:li>
        <rdf:li>D</rdf:li>
      </rdf:Bag>
    </com:cities>
    <com:name>3</com:name>
  </rdf:Description>
</rdf:RDF>

Using Sparql I need to get names of all objects, which contains city D (1 and 3). I can do something like this:

SELECT ?name
WHERE {
?a com:cities ?cities.
?cities rdf:_1 ?s.
FILTER(?s = "D")
?a com:name ?name.    
}

but it will check only first element. I could make more queries, with rdf:_2, rdf:_3, ... and use UNION, but I don't know number of rdf:li in Bag. Is there something like rdf:all? Or some other solution to do this? Thanks


回答1:


Use rdfs:member

?cities rdfs:member ?s.

if your system supports it or filter on the property URI:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
...
?cities ?prop ?s. FILTER (strstarts(str(?prop), str(rdf:_)))


来源:https://stackoverflow.com/questions/16121150/sparql-query-rdf-container-rdf-bag

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