SKOS and XKOS¶
Who is this page for?
This page is for teams that publish code lists or statistical classifications as linked data. It is also for teams that want to link their own code lists to a classification that someone else publishes.
SKOS (Simple Knowledge Organization System) is the W3C standard for code lists, thesauri and taxonomies. A code list is a concept scheme, and each of its entries is a concept.
XKOS builds on SKOS for statistical classifications, such as NACE or ISCO. It adds levels, correspondences between two classifications, and the order of versions. The DDI Alliance publishes it.
Built in, unlike SDMX
For SDMX, you build the whole mapping yourself. For SKOS and XKOS, ddigraph does most of it: an RDF export gives each code list, category and classification its SKOS or XKOS type. You write no Cypher for that part. Linking your code lists to a classification that someone else publishes is the part you build, as the last section shows.
Export as SKOS and XKOS¶
Install the rdf extra, then export any DDI file:
pip install "ddigraph[rdf]"
ddigraph export survey.xml --format turtle -o survey.ttl
Every node gets two types. One is its published class, from SKOS, XKOS or
DISCO, which any tool can read. The other is its ddigraph: class, which
keeps the DDI label, so that ddigraph can read the export back. The
RDF backend page covers the rest of the export.
What ddigraph maps¶
Classes¶
| DDI label | Published class |
|---|---|
CodeList, CodeScheme, CategoryScheme, ConceptScheme, StatisticalClassification |
skos:ConceptScheme |
CDICodeList, CDICategorySet, CDIConceptSystem, CDIStatisticalClassification |
skos:ConceptScheme |
Category, Concept, ClassificationItem |
skos:Concept |
CDICategory, CDIConcept, CDIClassificationItem |
skos:Concept |
ClassificationLevel, CategoryGroup, ConceptGroup |
xkos:ClassificationLevel |
ClassificationCorrespondenceTable, CDICorrespondenceTable, CDIConceptSystemCorrespondence |
xkos:Correspondence |
CDIConceptMap |
xkos:ConceptAssociation |
A DDI-L Code has no SKOS class. Its category is the concept: the category
carries the label, and it belongs to the code list's scheme.
Properties¶
| Graph property | Published property |
|---|---|
label |
skos:prefLabel, on a node with a SKOS class |
description |
skos:definition, on a node with a SKOS class |
code |
skos:notation |
rationale |
skos:scopeNote |
On any other node, label is rdfs:label and description is
dcterms:description.
Relationships¶
| Relationship | Published property |
|---|---|
HAS_CATEGORY |
skos:inScheme, from the category to its code list |
HAS_CODE, HAS_CONCEPT, HAS_CLASSIFICATION_ITEM |
skos:inScheme, from the member to its scheme |
IN_SCHEME |
skos:inScheme |
HAS_CONCEPT_MAP |
xkos:madeOf |
HAS_SOURCE |
xkos:sourceConcept, from a concept map |
HAS_TARGET |
xkos:targetConcept, from a concept map |
IS_SUCCESSOR_OF |
xkos:follows |
IS_PREDECESSOR_OF |
xkos:follows, from the other end |
The graph points from a container to its members, because that is how the
XML nests. SKOS puts the link on the member, so skos:inScheme runs the
other way.
DDI 3.2 and 3.3 give each code an ID of its own, so a DDI-L code is a node
between the code list and the category:
CodeList -CONTAINS-> Code -HAS_CATEGORY-> Category. The category is still
skos:inScheme the code list.
DDI-CDI has two kinds of correspondence: a correspondence table and a
concept system correspondence. Both hold their concept maps by
HAS_CONCEPT_MAP, so both are xkos:madeOf them. Many DDI-CDI classes have
a HAS_SOURCE and a HAS_TARGET, but only a concept map's are XKOS terms.
A correspondence table's source and target are levels, not concepts.
Each relationship also keeps its own ddigraph: predicate, in the graph's
direction. A relationship with no row above has only that one.
What is not mapped¶
XKOS has more terms than ddigraph writes. The levels of a classification
keep only their ddigraph: predicates: ddigraph writes no xkos:levels,
xkos:depth or xkos:classifiedUnder.
Query the export with SPARQL¶
Any SKOS tool can read the export. This example lists each code list and its answers, using SKOS terms only:
from ddigraph import iter_graph
from ddigraph.rdf.writer import build_graph
graph = build_graph(iter_graph("tests/fixtures/fragment_instance.xml"))
query = """
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?list ?answer WHERE {
?scheme a skos:ConceptScheme ; skos:prefLabel ?list .
?concept skos:inScheme ?scheme ; skos:prefLabel ?answer .
}
ORDER BY ?list ?answer
"""
for row in graph.query(query):
print(f"{row.list}: {row.answer}")
Age Groups: Under 18
For XKOS, this query lists the pairs of concepts that each correspondence links, using XKOS and SKOS terms only. The file holds one correspondence of each kind:
from ddigraph import iter_graph
from ddigraph.rdf.writer import build_graph
graph = build_graph(iter_graph("tests/fixtures/cdi_correspondence.xml"))
query = """
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX xkos: <http://rdf-vocabulary.ddialliance.org/xkos#>
SELECT ?source ?target WHERE {
?correspondence a xkos:Correspondence ; xkos:madeOf ?map .
?map xkos:sourceConcept/skos:prefLabel ?source ;
xkos:targetConcept/skos:prefLabel ?target .
}
ORDER BY ?source
"""
for row in graph.query(query):
print(f"{row.source} -> {row.target}")
18 to 64 -> 18 to 64 years
Farming -> Agriculture
Under 18 -> 0 to 17
Link to a published classification¶
Statistical offices publish classifications such as ISCO and NACE as SKOS or XKOS. Link your categories to their items, and a tool can see that your answer means the same as their item. SKOS has mapping properties for this:
skos:exactMatch: the two mean the same thing.skos:closeMatch: they are close enough for most uses.skos:broadMatchandskos:narrowMatch: one is wider than the other.
ddigraph writes none of these. As with SDMX, you build them yourself, in RDF or in Neo4j.
In RDF¶
Load the export and the published classification into one graph. Then add a
skos:exactMatch from each category to the item with the same label:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
CONSTRUCT { ?category skos:exactMatch ?item }
WHERE {
?category a skos:Concept ; skos:inScheme ?list ; skos:prefLabel ?label .
?item skos:inScheme <http://example.org/classification/occupations> ;
skos:prefLabel ?itemLabel .
FILTER (lcase(str(?label)) = lcase(str(?itemLabel)))
}
With rdflib, add the result to the graph and save it:
from rdflib import Graph
graph = Graph().parse("survey.ttl").parse("occupations.ttl")
for triple in graph.query(MATCH): # MATCH is the CONSTRUCT query above
graph.add(triple)
graph.serialize("survey-linked.ttl")
Match on skos:notation instead when both sides have codes. A Codebook
category's code is its skos:notation. A DDI-L category has no
skos:notation: the value is on its Code, as ddigraph:value, and the code
points to the category with ddigraph:hasCategory.
In Neo4j¶
A pattern, not a feature
Everything in this part is a pattern you build yourself, with your own
Cypher. That covers the SKOSConceptScheme and SKOSConcept nodes, and
the IN_SKOS_SCHEME, EXACT_MATCH and CLOSE_MATCH relationships.
The labels start with SKOS, so they never mix with the Concept and
ConceptScheme nodes that ddigraph writes, and ddigraph writes none of
these relationship types. The nodes are not DDINodes, so --replace
leaves them alone. Their edges to a DDI node go when a purge deletes
that node.
Load the published classification's items:
// A published classification (you create these)
MERGE (s:SKOSConceptScheme {uri: $scheme_uri})
SET s.pref_label = $scheme_label
WITH s
UNWIND $items AS item
MERGE (c:SKOSConcept {uri: item.uri})
SET c.notation = item.notation, c.pref_label = item.label
MERGE (c)-[:IN_SKOS_SCHEME]->(s)
Link each category of a code list to the item with the same label:
MATCH (:CodeList {urn: $code_list})-[:CONTAINS*0..1]->()-[:HAS_CATEGORY]->(cat:Category)
MATCH (item:SKOSConcept)-[:IN_SKOS_SCHEME]->(:SKOSConceptScheme {uri: $scheme_uri})
WHERE toLower(cat.label) = toLower(item.pref_label)
MERGE (cat)-[m:EXACT_MATCH]->(item)
SET m.method = 'label', m.matched_on = datetime()
RETURN cat.label, item.notation
Then list the categories that still have no match:
MATCH (cl:CodeList)-[:CONTAINS*0..1]->()-[:HAS_CATEGORY]->(cat:Category)
WHERE NOT (cat)-[:EXACT_MATCH|CLOSE_MATCH]->(:SKOSConcept)
RETURN cl.label AS code_list, cat.label AS unmatched
The [:CONTAINS*0..1] step reaches the categories whether or not each code
has an ID of its own
(relationship model).