SPARQL

Post on 11-Nov-2014

407 views 5 download

Tags:

description

SPARQL 1.0 tutorial

transcript

SPARQL

Raji GHAWI

30/03/2010

Query Language for RDF

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

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.

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:alice@example.net> .

person:B foaf:name "Bob" .

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:alice@example.net> .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" |-----------

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:alice@example.net> .person:B foaf:name "Bob" .

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

SELECT ?nameWHERE { ?person foaf:mbox <mailto:alice@example.net> . ?person foaf:name ?name . }

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

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 .

}

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.

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.

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.

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

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

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

14

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

SELECT ?x

WHERE {

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

}

Q1

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

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

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

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

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

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

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

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

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

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

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

26

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

SELECT ?resource

WHERE {

?resource info:age ?age .

FILTER (?age <= 24)

}

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

Q6

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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>

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>

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>

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>

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>

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>

51

References

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

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