Search code examples
sparqlturtle-rdfblank-nodes

SPARQL query turtle file with blank nodes in the graph


I'm trying to understand blank nodes in turtle correctly, and I need to know if I have understood them correctly.

Suppose we had a turtle file:

@prefix sn: <....some uri...> .
_:a sn:name "person1";
sn:email "[email protected]" .
_:b sn:name "person2";
sn:email "[email protected]" .
_:c sn:name "person3" .

and we have SPARQL query:

PREFIX sn: <...some uri...>
SELECT ?email WHERE {
?x sn:name "person1" .
?x sn:email ?email .
}

This would only return the email of person 1, because ?x binds to the blank node label _:a... So my question is if an undefined variable ?x can still bind to a blank node that has a label, just like it would if it was not blank... So in this example, return will be:

      email
--------------------------
<mailto:[email protected]> |

Would that be correct? Thanks.


Solution

  • You guess correctly: in both cases (and as said I don't really see a difference between your two examples) the answer wil be only the e-mail of person 1.

    A blank node is, from SPARQL's perspective, a resource like any other, and it will bind variables to a blank node in exactly the same way. So in the above example, since we know that the two statements use the same blank node as their subject (since they use the same label), they will be seen by SPARQL as being about the same subject.

    One caveat: the SPARQL engine/triplestore that you use will most likely not preserve the specific blank node id a: when adding your data to a triplestore it will be replaced by some generated internal id. However, the triplestore will of course make sure that both occurrences of a in your file get replaced by the exact same generated id.