Skip to content

Node Identity

Every node ddigraph writes has one identity. It says which DDI object the node stands for. A load uses it to merge a node with the one already stored, and to find the end of each reference. This page explains how a node is named, and what happens when a reference cannot find its end.

The examples run on the test fixtures, from a clone of the repository. Load them first:

ddigraph bootstrap
ddigraph load tests/fixtures/codebook_sample.xml --dataset-id survey_2024
ddigraph load tests/fixtures/codebook_sample.xml --dataset-id survey_2025
ddigraph load tests/fixtures/fragment_instance.xml
ddigraph load tests/fixtures/fragment_instance.xml --dataset-id questionnaire_v2
ddigraph load tests/fixtures/ir_cdi_valid.xml

The Three Kinds of Identity

An identity is one of three kinds. Each one has a text form, which ddigraph stores in the node's identity property.

Kind Text form Used for
DDI URN urn:ddi:{agency}:{id}:{version} Every DDI-L fragment, and a DDI-CDI object with a ddiIdentifier
Scoped urn:ddigraph:scoped:{dataset}:{id} A Codebook element with an ID, a DDI-CDI object with another identifier, and the Dataset node
Derived urn:ddigraph:derived:{parent key}:{role}:{content hash}:{ordinal} A Codebook element without an ID
  • A DDI URN is global. The same agency, ID and version name the same object in every file.
  • A scoped ID is unique only inside one dataset. A Codebook ID attribute is only unique inside its file, so ddigraph adds the dataset to it. The Dataset node is scoped too: its ID is :dataset.
  • A derived identity names an element by its parent node, its role under that parent (such as question), a hash of its own properties, and an ordinal. The ordinal counts only the identical siblings that came before it. So inserting an unrelated element changes no other key.

Each part of a text form is percent-encoded, so two identities never share one. A space becomes %20 and a colon %3A. The dataset survey 2024 and the ID v:1 give urn:ddigraph:scoped:survey%202024:v%3A1.

Count the nodes of each kind in one dataset:

MATCH (n:DDINode)-[:IN_DATASET]->(:Dataset {dataset_id: 'survey_2024'})
RETURN CASE
         WHEN n.identity STARTS WITH 'urn:ddi:' THEN 'urn'
         WHEN n.identity STARTS WITH 'urn:ddigraph:scoped:' THEN 'scoped'
         ELSE 'derived'
       END AS kind, count(*) AS nodes
ORDER BY kind
kind      nodes
derived   19
scoped    32

The 19 derived nodes are elements without an ID, such as the twelve DDIGenericIdentifiable nodes and the three Concept nodes.


The Key

A node's key is a hash of its identity's text form: BLAKE3, 128 bits, written as 32 hex characters. ddigraph bootstrap makes it unique, with one constraint on the DDINode label. Each Dataset node records the hash scheme in key_scheme, which is b3-128/1.

ddigraph.urn computes a key without a database:

from ddigraph import urn

identity = urn.Scoped("survey_2024", "v1")
print(urn.text(identity))
print(urn.key(identity))
urn:ddigraph:scoped:survey_2024:v1
d5e1ad8dd32f5044c0167dec5196f7b2

Look a node up by its key. The constraint's index makes this fast:

MATCH (n:DDINode {key: 'd5e1ad8dd32f5044c0167dec5196f7b2'})
RETURN labels(n) AS labels, n.identity AS identity
labels                   identity
["Variable", "DDINode"]  urn:ddigraph:scoped:survey_2024:v1

The Same ID in Two Datasets

A Codebook ID is scoped to its dataset. So the same file loaded into two datasets gives two sets of nodes, which do not touch:

MATCH (v:Variable {ddi_id: 'v1'})
RETURN v.dataset_id AS dataset, v.identity AS identity, v.key AS key
ORDER BY dataset
dataset      identity                            key
survey_2024  urn:ddigraph:scoped:survey_2024:v1  d5e1ad8dd32f5044c0167dec5196f7b2
survey_2025  urn:ddigraph:scoped:survey_2025:v1  6e3f151b424b71cc05bd4e22b3fc2c76

Two Codebook files often reuse IDs such as v1 for different variables, so this keeps them apart. Reload a file with the same --dataset-id to update its nodes.

Without --replace, a reload only adds and updates. An element with no ID that you edited has a new key, so it becomes a new node beside the old one. A reference list you reordered keeps its old edges too, since an edge's ordinal is part of its identity. So reload an edited file with --replace.

A DDI URN is not scoped. The same DDI-L file loaded into two datasets gives one set of nodes, and each node is in both datasets:

MATCH (c:Category {ddi_id: 'cat1'})-[:IN_DATASET]->(d:Dataset)
RETURN c.identity AS identity, collect(d.dataset_id) AS datasets
identity                   datasets
urn:ddi:test.org:cat1:1.0  ["questionnaire_v2", "fragment_instance"]

A node that datasets of two flavors hold, such as a DDI-L Category that a DDI-CDI file names by its URN, has a type label for each: Category and CDICategory. It has one set of properties, and the last load sets them.


The Readable Properties

The key and identity are for ddigraph. These properties are for you, and ddigraph bootstrap indexes the first, fourth and fifth:

Property What it holds Which nodes have it
ddi_id The ID the file gives: a Codebook ID, a DDI-L ID, a DDI-CDI identifier Every node with an ID; not a derived node
agency The maintaining agency DDI-L nodes, DDI-CDI nodes with a DDI URN, and Codebook elements with an agency attribute
version The version, as the file writes it The same nodes as agency
urn The DDI URN DDI-L nodes and DDI-CDI nodes with a DDI URN; a Codebook element whose file gives one
dataset_id The dataset Codebook nodes, scoped DDI-CDI nodes and Dataset nodes

A node with a DDI URN has no dataset_id, since it can be in many datasets. Follow its IN_DATASET edges instead. A Codebook element's urn, agency and version are what the file says. They do not change its identity, which stays scoped to the dataset:

MATCH (v:Variable {dataset_id: 'survey_2024', ddi_id: 'v1'})
RETURN v.identity AS identity, v.urn AS urn, v.agency AS agency, v.version AS version
identity                            urn         agency  version
urn:ddigraph:scoped:survey_2024:v1  urn:ddi:v1  NSO     1.0

Every node but a stub also has a content_hash, a hash of its properties.


References and Versions

A reference in a file becomes an edge. A DDI-L or DDI-CDI reference names an agency, an ID and a version. --resolve (resolve= in Python) sets how a load finds its end:

--resolve What it does
exact (default) The end must have the version the reference names.
latest-declared The end is the highest version of the same agency and ID that the file declares.
fail The load refuses the file if a reference has no exact end. Nothing is purged or written.

latest-declared orders versions by value, so 1.10 comes after 1.9. It never crosses agencies. It takes the highest version even when the file also declares the one named. Every reference edge records how it was found in its resolution property: exact or latest-declared. Codebook references have no version, so exact and latest-declared read them alike.

A Worked Example

tests/fixtures/ir_cdi_valid.xml declares the category cat-unemployed at versions 1.9 and 1.10 by example.org, and at version 5 by other.org. A code names version 2.0, which no one declares. Another reference names a code the file lacks. Each load below also prints a Pre-ingestion: line and INFO log lines, left out here. With the default, both references end at stubs:

$ ddigraph load tests/fixtures/ir_cdi_valid.xml
Ingestion complete: dataset_id=ir_cdi_valid, nodes_written=15, relationships_written=9, stubs=2, references=9, resolved=7, unresolved=2, conflicts=0

With fail, the load stops before it writes anything, and exits with 1:

$ ddigraph load tests/fixtures/ir_cdi_valid.xml --resolve fail
/home/you/ddigraph/tests/fixtures/ir_cdi_valid.xml:140: <Code_denotes_Category> refers to urn:ddi:example.org:cat-unemployed:2.0, which is not in the file (version_not_in_file)

With latest-declared, the code reaches version 1.10 of example.org. Here --replace deletes the first load's nodes, the stub for version 2.0 included:

$ ddigraph load tests/fixtures/ir_cdi_valid.xml --resolve latest-declared --replace
Ingestion complete: dataset_id=ir_cdi_valid, nodes_written=14, relationships_written=9, stubs=1, references=9, resolved=8, unresolved=1, conflicts=0

See how each code found its category:

MATCH (code:CDICode)-[r:DENOTES]->(cat:CDICategory)
RETURN code.ddi_id AS code, cat.identity AS category, r.resolution AS resolution
ORDER BY code
code    category                                resolution
code-1  urn:ddi:example.org:cat-employed:1      exact
code-2  urn:ddi:example.org:cat-unemployed:1.10 latest-declared

Resolution looks only inside the file. If the end is already stored by an earlier load, the edge still reaches it: the stub has the same key, and it never changes a node that is already there.


Stubs

A reference that has no end in the file ends at a stub. A stub is a node with the labels DDINode and Unresolved, plus the type the reference expects. It has its key, its identity and an unresolved_reason, and no other properties:

Reason Meaning
not_in_file The file declares no object with that ID.
version_not_in_file The file declares the object, but not at that version.

List the stubs:

MATCH (s:Unresolved)
RETURN [l IN labels(s) WHERE NOT l IN ['DDINode', 'Unresolved']] AS type,
       s.identity AS identity, s.unresolved_reason AS reason
type        identity                      reason
["CDICode"] urn:ddi:example.org:code-3:1  not_in_file

A later load of the real node fills the stub in place. It drops the Unresolved label and the reason, sets the node's properties, and keeps its edges. Save this as code-3.xml, a file that declares the missing code:

<Wrapper xmlns="http://ddialliance.org/Specification/DDI-CDI/1.0/XMLSchema/">
  <Code>
    <identifier>
      <ddiIdentifier>
        <dataIdentifier>code-3</dataIdentifier>
        <registrationAuthorityIdentifier>example.org</registrationAuthorityIdentifier>
        <versionIdentifier>1</versionIdentifier>
      </ddiIdentifier>
    </identifier>
  </Code>
</Wrapper>
$ ddigraph load code-3.xml
Ingestion complete: dataset_id=code-3, nodes_written=2, relationships_written=0, stubs=0, references=0, resolved=0, unresolved=0, conflicts=0

The node is real now, it is in both datasets, and the code list still points at it:

MATCH (list:CDICodeList)-[:HAS_CODE]->(code:CDICode {ddi_id: 'code-3'})
MATCH (code)-[:IN_DATASET]->(d:Dataset)
RETURN list.ddi_id AS list, code:Unresolved AS stub, collect(d.dataset_id) AS datasets
list                stub   datasets
labour-force-codes  false  ["code-3", "ir_cdi_valid"]

A reference that reaches a node of a type its relationship cannot end at gives no edge and no stub. The load counts it as unresolved, with the reason wrong_label. In codebook_sample.xml, variable v1 asks a question whose ID an earlier QuestionItem took (see Conflicts), so its ASKED_AS edge would end at a QuestionItem. --resolve fail refuses that file for this reason.

What a Load Counts

ddigraph load prints the counts, and ddigraph.load() returns them in its LoadResult:

  • stubs counts the stubs written. They are part of nodes_written.
  • references counts the references the file holds. It is always resolved plus the unresolved counts.
  • unresolved counts the references with no end, by reason. The text summary prints their total; --json prints each reason.

An RDF file that ddigraph wrote was resolved when it was written, so its load counts only nodes_written, relationships_written and stubs.


Conflicts and Strict Parsing

When two elements of one file claim the same key, the load keeps the first and drops the second, with every node under it. In a Codebook file this happens when two elements share an ID. The kept element is the first in the file, and each dropped one is a conflict.

tests/fixtures/codebook_sample.xml has three. One of them is a <qstn ID="q1"> inside variable v1, which reuses the ID of an earlier <qstnItem ID="q1">. The load warns, and LoadResult.conflicts lists each one with its key, its label and where the kept and dropped elements are:

$ ddigraph load tests/fixtures/codebook_sample.xml --dataset-id survey_2024
2026-11-21 00:57:59,929 WARNING [ddigraph.api] 3 elements of /home/you/ddigraph/tests/fixtures/codebook_sample.xml were dropped: an earlier element had the same key conflicts=3 dataset_id=survey_2024
Ingestion complete: dataset_id=survey_2024, nodes_written=52, relationships_written=20, stubs=0, references=17, resolved=16, unresolved=1, conflicts=3

Only the first q1 became a node:

MATCH (n:DDINode {dataset_id: 'survey_2024', ddi_id: 'q1'})
RETURN labels(n) AS labels, n.question_text AS text
labels                       text
["QuestionItem", "DDINode"]  Grid item detail

--strict-parsing (DDIGRAPH_STRICT_PARSING=true) refuses such a file. The file is read once without writing, so nothing is purged or written, and the load exits with 1:

$ ddigraph load tests/fixtures/codebook_sample.xml --strict-parsing --dataset-id strict_test
/home/you/ddigraph/tests/fixtures/codebook_sample.xml: 3 elements claim the key of an earlier element: QuestionItem a6085af806e54f1473a867b768c9fe75 at line 64 (the one at line 35 is kept); Universe a90841a086eee4c32d84b97669ef8551 at line 65 (the one at line 15 is kept); Category eec0a88f0e36ec21b87e798300cc2dcc at line 67 (the one at line 28 is kept). Strict parsing refuses the file before anything is written; without it, the first element with each key is kept and the others are skipped.

Strict parsing also raises on XML syntax errors instead of reading past them. The message names at most ten conflicts.


Upgrading From 0.5

ddigraph 0.5 named nodes by other properties, and did not scope Codebook IDs to a dataset. A database that 0.5 loaded does not match the 0.6 keys. See Upgrading to 0.6 for the steps, and for each old name with the one that replaces it.