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