SPARQL
SPARQL is the standard query language
used to extract information from RDF data. In rdfpub, SPARQL queries are used
to extract information about your resources for use in Handlebars templates.
Creating SPARQL queries
A SPARQL query is created for each .sparql
file in your site directory. The
'name' part of the file (the file name minus the .sparql
extension) is used
to reference the results of the query in Handlebars templates.
Query inheritance
When an rdfpub site is built, SPARQL queries are recursively copied down from
its own directory to all subdirectories. This allows you to write a generic
SPARQL query that all 'child' resources can use without having to duplicate
your queries for every resource that needs them.
For example,
the lesson
query
of the tutorial site is passed down to each of the individual tutorial lessons.
Generic queries
In order to make queries generic, rdfpub will run your query against every
resource that inherits it with two special variables:
$resource
- the resource that the query is being executed for
$language
- the current language that is currently being queried for
(explained in greater detail in
the i18n lesson)
With these two variables, you can write queries that can apply to any resource
that they're run for. For instance, let's say that you want to write a SPARQL
query that extracts a name and description for a person. Such a query might
look like this:
SELECT $name $description
WHERE {
GRAPH $resource {
$resource a foaf:Person
; foaf:name $name
; dcterms:description $description
}
}
If you define this query as person.sparql
in a directory /person
, then
every resource in a subdirectory will receive this query with its own values
injected into it. So /person/Alice
and /person/Bob
would have their own
set of results for this query based on their individual URL's and RDF data.
Again using
the lesson
query
as an example, not only does it get passed down to each tutorial lesson, but
it also get run against each lesson with its $resource
variable bound to each
lesson's URL and its $language
variable bound to each language that the
lesson supports.
Supported SPARQL query types
As of this writing, only SPARQL tuple queries are supported, that is, queries
that return SPARQL results created from a SELECT
query. All of ASK
,
CONSTRUCT
, and DESCRIBE
are intended to be supported in the future. For
now, SELECT
queries should suffice for almost all practical purposes.
SPARQL endpoint
The rdfpub site generator creates a
SPARQL endpoint for every site that
it generates. This endpoint can be used to make arbitrary SPARQL queries over
your data. This can be useful for anyone looking to examine or analyze your
data. It also allows others to federate their own SPARQL queries to your site
by way of the SERVICE
SPARQL keyword, thus connecting datasets across the
web.
As is common convention, an rdfpub site's SPARQL endpoint is served from the
/sparql
URL by default. It is a SPARQL 1.1 compliant endpoint except that it
does not support SPARQL update operations as rdfpub sites are read-only. A
site's SPARQL endpoint also serves
SPARQL 1.1 service description
data which describes the SPARQL endpoint and the features/data it supports.
Lastly, you can publish an HTML page for your SPARQL endpoint by putting an
index.handlebars
file in your SPARQL endpoint directory. This tutorial site
does exactly that with its own SPARQL endpoint. Index
templates are explained in
the next lesson.
If you want to see a live query in action, you can try it for yourself with
this query to fetch lessons
from this tutorial site's SPARQL endpoint.