SPARQL SPARQL SPARQL

SPARQL

RDF GRAPH

  • XML/RDF

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <rdf:RDF                                                                                                                                                                             
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xmlns:vCard='http://www.w3.org/2001/vcard-rdf/3.0#'
    xmlns:info='http://somewhere/peopleInfo#'
    >


    <rdf:Description rdf:about="http://somewhere/JohnSmith/">
    <vCard:FN>John Smith</vCard:FN>
    <info:age rdf:datatype='http://www.w3.org/2001/XMLSchema#integer'>25</info:age>
    <vCard:N rdf:parseType="Resource">
    <vCard:Family>Smith</vCard:Family>
    <vCard:Given>John</vCard:Given>
    </vCard:N>
    </rdf:Description>
    </rdf:RDF>
  • TRUTLE

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @prefix vCard:   <http://www.w3.org/2001/vcard-rdf/3.0#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix : <#> .

    <http://somewhere/MattJones/>
    vCard:FN "Matt Jones" ;
    vCard:N [ vCard:Family
    "Jones" ;
    vCard:Given
    "Matthew"
    ] .
  • N3

    1
    like representation of turtle.

SPARQL QUERY

GRAPH PATTERNS

  • Basic Graph Patterns : where a set of triple patterns must match
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    #dataset
    <rdf:RDF
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xmlns:vCard='http://www.w3.org/2001/vcard-rdf/3.0#'
    >

    <rdf:Description rdf:about="http://somewhere/JohnSmith/">
    <vCard:FN>John Smith</vCard:FN>
    <vCard:N rdf:parseType="Resource">
    <vCard:Family>Smith</vCard:Family>
    <vCard:Given>John</vCard:Given>
    </vCard:N>
    </rdf:Description>

    <rdf:Description rdf:about="http://somewhere/RebeccaSmith/">
    <vCard:FN>Becky Smith</vCard:FN>
    <vCard:N rdf:parseType="Resource">
    <vCard:Family>Smith</vCard:Family>
    <vCard:Given>Rebecca</vCard:Given>
    </vCard:N>
    </rdf:Description>

    <rdf:Description rdf:about="http://somewhere/SarahJones/">
    <vCard:FN>Sarah Jones</vCard:FN>
    <vCard:N rdf:parseType="Resource">
    <vCard:Family>Jones</vCard:Family>
    <vCard:Given>Sarah</vCard:Given>
    </vCard:N>
    </rdf:Description>

    <rdf:Description rdf:about="http://somewhere/MattJones/">
    <vCard:FN>Matt Jones</vCard:FN>
    <vCard:N
    vCard:Family="Jones"
    vCard:Given="Matthew"/>
    </rdf:Description>

    </rdf:RDF>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# basic graph pattern
SELECT ?givenName
WHERE {
?y <http://www.w3.org/2001/vcard-rdf/3.0#Family> "Smith" .
?y <http://www.w3.org/2001/vcard-rdf/3.0#Given> ?givenName .
}
# same as
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?givenName
WHERE {
?y vcard:Family "Smith";
vcard:Given ?givenName .
}

# result
-------------
| givenName |
=============
| "John" |
| "Rebecca" |
-------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# blank node
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?y ?givenName
WHERE
{ ?y vcard:Family "Smith" .
?y vcard:Given ?givenName .
}
# result
--------------------
| y | givenName |
====================
| _:b0 | "John" |
| _:b1 | "Rebecca" |
--------------------

# note that: Syntax for Blank Nodes
[ :p "v" ] . || _:a0 :p "v" . # blank node
[] :p "v" . # as subject
:s :q [ :p "v" ] . # as object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# FILTER
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?g
WHERE{
?y vcard:Given ?g .
FILTER regex(?g, "r", "i")
}

#result
-------------
| g |
=============
| "Rebecca" |
| "Sarah" |
-------------

More example use FILTER.

  • Group Graph Pattern : where a set of graph patterns must all match

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # a group graph pattern is delimited with {}

    {} # empty graph pattern
    PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>

    SELECT ?y ?givenName
    WHERE {
    {?y vcard:Family "Smith" .}
    {?y vcard:Given ?givenName .}
    }
    # Result same as previous query.
  • Optional Graph patterns : where additional patterns may extend the solution

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    # dataset
    <rdf:RDF
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xmlns:vCard='http://www.w3.org/2001/vcard-rdf/3.0#'
    xmlns:info='http://somewhere/peopleInfo#'
    >

    <rdf:Description rdf:about="http://somewhere/JohnSmith/">
    <vCard:FN>John Smith</vCard:FN>
    <info:age rdf:datatype='http://www.w3.org/2001/XMLSchema#integer'>25</info:age>
    <vCard:N rdf:parseType="Resource">
    <vCard:Family>Smith</vCard:Family>
    <vCard:Given>John</vCard:Given>
    </vCard:N>
    </rdf:Description>

    <rdf:Description rdf:about="http://somewhere/RebeccaSmith/">
    <vCard:FN>Becky Smith</vCard:FN>
    <info:age rdf:datatype='http://www.w3.org/2001/XMLSchema#integer'>23</info:age>
    <vCard:N rdf:parseType="Resource">
    <vCard:Family>Smith</vCard:Family>
    <vCard:Given>Rebecca</vCard:Given>
    </vCard:N>
    </rdf:Description>

    <rdf:Description rdf:about="http://somewhere/SarahJones/">
    <vCard:FN>Sarah Jones</vCard:FN>
    <vCard:N rdf:parseType="Resource">
    <vCard:Family>Jones</vCard:Family>
    <vCard:Given>Sarah</vCard:Given>
    </vCard:N>
    </rdf:Description>

    <rdf:Description rdf:about="http://somewhere/MattJones/">
    <vCard:FN>Matt Jones</vCard:FN>
    <vCard:N
    vCard:Family="Jones"
    vCard:Given="Matthew"/>
    </rdf:Description>

    </rdf:RDF>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# OPTIONAL
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 . FILTER ( ?age > 24 ) }
}

# result
-----------------------
| name | age |
=======================
| "Becky Smith" | |
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
  • Alternative Graph Pattern : where two or more possible patterns are tried
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #dataset 
    @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" .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# UNION
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 }
}

# result
---------------------------------
| name1 | name2 |
=================================
| "Matt Jones" | |
| "Sarah Jones" | |
| | "Becky Smith" |
| | "John Smith" |
---------------------------------
  • Patterns on Named Graphs, where patterns are matched against named graphs
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # Default graph (ds-dft.ttl):
    @prefix dc: <http://purl.org/dc/elements/1.1/> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

    <ds-ng-1.ttl> dc:date "2005-07-14T03:18:56+0100"^^xsd:dateTime .
    <ds-ng-2.ttl> dc:date "2005-09-22T05:53:05+0100"^^xsd:dateTime .

    # Named graph (ds-ng-1.ttl):
    @prefix dc: <http://purl.org/dc/elements/1.1/> .

    [] dc:title "Harry Potter and the Philospher's Stone" .
    [] dc:title "Harry Potter and the Chamber of Secrets" .

    #Named graph (ds-ng-2.ttl):
    @prefix dc: <http://purl.org/dc/elements/1.1/> .

    [] dc:title "Harry Potter and the Sorcerer's Stone" .
    [] dc:title "Harry Potter and the Chamber of Secrets" .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# query
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <.>

SELECT ?date ?title
{
?g dc:date ?date . FILTER (?date > "2005-08-01T00:00:00Z"^^xsd:dateTime )
GRAPH ?g
{ ?b dc:title ?title }
}

# result
-----------------------------------------------------------------------------------------
| date | title |
=========================================================================================
| "2005-09-22T05:53:05+01:00"^^xsd:dateTime | "Harry Potter and the Sorcerer's Stone" |
| "2005-09-22T05:53:05+01:00"^^xsd:dateTime | "Harry Potter and the Chamber of Secrets" |
-----------------------------------------------------------------------------------------


# specific graph
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <.>

SELECT ?title
{
GRAPH :ds-ng-2.ttl
{ ?b dc:title ?title }
}

# result
---------------------------------------------
| title |
=============================================
| "Harry Potter and the Sorcerer's Stone" |
| "Harry Potter and the Chamber of Secrets" |
---------------------------------------------

# note that:
GRAPH <IRI> { ... pattern ... }
GRAPH VAR { ... pattern ... }
1
2
3
4
5
6
7
8
9
10
11
12
# FROM NAMED
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <.>

SELECT *
FROM <ds-dft.ttl>
FROM NAMED <ds-ng-1.ttl>
FROM NAMED <ds-ng-2.ttl>
{
{ ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } }
}

Producing Result Sets

SPARQL has four result forms:

  • SELECT – Return a table of results.
  • CONSTRUCT – Return an RDF graph, based on a template in the query.
  • DESCRIBE – Return an RDF graph, based on what the query processor is configured to return.
  • ASK – Ask a boolean query.

Pattern matching produces a set of solutions. This set can be modified in various ways:

  • Order By modifier: put the solutions in order
  • Projection modifier: choose certain variables
  • Distinct modifier: ensure solutions in the sequence are unique
  • Reduced modifier: permit any non-unique solutions to be eliminated
  • Offset modifier: control where the solutions start from in the overall sequence of solutions
  • Limit modifier: restrict the number of solutions