SPARQL (SPARQL Protocol And RDF Query Language) is the standardized query language for RDF.
The syntax is actually quite simple if you understand the underlying data model RDF which consists of triples.
A SELECT query consists of binding variables and a set of triple patterns in the WHERE-clause. Here is a simple SELECT query that selects all synonyms (=alternative labels) of a concept:
SELECT ?synonym
WHERE {
<http://reegle.info/glossary/39> <http://www.w3.org/2004/02/skos/core#altLabel> ?synonym.
}
If you run this query against the SPARQL endpoint of reegle you will get the three alternative labels of the concept:
| synonym |
|---|
| "PV cells"@en |
| "solar power cells"@en |
| "photovoltaic cells"@en |
Let's now assume we are interested in the spanish preferred label of the concept. We can get that information by using a basic FILTER expression in our query:
SELECT ?prefLabel
WHERE {
<http://reegle.info/glossary/39> <http://www.w3.org/2004/02/skos/core#prefLabel> ?prefLabel.
FILTER (lang(?prefLabel) = "es")
}
This will filter out all preferred labels that have no language tag "es" attached to them and leave you just with the spanish name:
| prefLabel |
|---|
| "célula solar"@es |
To strip the redundant namespace information from URIs in your SPARQL query you can use prefixes as in most of RDF serialization formats
So in the simple query from above the SKOS namespace can be replaced using a PREFIX declaration at the beginning of the query:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?synonym
WHERE {
<http://reegle.info/glossary/39> skos:altLabel ?synonym.
}
Below are a couple of more advanced example queries you can try using the reegle SPARQL endpoint.
Retrieve all top Concepts of the ConceptScheme "Ocean Power" and their preferred labels in english:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?topConcept ?prefLabel
WHERE {
<http://reegle.info/glossary/562> skos:hasTopConcept ?topConcept.
?topConcept skos:prefLabel ?prefLabel.
FILTER(lang(?prefLabel) = "en")
}
Select all related Concepts of the Concept "solar cells" and their preferred labels:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?relatedConcept ?prefLabel
WHERE {
<http://reegle.info/glossary/39> skos:related ?relatedConcept.
?relatedConcept skos:prefLabel ?prefLabel.
FILTER(lang(?prefLabel) = "en")
}
Returns all Concepts, their preferred labels that start with the letter "A" and the title of the parent ConceptScheme, sorting them alphabetically. A maximum of 50 concepts are returned.:
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
PREFIX dc:<http://purl.org/dc/elements/1.1/>
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?concept ?prefLabel ?conceptSchemeTitle
WHERE
{
?concept skos:prefLabel ?prefLabel .
?concept skos:broaderTransitive ?broaderTransitive.
?scheme skos:hasTopConcept ?broaderTransitive.
?scheme dc:title ?conceptSchemeTitle.
FILTER (regex(str(?prefLabel), '^a', 'i') && lang(?conceptSchemeTitle) = "en" && lang(?prefLabel) = "en")
}
ORDER BY ?prefLabel
LIMIT 50
OFFSET 0
You can use the SPARQL endpoint as a web service simply by sending a HTTP POST request to the endpoint URL and provide the following parameters (URL-encoded):
For SELECT queries the available formats are:
For CONSTRUCT and DESCRIBE queries (returning triples) the available formats are: