SPARQL
RDF GRAPH
-
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> -
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"
] . -
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 |
# basic graph pattern |
1 |
# blank node |
1 |
# FILTER |
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 |
# OPTIONAL |
- 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 |
# UNION |
- 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 |
# query |
1 |
# FROM NAMED |
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





近期评论