How to map RDF properties using SPARQL Update
You want to map RDF properties to properties from chosen RDF vocabularies. An inline table is a flexible way to specify mappings in SPARQL Update operations.
Problem
For example, consider you have the following table about Bulgarian local administrative units that you want to convert to RDF:
LAU2_NAT_CODE | NAME_1 | NAME_2_LAT | CHANGE |
---|---|---|---|
00045 | Мокрен | Mokren | 2015-01-01 |
00062 | Аврамово | Avramovo | 2016-01-01 |
62617 | Рибино | Ribino | 2015-01-01 |
You can automatically map this table to RDF using the Tabular component, which derives properties from column names in a given namespace, such as http://example.com/
:
The initial mapping to RDF can be done via this pipeline:
Consequently, you want to map the generated properties to properties from RDF vocabularies.
Solution
Use a SPARQL Update operation that defines the mapping via the VALUES
clause. VALUES
allows you to provide inline tabular data. In this case, you need two columns: one for the property in the source data and one for the property in the target RDF vocabulary:
PREFIX : <http://example.com/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
DELETE {
?s ?source ?o .
}
INSERT {
?s ?target ?o .
}
WHERE {
VALUES (?source ?target) {
(:LAU2_NAT_CODE skos:notation)
(:NAME_1 skos:prefLabel)
(:NAME_2_LAT skos:altLabel)
(:CHANGE dcterms:modified)
}
?s ?source ?o .
}
Each row of the VALUES
clause provides one mapping from a source property to a target property. In the example above, we map to properties from the Simple Knowledge Organization System, such as skos:prefLabel
, and Dublin Core Terms, such as dcterms:modified
. Using the SPARQL Update operation we delete the triples with the source property and insert the triples with the target property. If applied on the input data above, it produces the following result:
This mapping to properties can be executed via this pipeline:
Discussion
The declarative specification of the mappings allows you to extend the columns in the VALUES
clause to provide additional instructions on how to map the data. For example, we can add a column ?typeOrLang
that indicates what datatype or language tag should be applied to the objects of the target properties. If we do not want any processing to be applied to the objects, we can use the keyword UNDEF
instead of an RDF term. Literals with data types may be created via strdt()
function. Likewise, you can make literals with language tags via the strlang()
function.
PREFIX : <http://example.com/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
DELETE {
?s ?source ?_o .
}
INSERT {
?s ?target ?o .
}
WHERE {
VALUES (?source ?target ?typeOrLang) {
(:LAU2_NAT_CODE skos:notation UNDEF)
(:NAME_1 skos:prefLabel "bg")
(:NAME_2_LAT skos:altLabel UNDEF)
(:CHANGE dcterms:modified xsd:date)
}
?s ?source ?_o .
BIND (if(bound(?typeOrLang),
if(isLiteral(?typeOrLang),
strlang(?_o, ?typeOrLang),
strdt(?_o, ?typeOrLang)),
?_o) AS ?o)
}
This update operation produces data in the following shape:
You may also use the SPARQL CONSTRUCT component to map RDF properties. The CONSTRUCT
query form is handy when you want to ignore the data unaffected by the mapping, since CONSTRUCT
queries return only the data that is explicitly created via the CONSTRUCT
clause. In such case, you would remove the DELETE
clause and change the INSERT
to CONSTRUCT
in the update operation above to obtain a mapping CONSTRUCT
query.
See also
Mapping RDF properties via VALUES
is used in the tutorial on converting tabular data to RDF, where you can find more fleshed out examples of using this approach.