Semantic Web
SPARQL

Hideaki Takeda
National Institute of Informatics
takeda＠nii.ac.jp
Hideaki Takeda / National Institute of Informatics

How to use Linked Data

Linked Data
BrowserThingsLinked Data
MashupThings

ThingsLinked Data
Search EngineThings

ThingsSPARQLRDF Store
Hideaki Takeda / National Institute of Informatics

SPARQLSPARQL Query Language for RDF
W3C Recommendation 15 January 2008
http://www.w3.org/TR/rdf-sparql-query/

Data query language for RDF
l Similar syntax with SQL
lQuery form

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
Variable for query
SELECT ?name
WHERE {?person foaf:name ?name .
}

SPAQL
Query

RDF Store(RDF Database)

Query pattern (Turtle syntax)
Hideaki Takeda / National Institute of Informatics

Query form and results

Query form

Results

SELECT, ASK

XML, (JSON)CONSTRUCT,DESCRIBERDF Graph

Hideaki Takeda / National Institute of Informatics

CONSTRUCTPREFIX foaf: <http://xmlns.com/foaf/0.1/>
CONSTRUCT {?person foaf:name ?name .
}FROM <http://fumi.me/foaf.rdf>
WHERE {
?person foaf:name ?name .
}Hideaki Takeda / National Institute of Informatics

Multiple query patterns
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
All variables
SELECT *WHERE {
?person foaf:name ?name .
Matching multiple patterns?person foaf:mbox ?email .
}PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT *WHERE {
?person foaf:name ?name;foaf:mbox ?email .
}Hideaki Takeda / National Institute of Informatics

Connected graph
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name ?uri
WHERE {
?person foaf:knows ?who .
?who foaf:name ?name;rdfs:seeAlso ?uri .
}?personfoaf:name?namerdfs:seeAlso?uri?whofoaf:knows

Hideaki Takeda / National Institute of Informatics

Optional valuesl OK if it is not matched but return values if matched

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name ?uri
WHERE {?person foaf:knows ?who .
?who foaf:name ?name .
OPTIONAL {?who rdfs:seeAlso ?uri .
}}Hideaki Takeda / National Institute of Informatics

Modifiers
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name ?uri
WHERE {
?person foaf:knows ?who .
?who foaf:name ?name;OPTIONAL {rdfs:seeAlso ?uri .
}Ascending Sort}(if descending ORDER BY DESC(?name) )ORDER BY ?name
LIMIT 10Upper bound on the number of solutions
Hideaki Takeda / National Institute of Informatics

UNIONPREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE {
{ ?person foaf:name ?name;foaf:mbox ?mbox .
} UNION {?person foaf:name ?name;foaf:mbox_sha1sum ?mbox .
}}Hideaki Takeda / National Institute of Informatics

FROM
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
FROM <http://fumi.me/foaf.rdf>
WHERE {
?person foaf:name ?name .
Default dataset}

Hideaki Takeda / National Institute of Informatics

Multiple Dataset
eliminate duplicates

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name
FROM <http://www.w3.org/People/Berners-Lee/card>
FROM <http://fumi.me/foaf.rdf>
FROM <http://www.tom.sfc.keio.ac.jp/~hagino/foaf.rdf>
WHERE {
?person foaf:name ?name .
Merging dataset}

Hideaki Takeda / National Institute of Informatics

Named Graphll

Distinguish data resources by URI/IRI
Query for specifying Graphs

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?g ?name
FROM NAMED <http://www.w3.org/People/Berners-Lee/card>
FROM NAMED <http://fumi.me/foaf.rdf>
WHERE {
GRAPH ?g {?person foaf:name ?name .
Specifying graph resource}}

Hideaki Takeda / National Institute of Informatics

Default graph and Named graph
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rss: <http://purl.org/rss/1.0/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?name ?title ?link ?date
FROM <http://planetrdf.com/index.rdf>
FROM NAMED <http://fumi.me/foaf.rdf>
WHERE {
?item dc:creator ?name .
?item rss:title ?title .
?item rss:link ?link .
?item dc:date ?date .
GRAPH <http://fumi.me/foaf.rdf> {?me foaf:name “武田英明"@ja .
?me foaf:knows ?who .
?who foaf:name ?name .
}.
}Hideaki Takeda / National Institute of Informatics

FILTER: regex
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?name ?email
FROM <http://fumi.me/foaf.rdf>
WHERE {
?person foaf:name ?name;foaf:mbox ?email .
FILTER regex(str(?email), "w3.org”, “i”)}

Hideaki Takeda / National Institute of Informatics

FILTER: xsd:Date
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rss: <http://purl.org/rss/1.0/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?name ?title ?link ?date
FROM <http://planetrdf.com/index.rdf>
WHERE {?item dc:creator ?name .
?item rss:title ?title .
?item rss:link ?link .
?item dc:date ?date .
FILTER ( xsd:dateTime(?date) > "2008-03-31T00:00:00Z”^^xsd:dateTime )}Or compare as string FILTER ( str(?date) > "2008-03-31”)

Hideaki Takeda / National Institute of Informatics

FILTER
Logical connectives: !, &&, ||l Arithmetic connectives: +, -, *, /l Test: =, !=, >, <, ...
l Unary Test: isURI, isBlank, isLiteral, BOUNDl Data type: xsd:integer, xsd:decimal, xsd:float, xsd:double, xsd:string,
xsd:boolean, xsd:dateTimel Language Tag: @EN, @JPl regex, str
lHideaki Takeda / National Institute of Informatics

Language tag
Specifying language for literalsn Turtle: “Tokyo”@enn RDF/XML: xml:lang=“en“l FILTERn FILTER (lang(?name)=“en”)l Note: literal with language tag is not equivalent to one without
language tagn FILTER (?city = “Tokyo”)
u No match with ”Tokyo”@enlHideaki Takeda / National Institute of Informatics

Data typel Types for literals
n “1.3”^^xsd:decimaln “2008-05-01T09:12:58Z”^^xsd:dateTimen “true”^^xsd:booleann “Japan”^^xsd:stringl Castingn Casting may be needed for applying operaters.
u FILTER regex(str(?title), “Semantic”)
u FILTER xsd:int(?age) > 19
u FILTER

Hideaki Takeda / National Institute of Informatics

Find doctoral students supervised by the Japanesescholar awarded by Nobel Prize of Physics
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX dbp-owl: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?sname
WHERE {?scholar a dbp-owl:Person ; dbp-owl:award dbp:Nobel_Prize_in_Physics;
dbp-owl:doctoralStudent ?student; dbp-owl:nationality dbp:Japan .
?student rdfs:label ?sname .
Yoji Totuka}
rdfs:label
dbp-owl:Place

rdf:type

Dbp:Yoji_Totsuka

rdf:type
rdfs:label

foaf:Person

dbp-owl:doctoral_student
dbp:Japan
Dbp-owl:natinality
dbp:Masatoshi_KoshibaJapan

dbp-prop:dateOfBirthrdfs:label

rdf:type

foaf:Person

dbp-owl:award

dbp:Nobel_Priz
Msatoshi
e_in_Physics 20
Koshiba
Hideaki Takeda / National Institute of Informatics

1926-09-19Hideaki Takeda / National Institute of Informatics