SPARQL

From XennisWiki
Jump to: navigation, search

SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is an RDF query language, that is, a query language for databases, able to retrieve and manipulate data stored in Resource Description Framework format. (Wikipedia)

Example

Select

Find all persons, order them by identifier and output optional their name.

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd:  <http://www.w3.org/2001/XMLSchema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?person ?name
WHERE {
	# a = rdf:type
	?person a foaf:Person .
	OPTIONAL {
		?person foaf:name ?name .
	}
}
ORDER BY ?person

Find all persons, who have a father and mother, and output both the person and the parent.

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd:  <http://www.w3.org/2001/XMLSchema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix fam:  <http://www.example.com/family#>
prefix sim:  <http://www.example.com/simpsons#>

SELECT ?person ?parent
WHERE {
	?person a foaf:Person .
	{ ?person fam:hasFather ?parent . }
	UNION
	{ ?person fam:hasMother ?parent . }
}
ORDER BY ?parent

Find everyone, whose name starts with a M (Note: [] is a blank node).

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd:  <http://www.w3.org/2001/XMLSchema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix fam:  <http://www.example.com/family#>
prefix sim:  <http://www.example.com/simpsons#>

SELECT ?person
WHERE {
	[] foaf:name ?name .
# SPARQL 1.0
	FILTER(regex(str(?name), "^M")) .
# SPARQL >= 1.1
#       FILTER(STRSTARTS(STR(?name), "M")) .
}

Find all of Maggie's grandmothers.

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd:  <http://www.w3.org/2001/XMLSchema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix fam:  <http://www.example.com/family#>
prefix sim:  <http://www.example.com/simpsons#>

SELECT ?grandmother
WHERE {
	sim:Maggie fam:hasParent ?parent .
	?parent fam:hasMother ?grandmother .
}

Find each person, which is older then 10.

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd:  <http://www.w3.org/2001/XMLSchema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix fam:  <http://www.example.com/family#>
prefix sim:  <http://www.example.com/simpsons#>

SELECT ?person ?age
WHERE {
	?simpson foaf:name ?person .
	?simpson foaf:age ?age .
	FILTER(?age > 10) .
}
ORDER BY desc(?age)

Ask

Ask, if Herb is the brother of Homer.

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd:  <http://www.w3.org/2001/XMLSchema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix fam:  <http://www.example.com/family#>
prefix sim:  <http://www.example.com/simpsons#>

ASK {
	sim:Herb fam:hasBrother sim:Homer .
}

Construct

Produce a FOAF file for Homer, add his name, a foaf:knows relationship to his spouse and the name of his spouse.

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd:  <http://www.w3.org/2001/XMLSchema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix fam:  <http://www.example.com/family#>
prefix sim:  <http://www.example.com/simpsons#>

CONSTRUCT {
	# Homer
	sim:Homer a foaf:Person .
	sim:Homer foaf:name ?name .
	sim:Homer foaf:knows ?spouse .
	# Homers spouse
	?spouse a foaf:Person .
	?spouse foaf:name ?spouseName
}
WHERE {
	sim:Homer foaf:name ?name .
	sim:Homer fam:hasSpouse ?spouse .
	?spouse foaf:name ?spouseName .
}

See also