After this part of the tutorial you should:
SPARQL has three defined result formats:
SELECT ?name ?mbox
gives variable bindings:name | mbox |
---|---|
"Johnny Lee Outlaw" | <mailto:jlow@example.com> |
"Peter Goodguy" | <mailto:peter@example.org> |
ASK
gives a true
or false
.CONSTRUCT { pattern }
and DESCRIBE
give an RDF graph:<rdf:Description> <foaf:givenName>Johnny Lee Outlaw</foaf:givenName> <foaf:mbox rdf:resource="mailto:jlow@example.com"/> </rdf:Description>
SELECT ?name ?mbox
gives variable bindings:name | mbox |
---|---|
"Johnny Lee Outlaw" | <mailto:jlow@example.com> |
"Peter Goodguy" | <mailto:peter@example.org> |
ASK
gives a true
or false
.CONSTRUCT { pattern }
and DESCRIBE
give an RDF graph:<rdf:Description> <foaf:givenName>Johnny Lee Outlaw</foaf:givenName> <foaf:mbox rdf:resource="mailto:jlow@example.com"/> </rdf:Description>
SELECT ?name ?mbox
gives variable bindings:name | mbox |
---|---|
"Johnny Lee Outlaw" | <mailto:jlow@example.com> |
"Peter Goodguy" | <mailto:peter@example.org> |
ASK
gives a true
or false
.CONSTRUCT { pattern }
and DESCRIBE
give an RDF graph:<rdf:Description> <foaf:givenName>Johnny Lee Outlaw</foaf:givenName> <foaf:mbox rdf:resource="mailto:jlow@example.com"/> </rdf:Description>
The query allows transformation of sequence of results:
DISTINCT
: ensures results in the sequence are uniqueORDER BY
: puts the results in an orderLIMIT
integer: restricts the number of resultsOFFSET
integer: controls where the results start(Currently applied only to variable bindings results).
name | age |
---|---|
Bob | 37 |
Sue | |
Tom | 62 |
SPARQL results are similar to SQL query results:
The SPARQL XML Results Format defines a syntax for these results.
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2"> <head> <variable name="name"/> <variable name="mbox"/> </head> <results> <result> <binding name="name"><literal>Johnny Lee Outlaw</literal></binding> <binding name="mbox"><uri>mailto:jlow@example.com</uri></binding> </result> <result> <binding name="name"><literal>Peter Goodguy</literal></binding> <binding name="mbox"><uri>mailto:peter@example.org</uri></binding> </result> </results> </sparql>
This regular format makes it easy to write XSLT and XQuery to process results.
<xsl:template match="res:result"> <p><xsl:value-of select="res:binding[@name='name']/res:literal/text()"/>'s email address is <xsl:value-of select="res:binding[@name='mbox']/res:uri/text()"/>.</p><xsl:text> </xsl:text> </xsl:template> [see name-mbox.xsl]
...produces...
<p>Johnny Lee Outlaw's email address is mailto:jlow@example.com.</p> <p>Peter Goodguy's email address is mailto:peter@example.org.</p>
<binding name="x">variable binding</binding>
Variables may be bound to:
<literal>Bob</literal>
<uri>http://example.org/</uri>
<bnode>r1</bnode>
<unbound/>
Literals may have a
<literal xml:lang="en">Bob</literal>
<literal datatype="http://www.w3.org/2001/XMLSchema#integer" >30</literal>
Yes or No results are a simple form of the XML Results format.
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2"> <head/> <results> <boolean>true</boolean> </results> </sparql>
CONSTRUCT
and DESCRIBE
produce RDF graphs.CONSTRUCT
results are made from variable substitutions into the pattern:
CONSTRUCT { ?friend pim:fullName ?name . ?friend foaf:mbox ?mbox } WHERE { ?alice foaf:knows ?friend . ?friend foaf:given ?name . ?friend foaf:mbox ?mbox }
<rdf:Description> <pim:fullName>Johnny Lee Outlaw</pim:fullName> <foaf:mbox rdf:resource="mailto:jlow@example.com"/> </rdf:Description>
UNION
and OPTIONAL
can produce solutions with unbound variables.
CONSTRUCT { ?who foaf:mbox ?mbox . ?who foaf:givenName ?given . ?who foaf:familyName ?family . ?who foaf:name ?full } WHERE { ?who vCard:email <mailto:jlow@example.com> . OPTIONAL { ?who vCard:firstName ?given . ?who vCard:lastName ?family } OPTIONAL { ?who foaf:name ?full } }
Statements involving unbound variables are omitted.
CONSTRUCT { ?who foaf:mbox ?mbox . ?who foaf:givenName ?given . ?who foaf:familyName ?family . ?who foaf:name ?full } ...
+
who | mbox | given | family | full |
---|---|---|---|---|
_:a | <mailto:jlow@example.com> | "Johnny Lee Outlaw" |
=
_:a foaf:mbox <mailto:jlow@example.com> . _:a foaf:name "Johnny Lee Outlaw" .
CONSTRUCT { [] foaf:mbox ?mbox } WHERE { ... }
CONSTRUCT { [] foaf:mbox ?mbox } WHERE { ... }
+
name | mbox |
---|---|
"Johnny Lee Outlaw" | <mailto:jlow@example.com> |
"Peter Goodguy" | <mailto:peter@example.org> |
=
_:a foaf:mbox <mailto:jlow@example.com> . _:b foaf:mbox <mailto:peter@example.org> .
DESCRIBE
results are controlled by the server:
DESCRIBE ?friend WHERE { ?alice foaf:knows ?friend . ?friend foaf:given ?name . ?friend foaf:mbox ?mbox }
<rdf:Description> <foaf:givenName>Johnny</foaf:givenName> <foaf:mbox_sha1sum>BF5E68...</foaf:mbox_sha1sum> <foaf:knows> <rdf:Description> <foaf:givenName>Bob</foaf:givenName> <foaf:mbox_sha1sum>5EBF68...</foaf:mbox_sha1sum> </rdf:Description> </foaf:knows> <foaf:knows> ... </foaf:knows> </rdf:Description>
Semantic Discovery:
DESCRIBE <http://www.w3.org/TR/rdf-sparql-query/>
Related Work:
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.