SlideShare a Scribd company logo
1 of 51
SPARQL
Query Language for RDF

Raji GHAWI
30/03/2010
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
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
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
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
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
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
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
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
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
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
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
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
SELECT ?x
WHERE {
?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith"
}

Q1

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

14
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
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
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
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
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
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
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
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
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
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
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
PREFIX info : <http://somewhere/peopleInfo#>
SELECT ?resource
WHERE {
?resource
info:age
?age .
FILTER (?age <= 24)
}

Q6

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

26
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
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
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
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
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
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
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
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
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
@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
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
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
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
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
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
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
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
SPARQL Query Results XML Format
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
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
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
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
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
<?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
References


SPARQL at W3C




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

SPARQL Tutorial


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

51

More Related Content

What's hot

What's hot (20)

ch3
ch3ch3
ch3
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
23 data-structures
23 data-structures23 data-structures
23 data-structures
 
Sql 
statements , functions & joins
Sql 
statements , functions  &  joinsSql 
statements , functions  &  joins
Sql 
statements , functions & joins
 
Intro To TSQL - Unit 3
Intro To TSQL - Unit 3Intro To TSQL - Unit 3
Intro To TSQL - Unit 3
 
Sparql
SparqlSparql
Sparql
 
Querying Linked Data on Android
Querying Linked Data on AndroidQuerying Linked Data on Android
Querying Linked Data on Android
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Introduction to SPARQL
Introduction to SPARQLIntroduction to SPARQL
Introduction to SPARQL
 
2019 03 05_biological_databases_part5_v_upload
2019 03 05_biological_databases_part5_v_upload2019 03 05_biological_databases_part5_v_upload
2019 03 05_biological_databases_part5_v_upload
 
The Semantic Web #10 - SPARQL
The Semantic Web #10 - SPARQLThe Semantic Web #10 - SPARQL
The Semantic Web #10 - SPARQL
 
Greenplum 6 Changes
Greenplum 6 ChangesGreenplum 6 Changes
Greenplum 6 Changes
 
SAS and R Code for Basic Statistics
SAS and R Code for Basic StatisticsSAS and R Code for Basic Statistics
SAS and R Code for Basic Statistics
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1
 
Intro To TSQL - Unit 5
Intro To TSQL - Unit 5Intro To TSQL - Unit 5
Intro To TSQL - Unit 5
 
SQL
SQLSQL
SQL
 
R code descriptive statistics of phenotypic data by Avjinder Kaler
R code descriptive statistics of phenotypic data by Avjinder KalerR code descriptive statistics of phenotypic data by Avjinder Kaler
R code descriptive statistics of phenotypic data by Avjinder Kaler
 
Uplift – Generating RDF datasets from non-RDF data with R2RML
Uplift – Generating RDF datasets from non-RDF data with R2RMLUplift – Generating RDF datasets from non-RDF data with R2RML
Uplift – Generating RDF datasets from non-RDF data with R2RML
 

Similar to SPARQL

SWT Lecture Session 4 - SW architectures and SPARQL
SWT Lecture Session 4 - SW architectures and SPARQLSWT Lecture Session 4 - SW architectures and SPARQL
SWT Lecture Session 4 - SW architectures and SPARQL
Mariano Rodriguez-Muro
 
Triplestore and SPARQL
Triplestore and SPARQLTriplestore and SPARQL
Triplestore and SPARQL
Lino Valdivia
 

Similar to SPARQL (20)

AnzoGraph DB - SPARQL 101
AnzoGraph DB - SPARQL 101AnzoGraph DB - SPARQL 101
AnzoGraph DB - SPARQL 101
 
Semantic web meetup – sparql tutorial
Semantic web meetup – sparql tutorialSemantic web meetup – sparql tutorial
Semantic web meetup – sparql tutorial
 
Sparql service-description
Sparql service-descriptionSparql service-description
Sparql service-description
 
SWT Lecture Session 4 - SW architectures and SPARQL
SWT Lecture Session 4 - SW architectures and SPARQLSWT Lecture Session 4 - SW architectures and SPARQL
SWT Lecture Session 4 - SW architectures and SPARQL
 
XSPARQL CrEDIBLE workshop
XSPARQL CrEDIBLE workshopXSPARQL CrEDIBLE workshop
XSPARQL CrEDIBLE workshop
 
SPARQL Query Forms
SPARQL Query FormsSPARQL Query Forms
SPARQL Query Forms
 
Relational data model
Relational data modelRelational data model
Relational data model
 
Bio it 2005_rdf_workshop05
Bio it 2005_rdf_workshop05Bio it 2005_rdf_workshop05
Bio it 2005_rdf_workshop05
 
Introduction to SPARQL
Introduction to SPARQLIntroduction to SPARQL
Introduction to SPARQL
 
Triplestore and SPARQL
Triplestore and SPARQLTriplestore and SPARQL
Triplestore and SPARQL
 
Selectivity Estimation for SPARQL Triple Patterns with Shape Expressions
Selectivity Estimation for SPARQL Triple Patterns with Shape ExpressionsSelectivity Estimation for SPARQL Triple Patterns with Shape Expressions
Selectivity Estimation for SPARQL Triple Patterns with Shape Expressions
 
SPARQL introduction and training (130+ slides with exercices)
SPARQL introduction and training (130+ slides with exercices)SPARQL introduction and training (130+ slides with exercices)
SPARQL introduction and training (130+ slides with exercices)
 
Sparql a simple knowledge query
Sparql  a simple knowledge querySparql  a simple knowledge query
Sparql a simple knowledge query
 
RDF and Java
RDF and JavaRDF and Java
RDF and Java
 
Towards an RDF Validation Language based on Regular Expression Derivatives
Towards an RDF Validation Language based on Regular Expression DerivativesTowards an RDF Validation Language based on Regular Expression Derivatives
Towards an RDF Validation Language based on Regular Expression Derivatives
 
Bringing OpenClinica Data into SAS
Bringing OpenClinica Data into SASBringing OpenClinica Data into SAS
Bringing OpenClinica Data into SAS
 
LarKC Tutorial at ISWC 2009 - Data Model
LarKC Tutorial at ISWC 2009 - Data ModelLarKC Tutorial at ISWC 2009 - Data Model
LarKC Tutorial at ISWC 2009 - Data Model
 
DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6
 
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...
 
BAS 150 Lesson 6 Lecture
BAS 150 Lesson 6 LectureBAS 150 Lesson 6 Lecture
BAS 150 Lesson 6 Lecture
 

More from Raji Ghawi

More from Raji Ghawi (12)

Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming Techniques
 
Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML Schema
 
Java and XML
Java and XMLJava and XML
Java and XML
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQL
 
Java and OWL
Java and OWLJava and OWL
Java and OWL
 
XQuery
XQueryXQuery
XQuery
 
XPath
XPathXPath
XPath
 
Ontology-based Cooperation of Information Systems
Ontology-based Cooperation of Information SystemsOntology-based Cooperation of Information Systems
Ontology-based Cooperation of Information Systems
 
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information SourcesOWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
 
Coopération des Systèmes d'Informations basée sur les Ontologies
Coopération des Systèmes d'Informations basée sur les OntologiesCoopération des Systèmes d'Informations basée sur les Ontologies
Coopération des Systèmes d'Informations basée sur les Ontologies
 
Building Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information SourcesBuilding Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information Sources
 
Database-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic InteroperabilityDatabase-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic Interoperability
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 

SPARQL

  • 1. SPARQL Query Language for RDF Raji GHAWI 30/03/2010
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. SELECT ?x WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith" } Q1 --------------------------------| x | ================================= | <http://somewhere/JohnSmith/> | --------------------------------- 14
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. PREFIX info : <http://somewhere/peopleInfo#> SELECT ?resource WHERE { ?resource info:age ?age . FILTER (?age <= 24) } Q6 -----------------------------------| resource | ==================================== | <http://somewhere/RebeccaSmith/> | ------------------------------------ 26
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. @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. 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. 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. 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. 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. 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. 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. 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. SPARQL Query Results XML Format
  • 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. 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. 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. 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. 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. <?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. References  SPARQL at W3C   http://www.w3.org/TR/rdf-sparql-query/ SPARQL Tutorial  http://jena.sourceforge.net/ARQ/Tutorial/ 51