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
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>.
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.)
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.
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:_)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With