+(defun read-item-identities (node item)
+ ;; XTM 4.6: assign locators to the actual canonical item, never intern Topics.
+ ;; ADD-ITEM-IDENTIFIER owns set semantics and cross-item conflict checks.
+ (dolist (child (elements node) item)
+ (when (string= (node-name child) "itemIdentity")
+ (setf item (topic-maps.tmdm:add-item-identifier item (leaf-href child))))))
+(defun read-association (node map)
+ ;; Resolve references in source order, then construct the complete native
+ ;; Association. The constructor may already merge Associations AND Roles.
+ (let ((type nil) (scope nil) (roles nil) (role-nodes nil))
+ (labels ((reference (ref) (find-reference-topic map (reference-iri ref)))
+ (type-reference (node) (reference (first (elements node)))))
+ (dolist (child (elements node))
+ (cond ((string= (node-name child) "type") (setf type (type-reference child)))
+ ((string= (node-name child) "scope") (setf scope (mapcar #'reference (elements child))))
+ ((string= (node-name child) "role")
+ (let ((children (elements child)))
+ (push (list (type-reference (find "type" children :key #'node-name :test #'string=))
+ (reference (find "topicRef" children :key #'node-name :test #'string=))) roles)
+ (push child role-nodes)))))
+ (let ((association (read-item-identities node
+ (topic-maps.tmdm:make-association map type (reverse roles) :scope scope))))
+ ;; Match source metadata to the native Role by its complete signature
+ ;; within THIS canonical Parent. Role order/II are not equality keys.
+ (loop for pair in (reverse roles) for source in (reverse role-nodes) do
+ ;; A preceding Role Reifier may have merged Topics and this Parent.
+ (setf association (topic-maps.tmdm:canonical-association association))
+ (let ((role (find-if (lambda (role)
+ (and (eq (topic-maps.tmdm:association-role-type role)
+ (topic-maps.tmdm:canonical-topic (first pair)))
+ (eq (topic-maps.tmdm:association-role-player role)
+ (topic-maps.tmdm:canonical-topic (second pair)))))
+ (topic-maps.tmdm:association-roles association))))
+ (setf role (read-item-identities source role))
+ (read-item-reifier source (topic-maps.tmdm:canonical-association-role role) map)))
+ ;; Role metadata is now on the native canonical Parent. Reifier merging
+ ;; may normalize it again; return the resulting Association handle.
+ (topic-maps.tmdm:canonical-association
+ (read-item-reifier node association map))))))