lutaml-model 0.8.207 → 0.8.209

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,14 +45,24 @@ module Lutaml
45
45
  # child out again inside an already opted-out subtree. What the
46
46
  # opt-out does change is scope: a child spelled in
47
47
  # default-namespace form inside it re-declares its namespace.
48
+ #
49
+ # `default` and `prefixes` are the bindings in scope for the
50
+ # element's own children (the root's declarations, then whatever
51
+ # a nested element declared on itself): a nested element in a
52
+ # namespace nothing in scope binds declares it where it appears
53
+ # (the interpretive writer's placement for a child whose
54
+ # namespace differs from its parent's), never on the root — only
55
+ # one default namespace can be declared per element (W3C).
48
56
  class BlankScope
49
- attr_reader :root_default, :blank, :pretty
57
+ attr_reader :root_default, :blank, :pretty, :default, :prefixes
50
58
  attr_accessor :done
51
59
 
52
- def initialize(root_default, blank, pretty)
60
+ def initialize(root_default, blank, pretty, default: nil, prefixes: {})
53
61
  @root_default = root_default
54
62
  @blank = blank
55
63
  @pretty = pretty
64
+ @default = default
65
+ @prefixes = prefixes
56
66
  @done = false
57
67
  end
58
68
  end
@@ -125,12 +135,10 @@ module Lutaml
125
135
  end
126
136
  built = if ordered_walk?(plan, instance)
127
137
  build_ordered(root, instance, plan, doc, register, ns_ctx,
128
- scope: BlankScope.new(ns_ctx.default_namespace,
129
- false, pretty))
138
+ scope: ns_ctx.root_scope(pretty))
130
139
  else
131
140
  build(root, instance, plan, doc, register, ns_ctx,
132
- scope: BlankScope.new(ns_ctx.default_namespace,
133
- false, pretty))
141
+ scope: ns_ctx.root_scope(pretty))
134
142
  end
135
143
  return nil unless built
136
144
 
@@ -156,6 +164,7 @@ module Lutaml
156
164
  attr_reader :unspellable
157
165
 
158
166
  def initialize(instance, plan, register)
167
+ @register = register
159
168
  @model_prefix = nil
160
169
  @forms = {}
161
170
  @hoisted = {}
@@ -191,13 +200,31 @@ module Lutaml
191
200
  @hoisted[uri] = prefix
192
201
  @forms[uri] = { prefix: prefix, uri: uri }
193
202
  end
203
+ # The type-analysis plan keys its hoisted set by PREFIX, and
204
+ # its "remaining namespaces" pass hoists a nested model's
205
+ # prefix-less namespace in default form — displacing the
206
+ # root's own default declaration on the one nil key (the
207
+ # MathML island under a default-namespace UnitsML root,
208
+ # #1342). Only one default namespace can be declared per
209
+ # element (W3C): the root keeps its own, and the nested
210
+ # element declares its namespace where it appears
211
+ # (qualify!). The displaced form stays spellable; it is no
212
+ # longer a root declaration. The root's own entry is
213
+ # restored in the form it was displaced from — default.
214
+ model_uri = plan[:tree][:ns] && plan[:tree][:ns][:exact]
215
+ @hoisted.delete_if do |uri, prefix|
216
+ prefix.nil? && uri != model_uri
217
+ end
218
+ if model_uri && !@hoisted.key?(model_uri)
219
+ @hoisted[model_uri] = nil
220
+ @forms[model_uri] = { prefix: nil, uri: model_uri }
221
+ end
194
222
  # The writer's own root decision (its rule engine's
195
223
  # DefaultPreferenceRule over the built tree): an explicitly
196
224
  # :unqualified elementFormDefault never selects the prefix
197
225
  # format by itself — only prefixed attribute needs do (W3C).
198
226
  # The planner's type-analysis path prefers the prefix format
199
227
  # here so children can omit xmlns; the writer does not.
200
- model_uri = plan[:tree][:ns] && plan[:tree][:ns][:exact]
201
228
  ns_class = mapping.namespace_class
202
229
  if ns_class&.element_form_default_set? &&
203
230
  ns_class.element_form_default == :unqualified &&
@@ -257,14 +284,16 @@ module Lutaml
257
284
  # :qualified) take the model prefix instead of falling
258
285
  # into no namespace.
259
286
  ns_class = mapping.namespace_class
260
- if instance.element_order.nil? &&
261
- !instance.import_declaration_plan.is_a?(
262
- Lutaml::Xml::DeclarationPlan,
263
- ) && ns_class &&
287
+ constructed = instance.element_order.nil? &&
288
+ !instance.import_declaration_plan.is_a?(
289
+ Lutaml::Xml::DeclarationPlan,
290
+ )
291
+ if constructed && ns_class &&
264
292
  ns_class.attribute_form_default == :qualified &&
265
293
  !ns_class.prefix_default.to_s.empty?
266
294
  @model_prefix = ns_class.prefix_default.to_s
267
295
  end
296
+ @parsed = !constructed
268
297
  @default_namespace = @hoisted.any? { |_uri, prefix| prefix.nil? }
269
298
  end
270
299
 
@@ -290,6 +319,14 @@ module Lutaml
290
319
  # out with xmlns="".
291
320
  attr_reader :default_namespace
292
321
 
322
+ # The scope the root's children are built under: the root's
323
+ # own declarations are the bindings in scope.
324
+ def root_scope(pretty)
325
+ BlankScope.new(default_namespace, false, pretty,
326
+ default: default_namespace_uri,
327
+ prefixes: hoisted_prefixes)
328
+ end
329
+
293
330
  def collect(tree)
294
331
  check(tree[:ns])
295
332
  (tree[:attributes] || []).each { |a| check(a[:ns]) }
@@ -314,9 +351,81 @@ module Lutaml
314
351
  @hoisted[uri]
315
352
  end
316
353
 
354
+ # The prefix an exact form spells with: the one the root
355
+ # declares the URI under, else the form's own (a nested
356
+ # element's declared prefix, declared on that element).
357
+ def spelled_prefix(form)
358
+ return prefix_for(form) unless form.is_a?(::Hash)
359
+
360
+ uri = form[:exact]
361
+ prefix = if form[:nested]
362
+ # A nested element's decided form: its recorded
363
+ # prefix outranks a root declaration of the same
364
+ # URI (the doubly-defined `<a:leaf>` under
365
+ # `xmlns=A xmlns:a=A`).
366
+ form[:prefix]
367
+ elsif @hoisted.key?(uri)
368
+ @hoisted[uri]
369
+ else
370
+ form[:prefix]
371
+ end
372
+ prefix.to_s.empty? ? nil : prefix
373
+ end
374
+
375
+ # The form a nested element spells through: its plan's tree
376
+ # form carrying the prefix the nested model's own root
377
+ # decision gives its namespace — the instance's recorded
378
+ # prefix (format preservation), else the planner's decision
379
+ # for the nested class, which is the interpretive writer's
380
+ # form for the nested element (a prefix-default island built
381
+ # in memory spells `<leaf xmlns=P>`, not `<p:leaf>`). A URI
382
+ # the root declares spells with the root's form.
383
+ #
384
+ # Returns nil — unspellable, the document takes the
385
+ # interpretive lane — for a PARSED document whose nested
386
+ # class declares a prefix the decision does not use: its
387
+ # source may have spelled the island with that prefix
388
+ # (`<p:leaf xmlns:p=P>`), and the walk capture carries no
389
+ # element prefix to tell; the interpretive writer reads the
390
+ # prefix off the instance and preserves it.
391
+ def nested_form(item, child_plan)
392
+ form = child_plan[:tree][:ns]
393
+ return form unless form.is_a?(::Hash)
394
+ return form if @hoisted.key?(form[:exact])
395
+
396
+ recorded = if item.is_a?(::Lutaml::Model::Serialize)
397
+ item.xml_namespace_prefix
398
+ end
399
+ prefix = if recorded && !recorded.empty?
400
+ recorded
401
+ else
402
+ (@nested_prefixes ||= {}.compare_by_identity)[child_plan] ||=
403
+ root_prefix_for(item.class, child_plan)
404
+ end
405
+ return form if prefix.to_s == form[:prefix].to_s
406
+ return nil if @parsed && !form[:prefix].to_s.empty?
407
+
408
+ { exact: form[:exact], prefix: prefix, nested: true }
409
+ end
410
+
411
+ # The planner's hoisted prefix for a class's own namespace
412
+ # when the class is a document root (nil for the
413
+ # default-namespace form) — its type-analysis decision.
414
+ def root_prefix_for(klass, child_plan)
415
+ mapping = child_plan[:mapping]
416
+ uri = child_plan[:tree][:ns][:exact]
417
+ needs = Lutaml::Xml::NamespaceCollector.new(@register).collect(
418
+ nil, mapping, mapper_class: klass
419
+ )
420
+ declared = Lutaml::Xml::DeclarationPlanner.new(@register).plan(
421
+ klass, mapping, needs, options: {}
422
+ )
423
+ declared.root_node&.hoisted_declarations&.key(uri)
424
+ end
425
+
317
426
  # The wire spelling for a local name under this form: the
318
- # planner's hoisted prefix for the URI, or unprefixed when
319
- # hoisted as the default namespace.
427
+ # declared prefix for the URI, or unprefixed when the URI is
428
+ # in default-namespace form.
320
429
  def spell(local, ns_form = nil)
321
430
  if ns_form.nil?
322
431
  # No row ns: the element belongs to the model's own
@@ -326,19 +435,19 @@ module Lutaml
326
435
  return @model_prefix ? "#{@model_prefix}:#{local}" : local
327
436
  end
328
437
 
329
- uri = if ns_form.is_a?(Hash)
330
- ns_form[:exact]
331
- else
332
- @forms.key?(ns_form) ? @forms[ns_form][:uri] : nil
333
- end
334
- prefix = uri ? @hoisted[uri] : nil
438
+ prefix = if ns_form.is_a?(Hash)
439
+ spelled_prefix(ns_form)
440
+ else
441
+ uri = @forms.key?(ns_form) ? @forms[ns_form][:uri] : nil
442
+ uri ? @hoisted[uri] : nil
443
+ end
335
444
  prefix ? "#{prefix}:#{local}" : local
336
445
  end
337
446
 
338
447
  def form_for(ns_form)
339
448
  return nil if ns_form.nil? || ns_form == :none
340
449
 
341
- ns_form.is_a?(Hash) ? { prefix: @hoisted[ns_form[:exact]], uri: ns_form[:exact] } : @forms[ns_form]
450
+ ns_form.is_a?(Hash) ? { prefix: spelled_prefix(ns_form), uri: ns_form[:exact] } : @forms[ns_form]
342
451
  end
343
452
 
344
453
  # Declaration order matches the interpretive writer: the
@@ -363,6 +472,26 @@ module Lutaml
363
472
  root.add_namespace_definition(prefix, uri)
364
473
  end
365
474
  end
475
+
476
+ private
477
+
478
+ # The URI the root declares as the default namespace, if any.
479
+ def default_namespace_uri
480
+ entry = @hoisted.find { |_uri, prefix| prefix.nil? }
481
+ entry&.first
482
+ end
483
+
484
+ # The prefix bindings the root declares.
485
+ def hoisted_prefixes
486
+ prefixes = {}
487
+ @hoisted.each do |uri, prefix|
488
+ prefixes[prefix] = uri unless prefix.nil?
489
+ end
490
+ @extra_declarations.each do |prefix, uri|
491
+ prefixes[prefix] = uri unless prefix.nil?
492
+ end
493
+ prefixes
494
+ end
366
495
  end
367
496
 
368
497
  # Whether the compiled plan is serialize-shaped. Ordered/mixed
@@ -449,7 +578,12 @@ module Lutaml
449
578
  child_plan = nested_plans[[item.class, name]] ||
450
579
  (nested_plans[[item.class, name]] = PlanCompiler.compile(item.class, reg))
451
580
  row_ns = nested_child_ns(child_plan, ns_index[name],
452
- inherited_ns)
581
+ inherited_ns, plan[:tree][:ns],
582
+ rule)
583
+ row_ns = ns_ctx.nested_form(item, child_plan) if ns_ctx &&
584
+ row_ns.equal?(child_plan[:tree][:ns])
585
+ return nil unless row_ns
586
+
453
587
  form = spelled_form(row_ns, inherited_ns, plan)
454
588
  # The ROW's wire name wins over the child plan's
455
589
  # root — distinct rows (sz, szCs) share one class.
@@ -710,13 +844,21 @@ module Lutaml
710
844
 
711
845
  child_plan = nested_plans_for(plan)[[item.class, object.name]] ||
712
846
  (nested_plans_for(plan)[[item.class, object.name]] = PlanCompiler.compile(item.class, reg))
713
- child_ns = nested_child_ns(child_plan, row_ns, inherited_ns)
847
+ child_ns = nested_child_ns(child_plan, row_ns, inherited_ns,
848
+ plan[:tree][:ns], row_rule)
849
+ own_ns = child_ns.equal?(child_plan[:tree][:ns])
850
+ child_ns = ns_ctx.nested_form(item, child_plan) if ns_ctx && own_ns
851
+ return nil unless child_ns
852
+
714
853
  child = element.create_child(
715
854
  spell(ns_ctx, object.name, child_ns),
716
855
  )
856
+ # A child model's own namespace (reached over the row's
857
+ # bare match form) is not the row's blank spelling.
717
858
  child_scope = subtree_scope(
718
859
  scope,
719
- qualify!(child, child_ns, blank, scope, ns_ctx),
860
+ qualify!(child, child_ns, own_ns ? false : blank,
861
+ scope, ns_ctx),
720
862
  )
721
863
  if ordered_walk?(child_plan, item)
722
864
  return nil unless build_ordered(child, item, child_plan,
@@ -949,12 +1091,18 @@ module Lutaml
949
1091
  nested_plans_for(plan)[[item.class, name]] ||
950
1092
  (nested_plans_for(plan)[[item.class, name]] =
951
1093
  PlanCompiler.compile(item.class, reg))
952
- child_ns = nested_child_ns(child_plan, ns_form, inherited_ns)
1094
+ child_ns = nested_child_ns(child_plan, ns_form, inherited_ns,
1095
+ plan[:tree][:ns], row_rule)
1096
+ own_ns = child_ns.equal?(child_plan[:tree][:ns])
1097
+ child_ns = ns_ctx.nested_form(item, child_plan) if ns_ctx && own_ns
1098
+ return false unless child_ns
1099
+
953
1100
  child = element.create_child(
954
1101
  spell(ns_ctx, name, child_ns),
955
1102
  )
956
1103
  child_scope = subtree_scope(
957
- scope, qualify!(child, child_ns, blank, scope, ns_ctx)
1104
+ scope, qualify!(child, child_ns, own_ns ? false : blank,
1105
+ scope, ns_ctx)
958
1106
  )
959
1107
  if ordered_walk?(child_plan, item)
960
1108
  return nil unless build_ordered(child, item, child_plan,
@@ -1042,11 +1190,25 @@ module Lutaml
1042
1190
  # re-routes to the same attribute on reparse.
1043
1191
  # A nested child's namespace form. A row spelled :unqualified wins
1044
1192
  # over the child plan's own namespace: under a parent whose
1045
- # elementFormDefault is not :qualified, locally declared elements
1046
- # stay bare even when the child model declares the namespace
1047
- # (the interpretive writer's determine_element_namespace scope).
1048
- def nested_child_ns(child_plan, row_ns, inherited_ns)
1049
- return row_ns if %i[unqualified blank].include?(row_ns)
1193
+ # elementFormDefault is not :qualified, locally declared
1194
+ # elements stay bare even when the child model declares the
1195
+ # namespace (the interpretive writer's determine_element_namespace
1196
+ # scope). Except under a NAMESPACE-LESS parent: there the compiler
1197
+ # binds the row :unqualified as the walk's match form only (bare,
1198
+ # or an inherited default xmlns — leptris#1560), and a child model
1199
+ # that declares a namespace still spells and declares it (the
1200
+ # interpretive priority-1 rule: the type's namespace wins). An
1201
+ # explicit `form :unqualified` rule stays bare.
1202
+ def nested_child_ns(child_plan, row_ns, inherited_ns,
1203
+ parent_ns = nil, rule = nil)
1204
+ if %i[unqualified blank].include?(row_ns)
1205
+ if row_ns == :unqualified && parent_ns.nil? &&
1206
+ child_plan[:tree][:ns] && !(rule && rule.form == :unqualified)
1207
+ return child_plan[:tree][:ns]
1208
+ end
1209
+
1210
+ return row_ns
1211
+ end
1050
1212
 
1051
1213
  child_plan[:tree][:ns] || row_ns || inherited_ns
1052
1214
  end
@@ -1085,28 +1247,53 @@ module Lutaml
1085
1247
  # Bare, or spelled through a default-namespace form: the
1086
1248
  # child opts out, on the first such child of each parent
1087
1249
  # under pretty output.
1088
- default_spelled = !form.is_a?(::Hash) || (ns_ctx &&
1089
- ns_ctx.prefix_for(form[:exact]).nil?)
1250
+ default_spelled = !form.is_a?(::Hash) ||
1251
+ (ns_ctx && ns_ctx.spelled_prefix(form).nil?)
1090
1252
  if default_spelled && scope.root_default &&
1091
1253
  !(scope.pretty && scope.done)
1092
1254
  child.set_attribute("xmlns", "")
1093
1255
  scope.done = true
1094
- BlankScope.new(scope.root_default, true, scope.pretty)
1256
+ BlankScope.new(scope.root_default, true, scope.pretty,
1257
+ prefixes: scope.prefixes)
1095
1258
  end
1096
- elsif scope.blank && form.is_a?(::Hash) && ns_ctx &&
1097
- ns_ctx.prefix_for(form[:exact]).nil?
1098
- # A default-namespace child inside an opted-out subtree
1099
- # re-declares its namespace here: the root's declaration
1100
- # no longer reaches it.
1101
- child.set_attribute("xmlns", form[:exact].to_s)
1102
- BlankScope.new(scope.root_default, false, scope.pretty)
1259
+ elsif form.is_a?(::Hash) && ns_ctx
1260
+ declare_on_self(child, form, scope, ns_ctx)
1261
+ end
1262
+ end
1263
+
1264
+ # A child spelled in a namespace nothing in scope binds declares
1265
+ # it on itself (the interpretive writer's placement for a child
1266
+ # whose namespace differs from its parent's — the MathML island
1267
+ # under a default-namespace UnitsML root, #1342): the default
1268
+ # form when the URI is not the default in scope, the prefix form
1269
+ # when the prefix is not bound to it. The scope the child's own
1270
+ # children are built under, nil when nothing changed.
1271
+ def declare_on_self(child, form, scope, ns_ctx)
1272
+ uri = form[:exact].to_s
1273
+ return if uri.empty?
1274
+
1275
+ prefix = ns_ctx.spelled_prefix(form)
1276
+ if prefix.nil?
1277
+ return if scope.default == uri
1278
+
1279
+ child.set_attribute("xmlns", uri)
1280
+ BlankScope.new(scope.root_default || uri, false, scope.pretty,
1281
+ default: uri, prefixes: scope.prefixes)
1282
+ else
1283
+ return if scope.prefixes[prefix] == uri
1284
+
1285
+ child.set_attribute("xmlns:#{prefix}", uri)
1286
+ BlankScope.new(scope.root_default, false, scope.pretty,
1287
+ default: scope.default,
1288
+ prefixes: scope.prefixes.merge(prefix => uri))
1103
1289
  end
1104
1290
  end
1105
1291
 
1106
1292
  # The scope a nested child's subtree is built under.
1107
1293
  def subtree_scope(scope, changed)
1108
1294
  changed || BlankScope.new(scope.root_default, scope.blank,
1109
- scope.pretty)
1295
+ scope.pretty, default: scope.default,
1296
+ prefixes: scope.prefixes)
1110
1297
  end
1111
1298
 
1112
1299
  def add_leaf(element, name, value, doc, attrs: nil, cdata: false,
@@ -12,6 +12,10 @@ module Lutaml
12
12
  # - Used by model's serialization pipeline via Transform.for(:xml)
13
13
  #
14
14
  class ModelTransform < ::Lutaml::Model::Transform
15
+ # The child-name set of a childless element: names nothing, so
16
+ # every named rule is unmatched there (see apply_xml_mapping).
17
+ EMPTY_CHILD_NAMES = Set.new.freeze
18
+
15
19
  # Per (model class, register): compiled records for the mapping
16
20
  # rules — attribute, derived/valid flags, and #765 group-skip —
17
21
  # keyed on the mapping's finalize version so late changes rebuild.
@@ -330,18 +334,42 @@ module Lutaml
330
334
  child_names_set = nil
331
335
  if doc.is_a?(::Lutaml::Xml::XmlElement) || doc.is_a?(::Lutaml::Xml::Document)
332
336
  ec = doc.element_children
333
- unless ec.empty?
334
- child_names_set = Set.new
335
- ec.each do |child|
336
- child_names_set.add(child.namespaced_name)
337
- child_names_set.add(child.unprefixed_name)
338
- # URI-qualified spelling: prefix-independent exact matching
339
- # against the rule's URI-form namespaced name — derived
340
- # models bind per schema unit while instances spell
341
- # arbitrary prefixes (#1212).
342
- if (c_uri = child.namespace_uri)
343
- child_names_set.add("#{c_uri}:#{child.unprefixed_name}")
344
- end
337
+ # A childless element names no child: its named element rules
338
+ # are all unmatched, and replay them like any other element's
339
+ # (an empty set keeps the fast-skip and replay branches open).
340
+ child_names_set = if ec.empty?
341
+ EMPTY_CHILD_NAMES
342
+ else
343
+ Set.new
344
+ end
345
+ ec.each do |child|
346
+ child_names_set.add(child.namespaced_name)
347
+ child_names_set.add(child.unprefixed_name)
348
+ # URI-qualified spelling: prefix-independent exact matching
349
+ # against the rule's URI-form namespaced name — derived
350
+ # models bind per schema unit while instances spell
351
+ # arbitrary prefixes (#1212).
352
+ if (c_uri = child.namespace_uri)
353
+ child_names_set.add("#{c_uri}:#{child.unprefixed_name}")
354
+ end
355
+ end
356
+ end
357
+
358
+ # An attribute rule can only match an attribute whose local name
359
+ # one of its spellings names: the exact-name, URI-resolved and
360
+ # lenient local-name lookups all key on it. Classes declare
361
+ # hundreds of attributes and instances carry a few (the MathML
362
+ # token classes declare ~280 and set one or two), so the
363
+ # unmatched majority is replayed without its lookup machinery
364
+ # (lutaml-model#1333).
365
+ attribute_local_names = nil
366
+ if doc.is_a?(::Lutaml::Xml::XmlElement) || doc.is_a?(::Lutaml::Xml::Document)
367
+ el = doc.is_a?(::Lutaml::Xml::XmlElement) ? doc : doc.root
368
+ if el.is_a?(::Lutaml::Xml::XmlElement)
369
+ attribute_local_names = []
370
+ el.attributes.each_value do |xml_attr|
371
+ attribute_local_names << xml_attr.name
372
+ xml_attr.local_names.each { |n| attribute_local_names << n }
345
373
  end
346
374
  end
347
375
  end
@@ -375,9 +403,9 @@ module Lutaml
375
403
 
376
404
  # #903/#922: replay-or-drop unmatched rules; never clobber
377
405
  # a mid-parse custom-writer assignment.
378
- next if replay_unmatched_rule(child_names_set, record, rule,
379
- instance, defaults_used,
380
- xml_mapping)
406
+ next if replay_unmatched_rule(child_names_set,
407
+ attribute_local_names, record,
408
+ rule, instance, defaults_used)
381
409
 
382
410
  rule_to = rule.to
383
411
 
@@ -957,9 +985,14 @@ _effective_register)
957
985
  Lutaml::Xml::HtmlEntities.decode(value)
958
986
  end
959
987
 
988
+ # unmatched_kind: :attribute / :element for a replayable rule
989
+ # (nil otherwise); unmatched_name: a replayable rule's own wire
990
+ # name, so the per-element presence test needs no block.
960
991
  Record = Struct.new(:attr, :derived, :group_skip, :valid,
961
992
  :unmatched_mode, :replay_writer,
962
- :unmatched_names, :raw_reader)
993
+ :unmatched_names, :raw_reader,
994
+ :unmatched_local, :unmatched_kind,
995
+ :unmatched_name)
963
996
 
964
997
  def compiled_rule_records(model_class, register, grouped_plain_rules)
965
998
  return {}.compare_by_identity unless model_class.include?(Lutaml::Model::Serialize)
@@ -990,10 +1023,14 @@ _effective_register)
990
1023
  # :write_nil (setter write + flag churn) or :drop (nothing).
991
1024
  # Everything else -- defaults, aliases, custom from:,
992
1025
  # delegates, when_attribute dispatch, transformers, value
993
- # maps beyond those two -- keeps its visit.
1026
+ # maps beyond those two -- keeps its visit. So does every rule
1027
+ # the name test cannot classify: content and raw mappings,
1028
+ # name-less rules, the catch-all.
994
1029
  unmatched_mode = nil
995
1030
  replay_writer = nil
996
- if attr && !attr.derived? &&
1031
+ if attr && !attr.derived? && rule.name &&
1032
+ !rule.content_mapping? && !rule.raw_mapping? &&
1033
+ !mapping.any_element_rule.equal?(rule) &&
997
1034
  !rule.render_default && !rule.multiple_mappings? &&
998
1035
  !rule.has_custom_method_for_deserialization? &&
999
1036
  !rule.delegate &&
@@ -1034,6 +1071,15 @@ _effective_register)
1034
1071
  else
1035
1072
  [rule.name.to_s]
1036
1073
  end
1074
+ # An attribute rule's local name — the part every spelling it
1075
+ # resolves to shares, and the key the element's attribute
1076
+ # names are tested against.
1077
+ unmatched_local = nil
1078
+ if rule.attribute? && !rule.multiple_mappings?
1079
+ rule_name = rule.name.to_s
1080
+ colon = rule_name.rindex(":")
1081
+ unmatched_local = colon ? rule_name[(colon + 1)..] : rule_name
1082
+ end
1037
1083
  if unmatched_mode == :write_nil
1038
1084
  replay_writer =
1039
1085
  rule.class.parsed_assign_writer(model_class, rule.to)
@@ -1057,6 +1103,9 @@ _effective_register)
1057
1103
  rule.to.to_s.match?(/\A[a-zA-Z_][a-zA-Z0-9_]*\z/)
1058
1104
  :"__lutaml_raw_#{rule.to}"
1059
1105
  end,
1106
+ unmatched_local,
1107
+ unmatched_mode && rule.attribute? ? :attribute : nil,
1108
+ rule.name.to_s,
1060
1109
  )
1061
1110
  end
1062
1111
  entry = Struct.new(:version, :records).new(version, records)
@@ -1774,20 +1823,32 @@ effective_register = lutaml_register)
1774
1823
  raw.equal?(::Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION)
1775
1824
  end
1776
1825
 
1777
- def replay_unmatched_rule(child_names_set, record, rule, instance,
1778
- defaults_used, xml_mapping)
1779
- return false unless child_names_set && record
1826
+ def replay_unmatched_rule(child_names_set, attribute_local_names,
1827
+ record, rule, instance, defaults_used)
1828
+ return false unless record
1780
1829
 
1781
1830
  mode = record.unmatched_mode
1782
1831
  return false unless mode
1783
- return false if rule.attribute? || rule.content_mapping? ||
1784
- rule.raw_mapping? || !rule.name ||
1785
- xml_mapping.any_element_rule.equal?(rule)
1786
1832
 
1787
- matched = record.unmatched_names.any? do |n|
1788
- child_names_set.include?(n)
1833
+ # Attribute rules name the element's attributes, element rules
1834
+ # its children; an unmatched rule of either kind has the same
1835
+ # two endings (lutaml-model#1333).
1836
+ if record.unmatched_kind
1837
+ return false unless attribute_local_names
1838
+
1839
+ return false if attribute_local_names.include?(record.unmatched_name) ||
1840
+ attribute_local_names.include?(record.unmatched_local)
1841
+ else
1842
+ return false unless child_names_set
1843
+
1844
+ names = record.unmatched_names
1845
+ matched = if names.size == 1
1846
+ child_names_set.include?(names.first)
1847
+ else
1848
+ names.any? { |n| child_names_set.include?(n) }
1849
+ end
1850
+ return false if matched
1789
1851
  end
1790
- return false if matched
1791
1852
 
1792
1853
  return true if mode == :drop
1793
1854
 
@@ -1813,8 +1874,8 @@ effective_register = lutaml_register)
1813
1874
  return true unless raw_initialized?(record, instance)
1814
1875
 
1815
1876
  defaults_used << rule_to
1877
+ # The parsed writer marks the attribute set itself.
1816
1878
  instance.public_send(record.replay_writer, nil)
1817
- instance.value_set_for(rule_to)
1818
1879
  true
1819
1880
  end
1820
1881