After this part of the tutorial you should
A series of short tasks leading up to a larger query exercise.:
Use the following skeleton query (not complete) to setup the data you will be using:
BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/> # ... FROM NAMED <aliceFoaf.ttl> FROM NAMED <bobFoaf.ttl> FROM NAMED <celineFoaf.ttl> FROM NAMED <danFoaf.ttl> FROM NAMED <eveFoaf.ttl> # ...
(data.rq)
Task 1 - Write a query to find all the nodes in the RDF graph that are about a person.
Hint: the nodes about people all have an rdf:type property
with the same value.
BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?person
FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>
WHERE {
GRAPH ?g { ?person rdf:type foaf:Person } .
}
This did not return the expected results (6 people) because:
So try to find more information
Task 2 - Write a query to find all people and their names
Hint: use the foaf:name property
BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?person ?name
FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>
WHERE {
GRAPH ?g { ?person rdf:type foaf:Person;
foaf:name ?name
} .
}
(names.rq)
This did not return the expected results (6 people) either because:
So try to find some more information
Task 3 - Write a query to find all people, their names and mailboxes
Hint: use the foaf:mbox property
BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?person ?name ?mbox
FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>
WHERE {
GRAPH ?g { ?person rdf:type foaf:Person;
foaf:name ?name; foaf:mbox ?mbox
} .
}
(mbox.rq)
OPTIONAL matchesTask 4 - Write a query to find all people and optionally their names and mailboxes
BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?person ?name ?mbox
FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>
WHERE {
GRAPH ?g { ?person rdf:type foaf:Person .
OPTIONAL { ?person foaf:name ?name } .
OPTIONAL { ?person foaf:mbox ?mbox }
} .
}
The last answer showed:
Task 5 - Write a query to find all people, optionally their names and mailboxes eliminating the duplicates
Hint: DISTINCT
BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name ?mbox
FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>
WHERE {
GRAPH ?g { ?person rdf:type foaf:Person .
OPTIONAL { ?person foaf:name ?name } .
OPTIONAL { ?person foaf:mbox ?mbox }
} .
}
Task 6 - Order the results of Task 5 sorting by name of the person, in ascending order.
BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name ?mbox
FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>
WHERE {
GRAPH ?g { ?person rdf:type foaf:Person .
OPTIONAL { ?person foaf:name ?name } .
OPTIONAL { ?person foaf:mbox ?mbox }
} .
}
ORDER BY ASC[?name]
(order.rq)
Hints below
Use the structure of the data for birthday events to create part of the query
An OPTIONAL { ... } block can cover many
patterns
Note: there are no canned answers for all of these yet
BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX bio: <http://purl.org/vocab/bio/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT ?name ?birthday
FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>
WHERE {
GRAPH ?g {
?person a foaf:Person; foaf:name ?name .
OPTIONAL {
?person bio:event ?b .
?b a bio:Birth .
?b bio:date ?birthday .
}
}
}
ORDER BY ASC[?birthday]
Questions?
Copyright 2005 Dave Beckett, Steve Harris, Eric Prud'hommeaux and Andy Seaborne. Terms of use are given on the main Introduction to RDF Query with SPARQL Tutorial page.