0

I am relatively new to SPARQL and struggling with the difference of the following two query examples:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?email
WHERE {
  ?person a foaf:Person.
  ?person foaf:name ?name.
  ?person foaf:mbox ?email.
}

vs.

PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX foaf:  <http://xmlns.com/foaf/0.1/>
SELECT ?name ?img ?hp ?loc
WHERE {
  ?a a mo:MusicArtist ;
     foaf:name ?name ;
     foaf:img ?img ;
     foaf:homepage ?hp ;
     foaf:based_near ?loc .
}

My question: why is the '?person' variable name in the first example needed on every line whereas the '?a' variable name in the second example is only used once?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Airlike
  • 233
  • 2
  • 6
  • 14
  • I've closed this as a duplicate of [a question about the meaning of semicolon (;) in SPARQL queries](http://stackoverflow.com/questions/18214338/meaning-of-sparql-operator). The short answer is that `.` terminates a triple, and another pattern follows, but that `;` terminates a triple, and is followed by another predicate and object. so `a b c . a d e .` is the same as `a b c ; d e .` There's also a `,` that lets you terminate a triple, but follow with another object for the same property, so you can turn `a b c . a b d .` into `a b c, d .`. You can combine these, too, so that you can turn – Joshua Taylor Sep 15 '15 at 15:32
  • `a p b . a p c . a q d . a q e.` into `a p b, c ; q d, e .`, which looks terrible on a single line, but can work quite nicely when formatted well. – Joshua Taylor Sep 15 '15 at 15:33

1 Answers1

0

It's a short hand. See that ";" at the end of the line? It means "next triple pattern uses the same subject as this one".

http://www.w3.org/TR/sparql11-query/#QSynTriples

?a a mo:MusicArtist ;
   foaf:name ?name ;
   foaf:img ?img ;
   foaf:homepage ?hp ;
   foaf:based_near ?loc .

is a block that is exactly the same triple patterns as

?a a mo:MusicArtist .
?a foaf:name ?name .
?a foaf:img ?img .
?a foaf:homepage ?hp .
?a foaf:based_near ?loc .

It's just syntax.

AndyS
  • 16,345
  • 17
  • 21