SlideShare a Scribd company logo
Linked Data &
Semantic Web
Technology
The Semantic Web
Part 10. SPARQL
Dr. Myungjin Lee
Linked Data & Semantic Web Technology
Query Language
• What is SQL (Structured Query Language)?
– a special-purpose programming language designed for managing data
held in a relational database management system (RDBMS)
• How to access the knowledge from RDF knowledge base?
2
Linked Data & Semantic Web Technology
SPARQL
• What is SPARQL?
– SPARQL Protocol and RDF Query Language
– an RDF query language to retrieve and manipulate data stored in
Resource Description Framework format
– W3C Recommendation 15 January 2008
• SPARQL recommendations consist of:
– Query Language for RDF Graph
– Protocol Layer to use SPARQL via HTTP
– XML Output Format for SPARQL Queries
3
Linked Data & Semantic Web Technology
SPARQL Query From
• SELECT
– to identify the variables to appear in the query results
• FROM
– to specify the dataset to be used for matching
• WHERE
– to provide the basic graph pattern to match against the data graph
4
SELECT …
FROM …
WHERE {
…
}
Linked Data & Semantic Web Technology
SQL and SPARQL
5
SELECT name
FROM users
WHERE contact=„011-201-1111‟;
SELECT ?name
FROM <http://semantics.kr/user.rdf>
WHERE {
?user rdf:type :User.
?user :name ?name.
?user :contact “011-201-1111”.
}
SQL
SPARQL
Linked Data & Semantic Web Technology
Query Variables of SELECT Clause
• Query Variables
– prefixed by either "?" or "$"
– use of a given variable name anywhere in a query identifies the same
variable (global scope)
– ?name, ?title, ?author
• Query Results as a table
6
SELECT ?title ?author
title author
Novel XML Myungjin Lee
Web Tim-Berners Lee
SPARQL
Query Results
Knowledge Base
Linked Data & Semantic Web Technology
Pattern of WHERE Clause
• Triple Pattern
– written as a whitespace-separated list of a subject, predicate and
object
• Graph Pattern
– a set of triple patterns
• WHERE Clause of SPARQL
– based on RDF Turtle serialization and graph pattern matching
7
Linked Data & Semantic Web Technology
Simple Query Example
8
Data
<http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL Tutorial" .
Query
SELECT ?title
WHERE
{
<http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .
}
Query Result
title
“SPARQL Tutorial”
Linked Data & Semantic Web Technology
Multiple Matches
9
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Johnny Lee Outlaw" .
_:a foaf:mbox <mailto:jlow@example.com> .
_:b foaf:name "Peter Goodguy" .
_:b foaf:mbox <mailto:peter@example.org> .
_:c foaf:mbox <mailto:carol@example.org> .
Query
SELECT ?name ?mbox
WHERE
{ ?x <http://xmlns.com/foaf/0.1/name> ?name .
?x <http://xmlns.com/foaf/0.1/mbox> ?mbox }
Query Result
name mbox
“Johnny Lee Outlaw” <mailto:jlow@example.com>
“Peter Goodguy” <mailto:peter@example.org>
Linked Data & Semantic Web Technology
Syntax for Triple Patterns
• Predicate-Object Lists
• Object Lists
10
?x foaf:name ?name ;
foaf:mbox ?mbox .
?x foaf:name ?name .
?x foaf:mbox ?mbox .
?x foaf:nick "Alice" , "Alice_" .
?x foaf:nick "Alice" .
?x foaf:nick "Alice_" .
?x foaf:name ?name ; foaf:nick "Alice" , "Alice_" .
?x foaf:name ?name .
?x foaf:nick "Alice" .
?x foaf:nick "Alice_" .
Linked Data & Semantic Web Technology
Syntax for Triple Patterns
• rdf:type
– the keyword "a" can be used as a predicate in a triple pattern
– an alternative for the IRI
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
11
?x a :Class1 .
[ a :appClass ] :p "v" .
?x rdf:type :Class1 .
_:b0 rdf:type :appClass .
_:b0 :p "v" .
Linked Data & Semantic Web Technology
PREFIX and BASE Keywords
• PREFIX Keyword
– to associate a prefix label with an IRI
– a prefix label and a local part, separated by a colon ":“
• BASE Keyword
– to define the Base IRI
12
<http://example.org/book/book1>
BASE <http://example.org/book/>
<book1>
PREFIX book: <http://example.org/book/>
book:book1
Linked Data & Semantic Web Technology
PREFIX and BASE Keywords
13
PREFIX dc: <http://purl.org/dc/elements/purl.org/>
SELECT ?title
WHERE { <http://example.org/book/book> dc:title ?title }
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://example.org/book/>
SELECT $title
WHERE { :book1 dc:title $title }
BASE <http://example.org/book/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT $title
WHERE { <book1> dc:title $title }
Linked Data & Semantic Web Technology
• Literals with Language Tags
– expressed using @ and the
language tag
• Literals with Numeric Types
– Integers in a SPARQL query indicate
an RDF typed literal with the
datatype xsd:integer.
RDF Literals
14
Data
@prefix dt: <http://example.org/datatype#> .
@prefix ns: <http://example.org/ns#> .
@prefix : <http://example.org/ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:x ns:p "cat"@en .
:y ns:p "42"^^xsd:integer .
:z ns:p "abc"^^dt:specialDatatype .
Query
SELECT ?v WHERE { ?v ?p "cat" }
Query Result
v
Query
SELECT ?v WHERE { ?v ?p 42 }
Query Result
v
<http://example.org/ns#y>
Linked Data & Semantic Web Technology
Syntax for Literals
• the General Syntax for Literals
– a string (enclosed in either double quotes, "...", or single quotes, '...'), with
either an optional language tag or an optional datatype IRI or prefixed
name
– integers, decimal numbers, and booleans can be written directly (without
quotation marks and an explicit datatype IRI)
• Examples
– "chat"
– 'chat'@fr with language tag "fr"
– "xyz"^^<http://example.org/ns/userDatatype>
– "abc"^^appNS:appDataType
– 1, which is the same as "1"^^xsd:integer
– 1.3, which is the same as "1.3"^^xsd:decimal
– 1.0e6, which is the same as "1.0e6"^^xsd:double
– true, which is the same as "true"^^xsd:boolean
15
Linked Data & Semantic Web Technology
RDF Term Constraints
• FILTER Keyword
– to restrict solutions to those for which the filter expression evaluates to
TRUE
16
Data
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix : <http://example.org/book/> .
@prefix ns: <http://example.org/ns#> .
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
Linked Data & Semantic Web Technology
• Restricting the Values of
Strings
– regex function
• to match only plain literals
with no language tag
• Restricting Numeric Values
RDF Term Constraints
17
Query
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?title
WHERE { ?x dc:title ?title
FILTER regex(?title, "^SPARQL")
}
Query Result
title
“SPARQL Tutorial”
Query
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price
WHERE { ?x ns:price ?price .
FILTER (?price < 30.5)
?x dc:title ?title . }
Query Result
title price
“The Semantic Web” 23
Linked Data & Semantic Web Technology
Optional Pattern Matching
• OPTIONAL keyword
– to select optional elements from the RDF graph
18
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
_:a rdf:type foaf:Person .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@example.com> .
_:a foaf:mbox <mailto:alice@work.example> .
_:b rdf:type foaf:Person .
_:b foaf:name "Bob" .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE { ?x foaf:name ?name .
OPTIONAL { ?x foaf:mbox ?mbox }
}
Query Result
name mbox
“Alice” <mailto:alice@example.com>
“Alice” <mailto:alice@work.example>
“Bob”
Linked Data & Semantic Web Technology
Matching Alternatives
• a means of combining graph patterns
19
Data
@prefix dc10: <http://purl.org/dc/elements/1.0/> .
@prefix dc11: <http://purl.org/dc/elements/1.1/> .
_:a dc10:title "SPARQL Query Language Tutorial" .
_:a dc10:creator "Alice" .
_:b dc11:title "SPARQL Protocol Tutorial" .
_:b dc11:creator "Bob" .
_:c dc10:title "SPARQL" .
_:c dc11:title "SPARQL (updated)" .
Query
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?title
WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } }
Query Result
name
"SPARQL Protocol Tutorial"
"SPARQL"
"SPARQL (updated)"
"SPARQL Query Language Tutorial"
Linked Data & Semantic Web Technology
ORDER BY
• ORDER BY Clause
– the order of a solution sequence
20
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name }
ORDER BY ?name
PREFIX : <http://example.org/ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY DESC(?emp)
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY ?name DESC(?emp)
Linked Data & Semantic Web Technology
Duplicate Solutions
• DISTINCT Modifier
– to eliminate duplicate solutions
21
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:x foaf:name "Alice" .
_:x foaf:mbox <mailto:alice@example.com> .
_:y foaf:name "Alice" .
_:y foaf:mbox <mailto:asmith@example.com> .
_:z foaf:name "Alice" .
_:z foaf:mbox <mailto:alice.smith@example.com> .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }
Query Result
name
“Alice”
Linked Data & Semantic Web Technology
OFFSET and LIMIT
• LIMIT Clause
– to put an upper bound on the number of solutions returned
• OFFSET Clause
– to cause the solutions generated to start after the specified number of
solutions
22
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name }
ORDER BY ?name
LIMIT 5
OFFSET 10
Linked Data & Semantic Web Technology
Query Forms
• Four Query Forms
– SELECT
• Returns all, or a subset of, the variables bound in a query pattern match.
– CONSTRUCT
• Returns an RDF graph constructed by substituting variables in a set of triple
templates.
– ASK
• Returns a boolean indicating whether a query pattern matches or not.
– DESCRIBE
• Returns an RDF graph that describes the resources found.
23
Linked Data & Semantic Web Technology
SELECT Form
• to return variables and their bindings directly
24
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:knows _:b .
_:a foaf:knows _:c .
_:b foaf:name "Bob" .
_:c foaf:name "Clare" .
_:c foaf:nick "CT" .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?nameX ?nameY ?nickY
WHERE
{ ?x foaf:knows ?y ;
foaf:name ?nameX .
?y foaf:name ?nameY .
OPTIONAL { ?y foaf:nick ?nickY }
}
Query Result
nameX nameY nickY
“Alice” “Bob”
“Alice” “Clare” “CT”
Linked Data & Semantic Web Technology
CONSTRUCT Form
• to return a single RDF graph specified by a graph template
25
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@example.org> .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
CONSTRUCT { <http://example.org/person#Alice> vcard:FN ?name }
WHERE { ?x foaf:name ?name }
Query Result
@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
<http://example.org/person#Alice> vcard:FN "Alice" .
Linked Data & Semantic Web Technology
ASK Form
• to test whether or not a query pattern has a solution
26
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:homepage <http://work.example.org/alice/> .
_:b foaf:name "Bob" .
_:b foaf:mbox <mailto:bob@work.example> .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
ASK { ?x foaf:name "Alice" }
Query Result
yes
Linked Data & Semantic Web Technology
SPARQL Query Results XML Format
• an XML format for the variable binding and boolean results
formats provided by the SPARQL query language for RDF
27
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="x"/>
<variable name="hpage"/>
<variable name="name"/>
<variable name="age"/>
<variable name="mbox"/>
<variable name="friend"/>
</head>
<results>
<result>
<binding name="x">
<bnode>r2</bnode>
</binding>
<binding name="hpage">
<uri>http://work.example.org/bob/</uri>
</binding>
<binding name="name">
<literal xml:lang="en">Bob</literal>
</binding>
<binding name="age">
<literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal>
</binding>
</result>
...
</results>
</sparql>
Linked Data & Semantic Web Technology
SPARQL Query Results XML Format
• Document Element
• Header
– to contain a sequence of elements describing the set of Query Variable
names in the Solution Sequence
28
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
...
</sparql>
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="x"/>
<variable name="hpage"/>
<variable name="name"/>
<variable name="mbox"/>
<variable name="blurb"/>
</head>
...
</sparql>
Linked Data & Semantic Web Technology
SPARQL Query Results XML Format
• Results
– the results element contains the complete sequence of query results
– a result child-element of results is added giving a document
– each result element corresponds to one Query Solution in a result
– each binding inside a solution is written as an element binding as a child of
result with the query variable name as the value of the name attribute
• the content of the binding
– RDF URI Reference U
• <binding><uri>U</uri></binding>
– RDF Literal S
• <binding><literal>S</literal></binding>
– RDF Literal S with language L
• <binding><literal xml:lang="L">S</literal></binding>
– RDF Typed Literal S with datatype URI D
• <binding><literal datatype="D">S</literal></binding>
– Blank Node label I
• <binding><bnode>I</bnode></binding>
29
Linked Data & Semantic Web Technology
an Example of a Query Solution
1. <?xml version="1.0"?>
2. <sparql xmlns="http://www.w3.org/2005/sparql-results#">
3. <head>
4. <variable name="x"/>
5. <variable name="hpage"/>
6. <variable name="name"/>
7. <variable name="age"/>
8. <variable name="mbox"/>
9. <variable name="friend"/>
10. </head>
11. <results>
12. <result>
13. <binding name="x">
14. <bnode>r2</bnode>
15. </binding>
16. <binding name="hpage">
17. <uri>http://work.example.org/bob/</uri>
18. </binding>
19. <binding name="name">
20. <literal xml:lang="en">Bob</literal>
21. </binding>
22. <binding name="age">
23. <literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal>
24. </binding>
25. <binding name="mbox">
26. <uri>mailto:bob@work.example.org</uri>
27. </binding>
28. </result>
29. ...
30. </results>
31. </sparql>
30
Linked Data & Semantic Web Technology
SPARQL Protocol for RDF
• SPARQL Protocol
– method to query/respond of SPARQL queries via HTTP
• Two Types of SPARQL Protocol
– HTTP Bindings
– SOAP Bindings
31
Linked Data & Semantic Web Technology
HTTP Bindings
32
Query
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?book ?who
WHERE { ?book dc:creator ?who }
Query Result
HTTP/1.1 200 OK
Date: Fri, 06 May 2005 20:55:12 GMT
Server: Apache/1.3.29 (Unix) PHP/4.3.4 DAV/1.0.3
Connection: close
Content-Type: application/sparql-results+xml
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="book"/>
<variable name="who"/>
</head>
<results distinct="false" ordered="false">
<result>
<binding name="book"><uri>http://www.example/book/book5</uri></binding>
<binding name="who"><bnode>r29392923r2922</bnode></binding>
</result>
...
</sparql>
HTTP Message
GET /sparql/?query=EncodedQuery HTTP/1.1
Host: www.example
User-agent: my-sparql-client/0.1
Knowledge Base
Linked Data & Semantic Web Technology
SOAP Bindings
33
Query Result
HTTP/1.1 200 OK
Content-Type: application/soap+xml
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<query-result xmlns="http://www.w3.org/2005/09/sparql-protocol-types/#">
<ns1:sparql xmlns:ns1="http://www.w3.org/2005/sparql-results#">
<ns1:head>
<ns1:variable name="z"/>
</ns1:head>
<ns1:results distinct="false" ordered="false">
<ns1:result>
<ns1:binding name="z">
<ns1:literal>Harry Potter and the Chamber of Secrets</ns1:literal>
</ns1:binding>
</ns1:result>
...
</ns1:results>
</ns1:sparql>
</query-result>
</soapenv:Body>
</soapenv:Envelope>
SOAP Request
POST /services/sparql-query HTTP/1.1
Content-Type: application/soap+xml
Accept: application/soap+xml, multipart/related, text/*
User-Agent: Axis/1.2.1
Host: www.example
SOAPAction: ""
Content-Length: 438
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<query-request xmlns="http://www.w3.org/2005/09/sparql-protocol-types/#">
<query>SELECT ?z {?x ?y ?z . FILTER regex(?z, 'Harry')}</query>
</query-request>
</soapenv:Body>
</soapenv:Envelope>
Knowledge Base
Linked Data & Semantic Web Technology
References
• http://en.wikipedia.org/wiki/Sparql
• http://www.w3.org/TR/rdf-sparql-query/
• http://www.w3.org/TR/rdf-sparql-protocol/
• http://www.w3.org/TR/2008/REC-rdf-sparql-XMLres-20080115/
• http://www.slideshare.net/lysander07/openhpi-semweb03part1
• http://www.slideshare.net/lysander07/open-hpi-semweb03part2
• http://www.slideshare.net/lysander07/openhpi-33-how-to-query-rdfs-sparql3
• http://www.slideshare.net/fabien_gandon/sparql-in-a-nutshell
• http://www.slideshare.net/kwangsub/rdf-tutorial-sparql-20091031
34
Linked Data & Semantic Web Technology
3535
Dr. Myungjin Lee
e-Mail : mjlee@li-st.com
Twitter : http://twitter.com/MyungjinLee
Facebook : http://www.facebook.com/mjinlee
SlideShare : http://www.slideshare.net/onlyjiny/

More Related Content

What's hot (20)

PPTX
SWT Lecture Session 2 - RDF
Mariano Rodriguez-Muro
 
PDF
RDF Tutorial - SPARQL 20091031
kwangsub kim
 
PDF
FOAF
R A Akerkar
 
PDF
An introduction to Semantic Web and Linked Data
Fabien Gandon
 
PPTX
Saveface - Save your Facebook content as RDF data
Fuming Shih
 
PPTX
GDG Meets U event - Big data & Wikidata - no lies codelab
CAMELIA BOBAN
 
PDF
Linked (Open) Data
Bernhard Haslhofer
 
PPT
Linked Data Tutorial
Sören Auer
 
KEY
Linked data: spreading data over the web
shellac
 
PPT
SemanticWeb Nuts 'n Bolts
Rinke Hoekstra
 
PDF
Introduction to RDF
Dr Sukhpal Singh Gill
 
PPTX
RDF Data Model
Jose Emilio Labra Gayo
 
PPTX
Querying Linked Data on Android
EUCLID project
 
KEY
RDFa Introductory Course Session 2/4 How RDFa
Platypus
 
PDF
Introduction to RDFa
Ivan Herman
 
PPTX
Name That Graph !
Fabien Gandon
 
PPTX
RDFa: an introduction
Kai Li
 
PPTX
Usage of Linked Data: Introduction and Application Scenarios
EUCLID project
 
PPTX
4 sw architectures and sparql
Mariano Rodriguez-Muro
 
PDF
An introduction to Semantic Web and Linked Data
Gabriela Agustini
 
SWT Lecture Session 2 - RDF
Mariano Rodriguez-Muro
 
RDF Tutorial - SPARQL 20091031
kwangsub kim
 
An introduction to Semantic Web and Linked Data
Fabien Gandon
 
Saveface - Save your Facebook content as RDF data
Fuming Shih
 
GDG Meets U event - Big data & Wikidata - no lies codelab
CAMELIA BOBAN
 
Linked (Open) Data
Bernhard Haslhofer
 
Linked Data Tutorial
Sören Auer
 
Linked data: spreading data over the web
shellac
 
SemanticWeb Nuts 'n Bolts
Rinke Hoekstra
 
Introduction to RDF
Dr Sukhpal Singh Gill
 
RDF Data Model
Jose Emilio Labra Gayo
 
Querying Linked Data on Android
EUCLID project
 
RDFa Introductory Course Session 2/4 How RDFa
Platypus
 
Introduction to RDFa
Ivan Herman
 
Name That Graph !
Fabien Gandon
 
RDFa: an introduction
Kai Li
 
Usage of Linked Data: Introduction and Application Scenarios
EUCLID project
 
4 sw architectures and sparql
Mariano Rodriguez-Muro
 
An introduction to Semantic Web and Linked Data
Gabriela Agustini
 

Viewers also liked (6)

PDF
Linked Open Data Tutorial
Myungjin Lee
 
PPTX
The Semantic Web #6 - RDF Schema
Myungjin Lee
 
PPTX
The Semantic Web #3 - URI
Myungjin Lee
 
PDF
The Semantic Web #8 - Ontology
Myungjin Lee
 
PPTX
Linked Data Modeling for Beginner
Myungjin Lee
 
PPTX
The Semantic Web #7 - RDF Semantics
Myungjin Lee
 
Linked Open Data Tutorial
Myungjin Lee
 
The Semantic Web #6 - RDF Schema
Myungjin Lee
 
The Semantic Web #3 - URI
Myungjin Lee
 
The Semantic Web #8 - Ontology
Myungjin Lee
 
Linked Data Modeling for Beginner
Myungjin Lee
 
The Semantic Web #7 - RDF Semantics
Myungjin Lee
 
Ad

Similar to The Semantic Web #10 - SPARQL (20)

PPT
Sparql
Serge Garlatti
 
PPT
SPARQL in the Semantic Web
Jan Beeck
 
PPT
Re-using Media on the Web: Media fragment re-mixing and playout
MediaMixerCommunity
 
PPTX
20100614 ISWSA Keynote
Axel Polleres
 
PDF
SPARQL and Linked Data
Fulvio Corno
 
PPTX
Sparql
Tamrat Amare
 
PPT
A hands on overview of the semantic web
Marakana Inc.
 
PDF
RDF Seminar Presentation
Muntazir Mehdi
 
ODP
SPARQL 1.1 Update (2013-03-05)
andyseaborne
 
PPTX
A Little SPARQL in your Analytics
Dr. Neil Brittliff
 
PPTX
What;s Coming In SPARQL2?
LeeFeigenbaum
 
PPSX
Introduction to SPARQL
Pedro Szekely
 
KEY
Linked services
Carlos Pedrinaci
 
PPTX
Madrid SPARQL handson
Victor de Boer
 
PDF
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
Franck Michel
 
PPT
From SQL to SPARQL
George Roth
 
PPTX
Semantic Web and Related Work at W3C
Ivan Herman
 
PPTX
SPARQL introduction and training (130+ slides with exercices)
Thomas Francart
 
PDF
Introducción a la web semántica - Linkatu - irekia 2012
Alberto Labarga
 
PPTX
Hack U Barcelona 2011
Peter Mika
 
SPARQL in the Semantic Web
Jan Beeck
 
Re-using Media on the Web: Media fragment re-mixing and playout
MediaMixerCommunity
 
20100614 ISWSA Keynote
Axel Polleres
 
SPARQL and Linked Data
Fulvio Corno
 
Sparql
Tamrat Amare
 
A hands on overview of the semantic web
Marakana Inc.
 
RDF Seminar Presentation
Muntazir Mehdi
 
SPARQL 1.1 Update (2013-03-05)
andyseaborne
 
A Little SPARQL in your Analytics
Dr. Neil Brittliff
 
What;s Coming In SPARQL2?
LeeFeigenbaum
 
Introduction to SPARQL
Pedro Szekely
 
Linked services
Carlos Pedrinaci
 
Madrid SPARQL handson
Victor de Boer
 
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
Franck Michel
 
From SQL to SPARQL
George Roth
 
Semantic Web and Related Work at W3C
Ivan Herman
 
SPARQL introduction and training (130+ slides with exercices)
Thomas Francart
 
Introducción a la web semántica - Linkatu - irekia 2012
Alberto Labarga
 
Hack U Barcelona 2011
Peter Mika
 
Ad

More from Myungjin Lee (20)

PDF
지식그래프 개념과 활용방안 (Knowledge Graph - Introduction and Use Cases)
Myungjin Lee
 
PDF
JSP 프로그래밍 #05 HTML과 JSP
Myungjin Lee
 
PDF
JSP 프로그래밍 #04 JSP 의 기본
Myungjin Lee
 
PDF
JSP 프로그래밍 #03 서블릿
Myungjin Lee
 
PDF
JSP 프로그래밍 #02 서블릿과 JSP 시작하기
Myungjin Lee
 
PDF
JSP 프로그래밍 #01 웹 프로그래밍
Myungjin Lee
 
PDF
관광 지식베이스와 스마트 관광 서비스 (Knowledge base and Smart Tourism)
Myungjin Lee
 
PDF
오픈 데이터와 인공지능
Myungjin Lee
 
PDF
법령 온톨로지의 구축 및 검색
Myungjin Lee
 
PDF
도서관과 Linked Data
Myungjin Lee
 
PDF
공공데이터, 현재 우리는?
Myungjin Lee
 
PDF
LODAC 2017 Linked Open Data Workshop
Myungjin Lee
 
PDF
Introduction of Deep Learning
Myungjin Lee
 
PDF
서울시 열린데이터 광장 문화관광 분야 LOD 서비스
Myungjin Lee
 
PDF
LOD(Linked Open Data) Recommendations
Myungjin Lee
 
PDF
Interlinking for Linked Data
Myungjin Lee
 
PPTX
Linked Data Usecases
Myungjin Lee
 
PDF
공공데이터와 Linked open data
Myungjin Lee
 
PDF
공공데이터와 Linked open data
Myungjin Lee
 
PPTX
Development of Twitter Application #8 - Streaming API
Myungjin Lee
 
지식그래프 개념과 활용방안 (Knowledge Graph - Introduction and Use Cases)
Myungjin Lee
 
JSP 프로그래밍 #05 HTML과 JSP
Myungjin Lee
 
JSP 프로그래밍 #04 JSP 의 기본
Myungjin Lee
 
JSP 프로그래밍 #03 서블릿
Myungjin Lee
 
JSP 프로그래밍 #02 서블릿과 JSP 시작하기
Myungjin Lee
 
JSP 프로그래밍 #01 웹 프로그래밍
Myungjin Lee
 
관광 지식베이스와 스마트 관광 서비스 (Knowledge base and Smart Tourism)
Myungjin Lee
 
오픈 데이터와 인공지능
Myungjin Lee
 
법령 온톨로지의 구축 및 검색
Myungjin Lee
 
도서관과 Linked Data
Myungjin Lee
 
공공데이터, 현재 우리는?
Myungjin Lee
 
LODAC 2017 Linked Open Data Workshop
Myungjin Lee
 
Introduction of Deep Learning
Myungjin Lee
 
서울시 열린데이터 광장 문화관광 분야 LOD 서비스
Myungjin Lee
 
LOD(Linked Open Data) Recommendations
Myungjin Lee
 
Interlinking for Linked Data
Myungjin Lee
 
Linked Data Usecases
Myungjin Lee
 
공공데이터와 Linked open data
Myungjin Lee
 
공공데이터와 Linked open data
Myungjin Lee
 
Development of Twitter Application #8 - Streaming API
Myungjin Lee
 

Recently uploaded (20)

PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PPTX
ENGLISH 8 REVISED K-12 CURRICULUM QUARTER 1 WEEK 1
LeomarrYsraelArzadon
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PDF
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
PPTX
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
ENGLISH 8 REVISED K-12 CURRICULUM QUARTER 1 WEEK 1
LeomarrYsraelArzadon
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 

The Semantic Web #10 - SPARQL

  • 1. Linked Data & Semantic Web Technology The Semantic Web Part 10. SPARQL Dr. Myungjin Lee
  • 2. Linked Data & Semantic Web Technology Query Language • What is SQL (Structured Query Language)? – a special-purpose programming language designed for managing data held in a relational database management system (RDBMS) • How to access the knowledge from RDF knowledge base? 2
  • 3. Linked Data & Semantic Web Technology SPARQL • What is SPARQL? – SPARQL Protocol and RDF Query Language – an RDF query language to retrieve and manipulate data stored in Resource Description Framework format – W3C Recommendation 15 January 2008 • SPARQL recommendations consist of: – Query Language for RDF Graph – Protocol Layer to use SPARQL via HTTP – XML Output Format for SPARQL Queries 3
  • 4. Linked Data & Semantic Web Technology SPARQL Query From • SELECT – to identify the variables to appear in the query results • FROM – to specify the dataset to be used for matching • WHERE – to provide the basic graph pattern to match against the data graph 4 SELECT … FROM … WHERE { … }
  • 5. Linked Data & Semantic Web Technology SQL and SPARQL 5 SELECT name FROM users WHERE contact=„011-201-1111‟; SELECT ?name FROM <http://semantics.kr/user.rdf> WHERE { ?user rdf:type :User. ?user :name ?name. ?user :contact “011-201-1111”. } SQL SPARQL
  • 6. Linked Data & Semantic Web Technology Query Variables of SELECT Clause • Query Variables – prefixed by either "?" or "$" – use of a given variable name anywhere in a query identifies the same variable (global scope) – ?name, ?title, ?author • Query Results as a table 6 SELECT ?title ?author title author Novel XML Myungjin Lee Web Tim-Berners Lee SPARQL Query Results Knowledge Base
  • 7. Linked Data & Semantic Web Technology Pattern of WHERE Clause • Triple Pattern – written as a whitespace-separated list of a subject, predicate and object • Graph Pattern – a set of triple patterns • WHERE Clause of SPARQL – based on RDF Turtle serialization and graph pattern matching 7
  • 8. Linked Data & Semantic Web Technology Simple Query Example 8 Data <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL Tutorial" . Query SELECT ?title WHERE { <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title . } Query Result title “SPARQL Tutorial”
  • 9. Linked Data & Semantic Web Technology Multiple Matches 9 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Johnny Lee Outlaw" . _:a foaf:mbox <mailto:[email protected]> . _:b foaf:name "Peter Goodguy" . _:b foaf:mbox <mailto:[email protected]> . _:c foaf:mbox <mailto:[email protected]> . Query SELECT ?name ?mbox WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name . ?x <http://xmlns.com/foaf/0.1/mbox> ?mbox } Query Result name mbox “Johnny Lee Outlaw” <mailto:[email protected]> “Peter Goodguy” <mailto:[email protected]>
  • 10. Linked Data & Semantic Web Technology Syntax for Triple Patterns • Predicate-Object Lists • Object Lists 10 ?x foaf:name ?name ; foaf:mbox ?mbox . ?x foaf:name ?name . ?x foaf:mbox ?mbox . ?x foaf:nick "Alice" , "Alice_" . ?x foaf:nick "Alice" . ?x foaf:nick "Alice_" . ?x foaf:name ?name ; foaf:nick "Alice" , "Alice_" . ?x foaf:name ?name . ?x foaf:nick "Alice" . ?x foaf:nick "Alice_" .
  • 11. Linked Data & Semantic Web Technology Syntax for Triple Patterns • rdf:type – the keyword "a" can be used as a predicate in a triple pattern – an alternative for the IRI http://www.w3.org/1999/02/22-rdf-syntax-ns#type 11 ?x a :Class1 . [ a :appClass ] :p "v" . ?x rdf:type :Class1 . _:b0 rdf:type :appClass . _:b0 :p "v" .
  • 12. Linked Data & Semantic Web Technology PREFIX and BASE Keywords • PREFIX Keyword – to associate a prefix label with an IRI – a prefix label and a local part, separated by a colon ":“ • BASE Keyword – to define the Base IRI 12 <http://example.org/book/book1> BASE <http://example.org/book/> <book1> PREFIX book: <http://example.org/book/> book:book1
  • 13. Linked Data & Semantic Web Technology PREFIX and BASE Keywords 13 PREFIX dc: <http://purl.org/dc/elements/purl.org/> SELECT ?title WHERE { <http://example.org/book/book> dc:title ?title } PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX : <http://example.org/book/> SELECT $title WHERE { :book1 dc:title $title } BASE <http://example.org/book/> PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT $title WHERE { <book1> dc:title $title }
  • 14. Linked Data & Semantic Web Technology • Literals with Language Tags – expressed using @ and the language tag • Literals with Numeric Types – Integers in a SPARQL query indicate an RDF typed literal with the datatype xsd:integer. RDF Literals 14 Data @prefix dt: <http://example.org/datatype#> . @prefix ns: <http://example.org/ns#> . @prefix : <http://example.org/ns#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :x ns:p "cat"@en . :y ns:p "42"^^xsd:integer . :z ns:p "abc"^^dt:specialDatatype . Query SELECT ?v WHERE { ?v ?p "cat" } Query Result v Query SELECT ?v WHERE { ?v ?p 42 } Query Result v <http://example.org/ns#y>
  • 15. Linked Data & Semantic Web Technology Syntax for Literals • the General Syntax for Literals – a string (enclosed in either double quotes, "...", or single quotes, '...'), with either an optional language tag or an optional datatype IRI or prefixed name – integers, decimal numbers, and booleans can be written directly (without quotation marks and an explicit datatype IRI) • Examples – "chat" – 'chat'@fr with language tag "fr" – "xyz"^^<http://example.org/ns/userDatatype> – "abc"^^appNS:appDataType – 1, which is the same as "1"^^xsd:integer – 1.3, which is the same as "1.3"^^xsd:decimal – 1.0e6, which is the same as "1.0e6"^^xsd:double – true, which is the same as "true"^^xsd:boolean 15
  • 16. Linked Data & Semantic Web Technology RDF Term Constraints • FILTER Keyword – to restrict solutions to those for which the filter expression evaluates to TRUE 16 Data @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix : <http://example.org/book/> . @prefix ns: <http://example.org/ns#> . :book1 dc:title "SPARQL Tutorial" . :book1 ns:price 42 . :book2 dc:title "The Semantic Web" . :book2 ns:price 23 .
  • 17. Linked Data & Semantic Web Technology • Restricting the Values of Strings – regex function • to match only plain literals with no language tag • Restricting Numeric Values RDF Term Constraints 17 Query PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, "^SPARQL") } Query Result title “SPARQL Tutorial” Query PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ns: <http://example.org/ns#> SELECT ?title ?price WHERE { ?x ns:price ?price . FILTER (?price < 30.5) ?x dc:title ?title . } Query Result title price “The Semantic Web” 23
  • 18. Linked Data & Semantic Web Technology Optional Pattern Matching • OPTIONAL keyword – to select optional elements from the RDF graph 18 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . _:a rdf:type foaf:Person . _:a foaf:name "Alice" . _:a foaf:mbox <mailto:[email protected]> . _:a foaf:mbox <mailto:[email protected]> . _:b rdf:type foaf:Person . _:b foaf:name "Bob" . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } } Query Result name mbox “Alice” <mailto:[email protected]> “Alice” <mailto:[email protected]> “Bob”
  • 19. Linked Data & Semantic Web Technology Matching Alternatives • a means of combining graph patterns 19 Data @prefix dc10: <http://purl.org/dc/elements/1.0/> . @prefix dc11: <http://purl.org/dc/elements/1.1/> . _:a dc10:title "SPARQL Query Language Tutorial" . _:a dc10:creator "Alice" . _:b dc11:title "SPARQL Protocol Tutorial" . _:b dc11:creator "Bob" . _:c dc10:title "SPARQL" . _:c dc11:title "SPARQL (updated)" . Query PREFIX dc10: <http://purl.org/dc/elements/1.0/> PREFIX dc11: <http://purl.org/dc/elements/1.1/> SELECT ?title WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } } Query Result name "SPARQL Protocol Tutorial" "SPARQL" "SPARQL (updated)" "SPARQL Query Language Tutorial"
  • 20. Linked Data & Semantic Web Technology ORDER BY • ORDER BY Clause – the order of a solution sequence 20 PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:name ?name } ORDER BY ?name PREFIX : <http://example.org/ns#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?name WHERE { ?x foaf:name ?name ; :empId ?emp } ORDER BY DESC(?emp) PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:name ?name ; :empId ?emp } ORDER BY ?name DESC(?emp)
  • 21. Linked Data & Semantic Web Technology Duplicate Solutions • DISTINCT Modifier – to eliminate duplicate solutions 21 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:x foaf:name "Alice" . _:x foaf:mbox <mailto:[email protected]> . _:y foaf:name "Alice" . _:y foaf:mbox <mailto:[email protected]> . _:z foaf:name "Alice" . _:z foaf:mbox <mailto:[email protected]> . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT ?name WHERE { ?x foaf:name ?name } Query Result name “Alice”
  • 22. Linked Data & Semantic Web Technology OFFSET and LIMIT • LIMIT Clause – to put an upper bound on the number of solutions returned • OFFSET Clause – to cause the solutions generated to start after the specified number of solutions 22 PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:name ?name } ORDER BY ?name LIMIT 5 OFFSET 10
  • 23. Linked Data & Semantic Web Technology Query Forms • Four Query Forms – SELECT • Returns all, or a subset of, the variables bound in a query pattern match. – CONSTRUCT • Returns an RDF graph constructed by substituting variables in a set of triple templates. – ASK • Returns a boolean indicating whether a query pattern matches or not. – DESCRIBE • Returns an RDF graph that describes the resources found. 23
  • 24. Linked Data & Semantic Web Technology SELECT Form • to return variables and their bindings directly 24 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Alice" . _:a foaf:knows _:b . _:a foaf:knows _:c . _:b foaf:name "Bob" . _:c foaf:name "Clare" . _:c foaf:nick "CT" . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?nameX ?nameY ?nickY WHERE { ?x foaf:knows ?y ; foaf:name ?nameX . ?y foaf:name ?nameY . OPTIONAL { ?y foaf:nick ?nickY } } Query Result nameX nameY nickY “Alice” “Bob” “Alice” “Clare” “CT”
  • 25. Linked Data & Semantic Web Technology CONSTRUCT Form • to return a single RDF graph specified by a graph template 25 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Alice" . _:a foaf:mbox <mailto:[email protected]> . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> CONSTRUCT { <http://example.org/person#Alice> vcard:FN ?name } WHERE { ?x foaf:name ?name } Query Result @prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> . <http://example.org/person#Alice> vcard:FN "Alice" .
  • 26. Linked Data & Semantic Web Technology ASK Form • to test whether or not a query pattern has a solution 26 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Alice" . _:a foaf:homepage <http://work.example.org/alice/> . _:b foaf:name "Bob" . _:b foaf:mbox <mailto:[email protected]> . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> ASK { ?x foaf:name "Alice" } Query Result yes
  • 27. Linked Data & Semantic Web Technology SPARQL Query Results XML Format • an XML format for the variable binding and boolean results formats provided by the SPARQL query language for RDF 27 <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="x"/> <variable name="hpage"/> <variable name="name"/> <variable name="age"/> <variable name="mbox"/> <variable name="friend"/> </head> <results> <result> <binding name="x"> <bnode>r2</bnode> </binding> <binding name="hpage"> <uri>http://work.example.org/bob/</uri> </binding> <binding name="name"> <literal xml:lang="en">Bob</literal> </binding> <binding name="age"> <literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal> </binding> </result> ... </results> </sparql>
  • 28. Linked Data & Semantic Web Technology SPARQL Query Results XML Format • Document Element • Header – to contain a sequence of elements describing the set of Query Variable names in the Solution Sequence 28 <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> ... </sparql> <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="x"/> <variable name="hpage"/> <variable name="name"/> <variable name="mbox"/> <variable name="blurb"/> </head> ... </sparql>
  • 29. Linked Data & Semantic Web Technology SPARQL Query Results XML Format • Results – the results element contains the complete sequence of query results – a result child-element of results is added giving a document – each result element corresponds to one Query Solution in a result – each binding inside a solution is written as an element binding as a child of result with the query variable name as the value of the name attribute • the content of the binding – RDF URI Reference U • <binding><uri>U</uri></binding> – RDF Literal S • <binding><literal>S</literal></binding> – RDF Literal S with language L • <binding><literal xml:lang="L">S</literal></binding> – RDF Typed Literal S with datatype URI D • <binding><literal datatype="D">S</literal></binding> – Blank Node label I • <binding><bnode>I</bnode></binding> 29
  • 30. Linked Data & Semantic Web Technology an Example of a Query Solution 1. <?xml version="1.0"?> 2. <sparql xmlns="http://www.w3.org/2005/sparql-results#"> 3. <head> 4. <variable name="x"/> 5. <variable name="hpage"/> 6. <variable name="name"/> 7. <variable name="age"/> 8. <variable name="mbox"/> 9. <variable name="friend"/> 10. </head> 11. <results> 12. <result> 13. <binding name="x"> 14. <bnode>r2</bnode> 15. </binding> 16. <binding name="hpage"> 17. <uri>http://work.example.org/bob/</uri> 18. </binding> 19. <binding name="name"> 20. <literal xml:lang="en">Bob</literal> 21. </binding> 22. <binding name="age"> 23. <literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal> 24. </binding> 25. <binding name="mbox"> 26. <uri>mailto:[email protected]</uri> 27. </binding> 28. </result> 29. ... 30. </results> 31. </sparql> 30
  • 31. Linked Data & Semantic Web Technology SPARQL Protocol for RDF • SPARQL Protocol – method to query/respond of SPARQL queries via HTTP • Two Types of SPARQL Protocol – HTTP Bindings – SOAP Bindings 31
  • 32. Linked Data & Semantic Web Technology HTTP Bindings 32 Query PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?book ?who WHERE { ?book dc:creator ?who } Query Result HTTP/1.1 200 OK Date: Fri, 06 May 2005 20:55:12 GMT Server: Apache/1.3.29 (Unix) PHP/4.3.4 DAV/1.0.3 Connection: close Content-Type: application/sparql-results+xml <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results distinct="false" ordered="false"> <result> <binding name="book"><uri>http://www.example/book/book5</uri></binding> <binding name="who"><bnode>r29392923r2922</bnode></binding> </result> ... </sparql> HTTP Message GET /sparql/?query=EncodedQuery HTTP/1.1 Host: www.example User-agent: my-sparql-client/0.1 Knowledge Base
  • 33. Linked Data & Semantic Web Technology SOAP Bindings 33 Query Result HTTP/1.1 200 OK Content-Type: application/soap+xml <?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <query-result xmlns="http://www.w3.org/2005/09/sparql-protocol-types/#"> <ns1:sparql xmlns:ns1="http://www.w3.org/2005/sparql-results#"> <ns1:head> <ns1:variable name="z"/> </ns1:head> <ns1:results distinct="false" ordered="false"> <ns1:result> <ns1:binding name="z"> <ns1:literal>Harry Potter and the Chamber of Secrets</ns1:literal> </ns1:binding> </ns1:result> ... </ns1:results> </ns1:sparql> </query-result> </soapenv:Body> </soapenv:Envelope> SOAP Request POST /services/sparql-query HTTP/1.1 Content-Type: application/soap+xml Accept: application/soap+xml, multipart/related, text/* User-Agent: Axis/1.2.1 Host: www.example SOAPAction: "" Content-Length: 438 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <query-request xmlns="http://www.w3.org/2005/09/sparql-protocol-types/#"> <query>SELECT ?z {?x ?y ?z . FILTER regex(?z, 'Harry')}</query> </query-request> </soapenv:Body> </soapenv:Envelope> Knowledge Base
  • 34. Linked Data & Semantic Web Technology References • http://en.wikipedia.org/wiki/Sparql • http://www.w3.org/TR/rdf-sparql-query/ • http://www.w3.org/TR/rdf-sparql-protocol/ • http://www.w3.org/TR/2008/REC-rdf-sparql-XMLres-20080115/ • http://www.slideshare.net/lysander07/openhpi-semweb03part1 • http://www.slideshare.net/lysander07/open-hpi-semweb03part2 • http://www.slideshare.net/lysander07/openhpi-33-how-to-query-rdfs-sparql3 • http://www.slideshare.net/fabien_gandon/sparql-in-a-nutshell • http://www.slideshare.net/kwangsub/rdf-tutorial-sparql-20091031 34
  • 35. Linked Data & Semantic Web Technology 3535 Dr. Myungjin Lee e-Mail : [email protected] Twitter : http://twitter.com/MyungjinLee Facebook : http://www.facebook.com/mjinlee SlideShare : http://www.slideshare.net/onlyjiny/