Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL query rdf container (rdf: Bag)

Tags:

rdf

sparql

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

like image 202
rholek Avatar asked Apr 20 '13 13:04

rholek


People also ask

What is RDF container?

RDF containers are used to describe group of things. For example, to list the authors of a book or to list the members in a band. The following RDF elements are used to describe such groups: <Bag>, <Seq>, and <Alt>.

What is the difference between SPARQL and SQL?

SQL does this by accessing tables in relational databases, and SPARQL does this by accessing a web of Linked Data. (Of course, SPARQL can be used to access relational data as well, but it was designed to merge disparate sources of data.)

How do I run a SPARQL query in Python?

To use as a command line script, you will need to install SPARQLWrapper and then a command line script called rqw (spaRQl Wrapper) will be available within the Python environment into which it is installed. run $ rql -h to see all the script's options.


1 Answers

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:_)))
like image 79
AndyS Avatar answered Nov 03 '22 06:11

AndyS