I'm looking to use SPARQL for a relatively basic task: Given a FOAF graph, I'd like to parse the elements I find in there, get their tags (if they exist) and then, use those as new graphs from which to find information about those people.
So for instance, you could imagine a simple use case where I want to run a SPARQL query to list all of my favorite foods (as per my FOAF file), and also the favorite foods of all my friends.
Here is what this looks like at the moment. Note that for testing purposes, at the moment all I'm trying to do with the query below is fetch the name of the friend, through the ?name3 variable. Running this query doesn't return any results for ?graph and ?name3, even though I know that the rdfs:seeAlso link to some valid RDF files, of which at least two should have a name attribute. Thanks for any input you might have!
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name1 ?name2 ?graph ?name3
FROM <my-rdf-file>
WHERE {
?person1 foaf:knows ?person2 .
?person1 foaf:name ?name1 .
?person2 foaf:name ?name2 .
OPTIONAL {
?person2 rdfs:seeAlso ?graph .
GRAPH ?graph {
?person3 foaf:name ?name3 .
}
}
}
GRAPH doesn't implicitly fetch remote data into the store, that would be too much of a security risk. There may be some systems where you can enable this, but it's non-standard.
However, in SPARQL 1.1 Update, there's a keyword LOAD, which does this though, you can write:
LOAD <uri>
Which will fetch the graph into the store, so you could write:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?graph
FROM <my-rdf-file>
WHERE {
?person1 foaf:knows ?person2 .
?person1 foaf:name ?name1 .
?person2 foaf:name ?name2 .
OPTIONAL {
?person2 rdfs:seeAlso ?graph .
}
}
Feed the bindings for ?graph into a set of LOAD statements, and then run your original query.
N.B. in some systems, e.g. 4store you need to enable LOAD, it's not allowed by default, so check the documentation of the store you're using.