Introduction to RDF Query with SPARQL

Part 4 - SPARQL Result Forms

Eric Prud'hommeaux

W3C/Keio

Learning Goals

magic eight ball options

After this part of the tutorial you should:

Result Formats

SPARQL has three defined result formats:

The Query Controls the Result Format

The Query Controls the Result Format

The Query Controls the Result Format

The Query Manipulates the Result Format

The query allows transformation of sequence of results:

  1. DISTINCT: ensures results in the sequence are unique
  2. ORDER BY: puts the results in an order
  3. LIMIT integer: restricts the number of results
  4. OFFSET integer: controls where the results start

(Currently applied only to variable bindings results).

Variable Bindings

nameage
Bob37
Sue
Tom62

SPARQL results are similar to SQL query results:

The SPARQL XML Results Format defines a syntax for these results.

Variable Bindings - XML 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>

SPARQL XML Results

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>

Variable Bindings

<binding name="x">variable binding</binding>

Variables may be bound to:

Literals

Literals may have a

Yes or No Results

magic eight ball

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>

RDF Graph Results

RDF Graph Results - CONSTRUCT

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>

Construct Rules - unbound variables

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 Rules - unbound variables

CONSTRUCT { ?who foaf:mbox ?mbox .
            ?who foaf:givenName ?given .
            ?who foaf:familyName ?family .
            ?who foaf:name ?full } ...

+

whomboxgivenfamilyfull
_:a<mailto:jlow@example.com> "Johnny Lee Outlaw"

=

_:a foaf:mbox <mailto:jlow@example.com> .
_:a foaf:name "Johnny Lee Outlaw" .

Construct Rules - blank nodes

Construct Rules - blank nodes

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

RDF Graph Results - DESCRIBE

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>

Uses for DESCRIBE

Semantic Discovery:

DESCRIBE Related Work

Related Work:

Questions

Questions?

Copyright

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.