SPARQL

716 views

Published on

SPARQL 1.0 tutorial

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
716
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
36
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SPARQL

  1. 1. SPARQL Query Language for RDF Raji GHAWI 30/03/2010
  2. 2. Introduction to SPARQL SPARQL = RDF Query Language + Protocol + XML Results Format  SPARQL has been created by the RDF Data Access Working Group (DAWG) of the W3C.  SPARQL is a W3C Recommendation since January 2008  It has a familiar looking SQL-style syntax.  Several implementations are available:  ARQ, Virtuoso, Sesame, etc.  “SPARQL will make a huge difference” Tim Berners-Lee, May 2006 2
  3. 3. Triple Patterns    A SPARQL query contains a set of triple patterns called a basic graph pattern. A triple pattern is similar to an RDF triple (subject, predicate, object), but any component can be a variable. We say that a basic graph pattern matches a subgraph of the RDF data, when RDF terms from that subgraph may be substituted for the variables.  the result of the matching is an RDF graph equivalent to the subgraph. 3
  4. 4. Turtle  Turtle is an RDF serialization   The RDF part of N3 SPARQL uses Turtle+variables as triple pattern syntax @prefix person: <http://example/person/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . person:A person:A person:B foaf:name foaf:mbox foaf:name "Alice" . <mailto:alice@example.net> . "Bob" . 4
  5. 5. SPARQL: Triple Pattern @prefix person: <http://example/person/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . person:A person:A person:B foaf:name foaf:mbox foaf:name "Alice" . <mailto:alice@example.net> . "Bob" . PREFIX person: <http://example/person/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:name ?name } ----------| name | =========== | "Bob" | | "Alice" | ----------5
  6. 6. SPARQL: Basic Graph Pattern @prefix person: <http://example/person/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . person:A person:A person:B foaf:name foaf:mbox foaf:name "Alice" . <mailto:alice@example.net> . "Bob" . PREFIX person: <http://example/person/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?person ?person foaf:mbox foaf:name <mailto:alice@example.net> . ?name . } ----------| name | =========== | "Alice" | ----------6
  7. 7. Inference :x :C rdf:type rdfs:subClassOf :C . :D . PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?type WHERE { ?x rdf:type ?type . } -------| type | ======== | :C | | :D | -------7
  8. 8. SPARQL Query Forms SPARQL has four query forms. These query forms use the solutions from pattern matching to form result sets or RDF graphs. The query forms are:    1. 2. 3. 4. SELECT – returns all, or a subset of, the variables bound in a query pattern match. CONSTRUCT – returns an RDF graph constructed by substituting variables in a set of triple templates. ASK – returns a boolean indicating whether a query pattern matches or not. DESCRIBE – returns an RDF graph that describes the resources found. 8
  9. 9. SELECT Form of SPARQL Query  A SPARQL SELECT query consists of two parts:   SELECT clause – identifies the variables to appear in the query results WHERE clause – provides the basic graph pattern to match against the data graph. 9
  10. 10. SELECT Form of SPARQL Query    The query attempts to match the triples of the graph pattern against the RDF data model. Matching means find a set of bindings such that the substitution of variables for values creates a triple that is in the set of triples making up the RDF graph. Each matching binding of the graph pattern variables to the model nodes becomes a query solution, and the values of the variables named in the SELECT clause become part of the query results. 10
  11. 11. A Dataset of RDF Triples vCard:FN http://somewhere/MattJones/ vCard:N vCard:FN http://somewhere/SarahJones/ Matt Jones vCard:Given Sarah Jones vCard:N vCard:Family vCard:Given vCard:FN http://somewhere/RebeccaSmith/ vCard:Family 23 vCard:Family vCard:Given vCard:FN http://somewhere/JohnSmith/ Matthew Jones Sarah Becky Smith info:age vCard:N Jones info:age Smith Rebecca John Smith 25 vCard:Family vCard:N vCard:Given Smith John 11
  12. 12. A Dataset of RDF Triples Subject Predicate Object <http://somewhere/MattJones/> vCard:FN "Matt Jones" <http://somewhere/MattJones/> vCard:N _:b0 _:b0 vCard:Family "Jones" _:b0 vCard:Given "Matthew" <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" <http://somewhere/RebeccaSmith/> vCard:N _:b1 _:b1 vCard:Family "Smith" _:b1 vCard:Given "Rebecca" <http://somewhere/JohnSmith/> vCard:FN "John Smith" <http://somewhere/JohnSmith/> vCard:N _:b2 _:b2 vCard:Family "Smith" _:b2 vCard:Given "John" <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" <http://somewhere/SarahJones/> vCard:N _:b3 _:b3 vCard:Family "Jones" _:b3 vCard:Given "Sarah" 12
  13. 13. SELECT ?x WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith" } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q1 "Sarah" . 13
  14. 14. SELECT ?x WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith" } Q1 --------------------------------| x | ================================= | <http://somewhere/JohnSmith/> | --------------------------------- 14
  15. 15. SELECT ?x ?fname WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> ?fname } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q2 "Sarah" . 15
  16. 16. SELECT ?x ?fname WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> ?fname } Q2 ---------------------------------------------------| x | fname | ==================================================== | <http://somewhere/RebeccaSmith/> | "Becky Smith" | | <http://somewhere/SarahJones/> | "Sarah Jones" | | <http://somewhere/JohnSmith/> | "John Smith" | | <http://somewhere/MattJones/> | "Matt Jones" | ---------------------------------------------------- 16
  17. 17. PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?givenName WHERE { ?y vCard:Family "Smith" . ?y vCard:Given ?givenName . } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q3 "Sarah" . 17
  18. 18. PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?givenName WHERE { ?y vCard:Family "Smith" . ?y vCard:Given ?givenName . } Q3 ------------| givenName | ============= | "John" | | "Rebecca" | ------------- 18
  19. 19. PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?y ?givenName WHERE { ?y vCard:Family "Smith" . ?y vCard:Given ?givenName . } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q4 "Sarah" . 19
  20. 20. PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?y ?givenName WHERE { ?y vCard:Family "Smith" . ?y vCard:Given ?givenName . } Q4 -------------------| y | givenName | ==================== | _:b1 | "John" | | _:b2 | "Rebecca" | -------------------- 20
  21. 21. FILTER clause     SPARQL allows to pose restrictions on the values in query solutions. These restrictions are defined in FILTER clauses. These clauses are, to some extent, similar to WHERE clause of an SQL query. SPARQL filters can be used to restrict:    string values (using REGEX function), numeric values (using <, >, =, <=, >= and != operators). SPARQL also provides test functions:  BOUND, isURI, isBLANK, isLITERAL 21
  22. 22. PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?g WHERE { ?y vCard:Given ?g . FILTER regex(?g, "r", "i") } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q5 "Sarah" . 22
  23. 23. PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?g WHERE { ?y vCard:Given ?g . FILTER regex(?g, "r", "i") } Q5 ------------| g | ============= | "Rebecca" | | "Sarah" | ------------- 23
  24. 24. Subject Predicate Object <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> info:age "2 "^^xsd:integer . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> info:age "25"^^xsd:integer . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given "Sarah" . 24
  25. 25. PREFIX info : <http://somewhere/peopleInfo#> SELECT ?resource WHERE { ?resource info:age ?age . FILTER (?age <= 24) } Q6 <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> info:age "2 "^^xsd:integer . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> info:age "25"^^xsd:integer . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given "Sarah" . 25
  26. 26. PREFIX info : <http://somewhere/peopleInfo#> SELECT ?resource WHERE { ?resource info:age ?age . FILTER (?age <= 24) } Q6 -----------------------------------| resource | ==================================== | <http://somewhere/RebeccaSmith/> | ------------------------------------ 26
  27. 27. OPTIONAL Clause    SPARQL also allows to define OPTIONAL blocks They offer the ability to query for data but not to fail query when that data does not exist. Optional blocks define additional graph patterns that do bind to the graph when they can be matched, but do not cause solutions to be rejected if they are not matched. 27
  28. 28. PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age } } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> info:age "2 "^^xsd:integer . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> info:age "25"^^xsd:integer . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q7 "Sarah" . 28
  29. 29. PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age } } Q7 ----------------------| name | age | ======================= | "Becky Smith" | 23 | | "Sarah Jones" | | | "John Smith" | 25 | | "Matt Jones" | | ----------------------- 29
  30. 30. PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . ?person info:age ?age . } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> info:age "2 "^^xsd:integer . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> info:age "25"^^xsd:integer . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q8 "Sarah" . 30
  31. 31. PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . ?person info:age ?age . } Q8 ----------------------| name | age | ======================= | "Becky Smith" | 23 | | "John Smith" | 25 | ----------------------- 31
  32. 32. PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age . FILTER ( ?age > 24 ) } } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> info:age "2 "^^xsd:integer . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> info:age "25"^^xsd:integer . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q9 "Sarah" . 32
  33. 33. PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age . FILTER ( ?age > 24 ) } } Q9 ----------------------| name | age | ======================= | "Becky Smith" | | | "Sarah Jones" | | | "John Smith" | 25 | | "Matt Jones" | | ----------------------- 33
  34. 34. PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age . } FILTER ( !bound(?age) || ?age > 24 ) } <http://somewhere/MattJones/> vCard:FN "Matt Jones" . <http://somewhere/MattJones/> vCard:N _:b0 . _:b0 vCard:Family "Jones" . _:b0 vCard:Given "Matthew" . <http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" . <http://somewhere/RebeccaSmith/> info:age "2 "^^xsd:integer . <http://somewhere/RebeccaSmith/> vCard:N _:b1 . _:b1 vCard:Family "Smith" . _:b1 vCard:Given "Rebecca" . <http://somewhere/JohnSmith/> vCard:FN "John Smith" . <http://somewhere/JohnSmith/> info:age "25"^^xsd:integer . <http://somewhere/JohnSmith/> vCard:N _:b2 . _:b2 vCard:Family "Smith" . _:b2 vCard:Given "John" . <http://somewhere/SarahJones/> vCard:FN "Sarah Jones" . <http://somewhere/SarahJones/> vCard:N _:b3 . _:b3 vCard:Family "Jones" . _:b3 vCard:Given Q10 "Sarah" . 34
  35. 35. PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age . } FILTER ( !bound(?age) || ?age > 24 ) } Q10 ----------------------| name | age | ======================= | "Sarah Jones" | | | "John Smith" | 25 | | "Matt Jones" | | ----------------------- 35
  36. 36. @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> . _:a foaf:name "Matt Jones" . _:b foaf:name "Sarah Jones" . _:c vcard:FN "Becky Smith" . _:d vcard:FN "John Smith" . 36
  37. 37. PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name WHERE { { [] foaf:name ?name } UNION { [] vCard:FN ?name } } _:a foaf:name "Matt Jones" . _:b foaf:name "Sarah Jones" . _:c vcard:FN "Becky Smith" . _:d vcard:FN Q11 "John Smith" . 37
  38. 38. PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name WHERE { { [] foaf:name ?name } UNION { [] vCard:FN ?name } } Q11 ----------------| name | ================= | "Matt Jones" | | "Sarah Jones" | | "Becky Smith" | | "John Smith" | ----------------- 38
  39. 39. PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name WHERE { [] ?p ?name FILTER ( ?p = foaf:name || ?p = vCard:FN ) } Q11 ----------------| name | ================= | "Matt Jones" | | "Sarah Jones" | | "Becky Smith" | | "John Smith" | ----------------- 39
  40. 40. PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name1 ?name2 WHERE { { [] foaf:name ?name1 } UNION { [] vCard:FN ?name2 } } _:a foaf:name "Matt Jones" . _:b foaf:name "Sarah Jones" . _:c vcard:FN "Becky Smith" . _:d vcard:FN Q12 "John Smith" . 40
  41. 41. PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name1 ?name2 WHERE { { [] foaf:name ?name1 } UNION { [] vCard:FN ?name2 } } Q12 --------------------------------| name1 | name2 | ================================= | "Matt Jones" | | | "Sarah Jones" | | | | "Becky Smith" | | | "John Smith" | --------------------------------- 41
  42. 42. PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name1 ?name2 WHERE { ?x a foaf:Person OPTIONAL { ?x foaf:name ?name1 } OPTIONAL { ?x vCard:FN ?name2 } } Q12 --------------------------------| name1 | name2 | ================================= | "Matt Jones" | | | "Sarah Jones" | | | | "Becky Smith" | | | "John Smith" | --------------------------------- 42
  43. 43. SPARQL Query PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX shop: <http://example/shop#> SELECT ?title ?name ?price WHERE { ?doc dc:title ?title . FILTER regex(?title, "SPARQL") . ?doc dc:creator ?c . ?c foaf:name ?name . OPTIONAL { ?doc shop:price ?price } } "Find books with ‘SPARQL’ in the title. Get the authors' name and the price (if available)." 43
  44. 44. SPARQL Query Results XML Format
  45. 45. SPARQL Query Results XML Format    SPARQL allows query results to be returned as XML The SPARQL Results Document begins with sparql root element The sparql element contains two sub-elements, head and results <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> ... </head> <results> ... </results> </sparql> 45
  46. 46. SPARQL Query Results XML Format    The element head contains a sequence of elements describing the set of Query Variable names in the query results. The order of the variable names in the sequence is the order of the variable names given in the SELECT statement in the SPARQL query The ordered sequence of variable names are used to create empty child elements variable with the variable name as the value of an attribute name <head> <variable name="name" /> <variable name="homepage" /> <variable name="age" /> </head> 46
  47. 47. SPARQL Query Results XML Format    The second child-element of sparql is results  it must appear after head. The results element contains the complete sequence of query results. For each Query Solution in the query results, a result child-element of results is added <results> <result> ... </result> <result> ... </result> ... </results> 47
  48. 48. SPARQL Query Results XML Format  Each result element     corresponds to one Query Solution in a result contains child elements (in no particular order) for each Query Variable that appears in the solution. It is used to record how the query variables bind to RDF Terms. Each binding inside a solution is written as an element binding as a child of result with the query variable name as the value of the name attribute. <result> <binding name="name"> ... </binding> <binding name="hpage"> ... </binding> <binding name="age"> ... </binding> </result> 48
  49. 49. SPARQL Query Results XML Format   The value of a query variable binding, which is an RDF Term, is included as the content of the binding as follows: RDF URI Reference U <binding> <uri> U </uri> </binding>  RDF Literal S <binding> <literal> S </literal> </binding>  RDF Typed Literal S with datatype URI D <binding> <literal datatype="D"> S </literal> </binding> 49
  50. 50. <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="name"/> <variable name="hpage"/> <variable name="age"/> </head> <results> <result> <binding name="name"> <literal>Bob</literal> </binding> <binding name="hpage"> <uri>http://work.example.org/bob/</uri> </binding> <binding name="age"> <literal datatype="http://www.w3.org/2001/XMLSchema#integer"> 30 </literal> </binding> </result> ... </results> </sparql> 50
  51. 51. References  SPARQL at W3C   http://www.w3.org/TR/rdf-sparql-query/ SPARQL Tutorial  http://jena.sourceforge.net/ARQ/Tutorial/ 51

×