+ All Categories

SPARQL

Date post: 11-Nov-2014
Category:
Upload: raji-ghawi
View: 407 times
Download: 5 times
Share this document with a friend
Description:
SPARQL 1.0 tutorial
Popular Tags:
51
SPARQL Raji GHAWI 30/03/2010 Query Language for RDF
Transcript
Page 1: SPARQL

SPARQL

Raji GHAWI

30/03/2010

Query Language for RDF

Page 2: SPARQL

2

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

Introduction to SPARQL

Page 3: SPARQL

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.

Page 4: SPARQL

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 foaf:name "Alice" .

person:A foaf:mbox <mailto:[email protected]> .

person:B foaf:name "Bob" .

Page 5: SPARQL

5

SPARQL: Triple Pattern

@prefix person: <http://example/person/> .@prefix foaf: <http://xmlns.com/foaf/0.1/> .

person:A foaf:name "Alice" .person:A foaf:mbox <mailto:[email protected]> .person:B foaf:name "Bob" .

PREFIX person: <http://example/person/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?nameWHERE { ?x foaf:name ?name }

-----------| name |===========| "Bob" || "Alice" |-----------

Page 6: SPARQL

6

SPARQL: Basic Graph Pattern

@prefix person: <http://example/person/> .@prefix foaf: <http://xmlns.com/foaf/0.1/> .

person:A foaf:name "Alice" .person:A foaf:mbox <mailto:[email protected]> .person:B foaf:name "Bob" .

PREFIX person: <http://example/person/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?nameWHERE { ?person foaf:mbox <mailto:[email protected]> . ?person foaf:name ?name . }

-----------| name |===========| "Alice" |-----------

Page 7: SPARQL

7

Inference

:x rdf:type :C .

:C rdfs:subClassOf :D .

--------| type |========| :C || :D |--------

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?type

WHERE {

?x rdf:type ?type .

}

Page 8: SPARQL

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. SELECT – returns all, or a subset of, the variables bound in a query pattern match.

2. CONSTRUCT – returns an RDF graph constructed by substituting variables in a set of triple templates.

3. ASK – returns a boolean indicating whether a query pattern matches or not.

4. DESCRIBE – returns an RDF graph that describes the resources found.

Page 9: SPARQL

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.

Page 10: SPARQL

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.

Page 11: SPARQL

11

http://somewhere/MattJones/

http://somewhere/RebeccaSmith/

http://somewhere/JohnSmith/

http://somewhere/SarahJones/Sarah Jones

John Smith

Becky Smith

Matt Jones

Matthew

Jones

Sarah

Jones

Rebecca

Smith

Smith

John

vCard:Family

vCard:Given

vCard:FN

vCard:N

info:age

vCard:FN

vCard:N

vCard:FN

vCard:N

vCard:FN

vCard:N

vCard:Family

vCard:Given

vCard:Family

vCard:Given

vCard:Family

vCard:Given

23

25info:age

A Dataset of RDF Triples

Page 12: SPARQL

12

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"

A Dataset of RDF Triples

Page 13: SPARQL

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 "Sarah" .

Q1

Page 14: SPARQL

14

---------------------------------| x |=================================| <http://somewhere/JohnSmith/> |---------------------------------

SELECT ?x

WHERE {

?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith"

}

Q1

Page 15: SPARQL

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 "Sarah" .

Q2

Page 16: SPARQL

16

SELECT ?x ?fname

WHERE {

?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> ?fname

}

----------------------------------------------------| x | fname |====================================================| <http://somewhere/RebeccaSmith/> | "Becky Smith" || <http://somewhere/SarahJones/> | "Sarah Jones" || <http://somewhere/JohnSmith/> | "John Smith" || <http://somewhere/MattJones/> | "Matt Jones" |----------------------------------------------------

Q2

Page 17: SPARQL

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 "Sarah" .

Q3

Page 18: SPARQL

18

-------------| givenName |=============| "John" || "Rebecca" |-------------

PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?givenName

WHERE {

?y vCard:Family "Smith" .

?y vCard:Given ?givenName .

}

Q3

Page 19: SPARQL

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 "Sarah" .

Q4

Page 20: SPARQL

20

PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?y ?givenName

WHERE {

?y vCard:Family "Smith" .

?y vCard:Given ?givenName .

}

--------------------| y | givenName |====================| _:b1 | "John" || _:b2 | "Rebecca" |--------------------

Q4

Page 21: SPARQL

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

Page 22: SPARQL

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 "Sarah" .

Q5

Page 23: SPARQL

23

PREFIX vCard : <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?g

WHERE {

?y vCard:Given ?g .

FILTER regex(?g, "r", "i")

}

-------------| g |=============| "Rebecca" || "Sarah" |-------------

Q5

Page 24: SPARQL

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 "23"^^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" .

Page 25: SPARQL

25

PREFIX info : <http://somewhere/peopleInfo#>

SELECT ?resource

WHERE {

?resource 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 "23"^^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" .

Q6

Page 26: SPARQL

26

PREFIX info : <http://somewhere/peopleInfo#>

SELECT ?resource

WHERE {

?resource info:age ?age .

FILTER (?age <= 24)

}

------------------------------------| resource |====================================| <http://somewhere/RebeccaSmith/> |------------------------------------

Q6

Page 27: SPARQL

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.

Page 28: SPARQL

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 "23"^^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" .

Q7

Page 29: SPARQL

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 }

}

-----------------------| name | age |=======================| "Becky Smith" | 23 || "Sarah Jones" | || "John Smith" | 25 || "Matt Jones" | |-----------------------

Q7

Page 30: SPARQL

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 "23"^^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" .

Q8

Page 31: SPARQL

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 .

}

-----------------------| name | age |=======================| "Becky Smith" | 23 || "John Smith" | 25 |-----------------------

Q8

Page 32: SPARQL

32

PREFIX info: <http://somewhere/peopleInfo#>PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?name ?ageWHERE { ?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 "23"^^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" .

Q9

Page 33: SPARQL

33

PREFIX info: <http://somewhere/peopleInfo#>PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?name ?ageWHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age . FILTER ( ?age > 24 ) }}

-----------------------| name | age |=======================| "Becky Smith" | || "Sarah Jones" | || "John Smith" | 25 || "Matt Jones" | |-----------------------

Q9

Page 34: SPARQL

34

PREFIX info: <http://somewhere/peopleInfo#>PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?name ?ageWHERE {

?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 "23"^^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" .

Q10

Page 35: SPARQL

35

PREFIX info: <http://somewhere/peopleInfo#>PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?name ?ageWHERE {

?person vcard:FN ?name .OPTIONAL { ?person info:age ?age . }FILTER ( !bound(?age) || ?age > 24 )

}

-----------------------| name | age |=======================| "Sarah Jones" | || "John Smith" | 25 || "Matt Jones" | |-----------------------

Q10

Page 36: SPARQL

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" .

Page 37: SPARQL

37

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?nameWHERE { { [] foaf:name ?name } UNION { [] vCard:FN ?name }}

Q11

_:a foaf:name "Matt Jones" .

_:b foaf:name "Sarah Jones" .

_:c vcard:FN "Becky Smith" .

_:d vcard:FN "John Smith" .

Page 38: SPARQL

38

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?nameWHERE { { [] foaf:name ?name } UNION { [] vCard:FN ?name }}

-----------------| name |=================| "Matt Jones" || "Sarah Jones" || "Becky Smith" || "John Smith" |-----------------

Q11

Page 39: SPARQL

39

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?nameWHERE { [] ?p ?name FILTER ( ?p = foaf:name || ?p = vCard:FN )}

-----------------| name |=================| "Matt Jones" || "Sarah Jones" || "Becky Smith" || "John Smith" |-----------------

Q11

Page 40: SPARQL

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 }}

Q12

_:a foaf:name "Matt Jones" .

_:b foaf:name "Sarah Jones" .

_:c vcard:FN "Becky Smith" .

_:d vcard:FN "John Smith" .

Page 41: SPARQL

41

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?name1 ?name2WHERE { { [] foaf:name ?name1 } UNION { [] vCard:FN ?name2 }}

---------------------------------| name1 | name2 |=================================| "Matt Jones" | || "Sarah Jones" | || | "Becky Smith" || | "John Smith" |---------------------------------

Q12

Page 42: SPARQL

42

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?name1 ?name2WHERE { ?x a foaf:Person OPTIONAL { ?x foaf:name ?name1 } OPTIONAL { ?x vCard:FN ?name2 }}

---------------------------------| name1 | name2 |=================================| "Matt Jones" | || "Sarah Jones" | || | "Becky Smith" || | "John Smith" |---------------------------------

Q12

Page 43: SPARQL

43

SPARQL Query

"Find books with ‘SPARQL’ in the title. Get the authors' name and the price (if available)."

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 ?priceWHERE { ?doc dc:title ?title . FILTER regex(?title, "SPARQL") . ?doc dc:creator ?c . ?c foaf:name ?name . OPTIONAL { ?doc shop:price ?price }}

Page 44: SPARQL

SPARQL Query Results XML Format

Page 45: SPARQL

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>

Page 46: SPARQL

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>

Page 47: SPARQL

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>

Page 48: SPARQL

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>

Page 49: SPARQL

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>

Page 50: SPARQL

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>

Page 51: SPARQL

51

References

SPARQL at W3C http://www.w3.org/TR/rdf-sparql-query/

SPARQL Tutorial http://jena.sourceforge.net/ARQ/Tutorial/


Recommended