The SPARQL interface is a service which allows queries in data stored in RDF in the SPARQL language. It offers developers/professionals great power and flexibility when creating applications and performing searches.

Stored Information

All of the information that can be searched can be found uploaded to a semantic database. In this type of database the information is stored in graphs.

The datos.gob.es semantic database has two graphs:

How to use it

All queries are sent using GET requests, which return data according to the parameters specified in the reference.

The base address of our SPARQL point is the following:

http://datos.gob.es/virtuoso/sparql

To this address, the parameter "query" must be added, followed by the query you wish to make.

For example, if you want to conduct the following search, select distinct ?type where {?x a ?type} LIMIT 100, which returns the different types uploaded in our SPARQL point:

  1. It should be coded substituting the spaces for “+”, in the following way: select+distinct+?type+where+{?x+a+?type}+LIMIT+100
  2. Once coded, and if you know the address of our SPARQL point, it is run as follows:

    http://datos.gob.es/virtuoso/sparql?query=select+distinct+?type+where+{?x+a+?type}+LIMIT+100

If you don’t add the parameter “query” to theSPARQL point base address in the search you’re conducting, it will give you a 'page not found' error.

By default, the response format is HTML, but another can be specified via the parameter “format”. This parameter may have the following values:

ValueFormat
text/htmlHTML
application/vnd.ms-excelSpreadsheet
text/tab-separated-valuesTSV
application/sparql-results+xmlXML
application/sparql-results+jsonJSON
application/javascriptJavascript
text/turtleTurtle
application/rdf+xmlRDF/XML
text/plainN-Triples
text/csvCSV

If no graph is specified while performing a search, information on all available graphs will be returned.

You can look up all the available graphs using:

select distinct ?uri where { graph ?uri { ?s a ?t } }

If you want to look up which graph the information belongs to, you can specify with the variable ?gto find in which graph each?x can be found:

select distinct ?g ?type where { graph ?g { ?x a ?type. } } limit 100

You can get the results for only one using:

select distinct ?type where { graph <http://datos.gob.es/catalogo> { ?x a ?type. } } limit 100

Now, you only see the ?x that you’ll find in the specified graph

To send these requests you can also use a form. These types of forms offers help to the developer by providing a text area that uses colours to highlight the syntax, identify syntax errors, it has an auto-complete function and it allows the option of conducting searches, among other things.

Examples

To obtain all the classes in the SPARQl point

This query is very useful, because you can see all the types of information that’s stored.

In addition, to block information that’s not useful, you can specify your graphs.

First, specify the graph from the Catalogue:

select distinct ?tipo where
{
graph <http://datos.gob.es/catalogo> {
?x a ?tipo.
}
}

Now use both graphs at the same time thanks to the VALUES

select distinct ?tipo where
{
graph ?grafo {
?x a ?tipo.
}
values ?grafo { <http://datos.gob.es/catalogo> <http://datos.gob.es/nti> }
}

The final result is:

type
http://www.w3.org/ns/dcat#Dataset
http://www.w3.org/2004/02/skos/core#Concept
http://www.w3.org/ns/dcat#Catalog
http://www.w3.org/2006/time#DurationDescription
http://purl.org/dc/terms/Frequency
http://purl.org/dc/terms/IMT
http://purl.org/dc/terms/PeriodOfTime
http://www.w3.org/ns/dcat#Distribution
http://vocab.linkeddata.es/datosabiertos/def/sector-publico/territorio#ComunidadAutonoma
http://vocab.linkeddata.es/datosabiertos/def/sector-publico/territorio#CiudadAutonoma
http://vocab.linkeddata.es/datosabiertos/def/sector-publico/territorio#Pais
http://vocab.linkeddata.es/datosabiertos/def/sector-publico/territorio#Provincia

To obtain all of the Catalogue data sets

Now that you know the types, you're going to ask for all data sets, which correspond to this: http://www.w3.org/ns/dcat#Dataset

select distinct ?dataset where
{
?dataset a <http://www.w3.org/ns/dcat#Dataset>
}

The result is a list of URLs from all of the data sets.

To obtain all the properties the data sets have

If you want to get more information from the data sets, but you only know their URIs, ask for all of their properties:

select distinct ?propiedad where
{
?dataset a <http://www.w3.org/ns/dcat#Dataset> . ?dataset ?propiedad ?valor .
}

These are all the properties they have:

property
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
http://purl.org/dc/terms/modified
http://www.w3.org/ns/dcat#distribution
http://www.w3.org/ns/dcat#keyword
http://www.w3.org/ns/dcat#theme
http://purl.org/dc/terms/accrualPeriodicity
http://purl.org/dc/terms/description
http://purl.org/dc/terms/identifier
http://purl.org/dc/terms/issued
http://purl.org/dc/terms/language
http://purl.org/dc/terms/publisher
http://purl.org/dc/terms/spatial
http://purl.org/dc/terms/title
http://purl.org/dc/terms/references
http://purl.org/dc/terms/temporal
http://purl.org/dc/terms/conformsTo
http://purl.org/dc/terms/valid

To obtain all entities that publish data

Use the property http://purl.org/dc/terms/publisher to get all the entities that publish data.

select distinct ?publicador where
{
?x a <http://www.w3.org/ns/dcat#Dataset> .
?x <http://purl.org/dc/terms/publisher> ?publicador.
}

The result is a set of URLs of all publishers.

publisher
http://datos.gob.es/recurso/sector-publico/org/Organismo/A02002834
http://datos.gob.es/recurso/sector-publico/org/Organismo/A04003003
http://datos.gob.es/recurso/sector-publico/org/Organismo/A05003638

To obtain the properties of the entities that publish data

With the URIs you don't know the names of the entities, so you're going to ask for the properties of these URIs

select distinct ?propiedad where
{
?x a <http://www.w3.org/ns/dcat#Dataset> .
?x <http://purl.org/dc/terms/publisher> ?publicador.
?publicador ?propiedad ?valor.
}

These are all of the publisher properties:

property
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
http://www.w3.org/2004/02/skos/core#prefLabel
http://www.w3.org/2004/02/skos/core#notation

To obtain the names of the entities that publish data

Ask for the URI and the property http://www.w3.org/2004/02/skos/core#prefLabel

select distinct ?publicador ?label where
{
?x a <http://www.w3.org/ns/dcat#Dataset> .
?x <http://purl.org/dc/terms/publisher>
?publicador. ?publicador <http://www.w3.org/2004/02/skos/core#prefLabel> ?label.
}

The result is a very long list:

publisherlabel
http://datos.gob.es/recurso/sector-publico/org/Organismo/A02002834"Gobierno de Aragón"
http://datos.gob.es/recurso/sector-publico/org/Organismo/A04003003"Gobierno de las Islas Baleares"
http://datos.gob.es/recurso/sector-publico/org/Organismo/A05003638"Comunidad Autónoma de Canarias"
http://datos.gob.es/recurso/sector-publico/org/Organismo/A07002862"Junta de Castilla y León"

To obtain the names of the ten entities that have the most published data sets and view how many there are

To conduct this search, you have to group the results, put them in order and limit the total to 10.

select distinct ?label count(?x) as ?num {
?x a <http://www.w3.org/ns/dcat#Dataset> .
?x <http://purl.org/dc/terms/publisher> ?publicador.
?publicador <http://www.w3.org/2004/02/skos/core#prefLabel> ?label.
}
group by (?label)
order by desc(?num)
limit 10

The result is:

labelnum
"Gobierno de Aragón"2659
"Comunidad Autónoma de País Vasco"2208
"Centro de Investigaciones Sociológicas"2107
"Ayuntamiento de Málaga"651
"Ayuntamiento de Gijón"627
"Xunta de Galicia"315
"Generalitat Valenciana"313
"Ayuntamiento de Madrid"231
"Instituto Nacional de Estadística"205
"Junta de Castilla y León"196