OPTIONAL

If one wants to select from a knowledge base optional information (e.g. mailbox for a person) one can use the OPTIONAL keyword provided by SPARQL. It provides the means to express some Patterns that whenever they are available will also be processed via the Graph Pattern. Int he case the this optional keyword Pattern is not available than it will not be processed but the expression before the OPTIONAL keyword will be processed and part of the answer. With the OPTIONAL Parameter one can put optional matches or optional restrictions onto the Graph Pattern Match that has to be performed against the knowledge base. Therefore one can has with one single Query Expression have included information whenever it it available.

  • The keyword OPTIONAL selects optional elements from the RDF Graph
  • complies to a Left Outer Join

Data:

   # Default Graph (stored at http://examples.org/addresses)
   @prefix foaf: <http://xmlns.com/foaf/0.1>] .
   @prefix rdf: <http://www.w3.org/1999/02/22-rdf-synthax-ns#>] .

   _:a rdf:type foaf:Person .
   _:a foaf:name "Hevok" .
   _:a foaf:mbox <mailto:hevok@example.com>
   _:a foaf:mbox <mailto:hevok@work.example.com>

   _:b rdf:type foaf:Person .
   _:b foaf:name "Eva" .

Query:

   PREFIX foaf: <http://xmlns.com/foaf/0.1/>]
   SELECT ?name ?mbox
   FROM <http://example.org/addresses>]
   WHERE { ?x foaf:name ?name }
         OPTIONAL { ?x foaf:mbox ?mbox }
   }
wiki-globe1.jpg/
Edit tutorial

Comment on This Data Unit