lutaml-model 0.8.206 → 0.8.208

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eab7d971a6cba2786d724c87b9cf76e6bd5dcfc8b18291748ea9c14d1f0d6e90
4
- data.tar.gz: 32ca845f618f08d9ae94278188ccad27fa4aec91fc5c00abe48876b552589425
3
+ metadata.gz: ad86126c9e0284053e6ff23df2d047c5c658d679e8c9c7712cc722fd7aa7c46b
4
+ data.tar.gz: 7b03f53eaa1ea056110bffe25f9db569397819bb835be2c10964de9359571ce2
5
5
  SHA512:
6
- metadata.gz: 928e1d25192b585379158191088e6e198694998141a614b6852e741449e990f95512158852b9ca28b1431a5a7bbcc5e7fd4db95cff873bb0f511a44681125132
7
- data.tar.gz: a451b8ae1a886fe2a3f859de5c13d92414613e58e161acad16a8658e165901eee8fd72e554c60739085189eb3ae4818b02faa01f9e1066f43366b18ff14b9410
6
+ metadata.gz: dd2ad49d9eb69cb840e7e99c88519665214e015e6fbc299bc1c9644eb52c2ea96b28be3372c3eeacc62c0c29e5dbd50807bb641ef6ec661f3ab0e33fd15e5f91
7
+ data.tar.gz: fdb971b77d5d19c7436491538114e2b5a14a930fe070ae59a25778311e8dd3a007eac2ad4d7f663c365b740963d1ed9f7b1a89f7d8de18e7284b700fff5a43a4
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "complex1-46KB": {
3
- "avg_time": 0.035969980333324734,
4
- "min_time": 0.03559261399999514,
5
- "max_time": 0.036185082999992346,
6
- "allocations": 50994,
7
- "ips": 27.800960432373113
3
+ "avg_time": 0.04722797766667478,
4
+ "min_time": 0.04673070100000132,
5
+ "max_time": 0.04775104800000918,
6
+ "allocations": 48293,
7
+ "ips": 21.17388991452887
8
8
  },
9
9
  "complex4-50KB": {
10
- "avg_time": 0.04991650399999988,
11
- "min_time": 0.04966256299999827,
12
- "max_time": 0.050061231000000816,
13
- "allocations": 71837,
14
- "ips": 20.03345426594784
10
+ "avg_time": 0.06411800366667346,
11
+ "min_time": 0.06375282199996946,
12
+ "max_time": 0.06461941400004889,
13
+ "allocations": 67871,
14
+ "ips": 15.596243532450604
15
15
  },
16
16
  "size1000-245KB": {
17
- "avg_time": 0.28591416999999336,
18
- "min_time": 0.2818747909999786,
19
- "max_time": 0.2920046950000028,
20
- "allocations": 406438,
21
- "ips": 3.4975531293185758
17
+ "avg_time": 0.36015998900001023,
18
+ "min_time": 0.35448734000004833,
19
+ "max_time": 0.36499723800000083,
20
+ "allocations": 382355,
21
+ "ips": 2.776543843130703
22
22
  },
23
23
  "complex3-567KB": {
24
- "avg_time": 0.40071153599999815,
25
- "min_time": 0.3966769870000064,
26
- "max_time": 0.4045028329999809,
27
- "allocations": 584888,
28
- "ips": 2.49556079663253
24
+ "avg_time": 0.5304185873333344,
25
+ "min_time": 0.5280918569999926,
26
+ "max_time": 0.5321308450000402,
27
+ "allocations": 560145,
28
+ "ips": 1.8853034638689303
29
29
  }
30
30
  }
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "metrology-4KB": {
3
- "avg_time": 0.008145603666662282,
4
- "min_time": 0.007795773999987432,
5
- "max_time": 0.008779492999906324,
6
- "allocations": 7544,
7
- "ips": 122.76560963710097
3
+ "avg_time": 0.013593411333332028,
4
+ "min_time": 0.013364400000000387,
5
+ "max_time": 0.013884672999999736,
6
+ "allocations": 6555,
7
+ "ips": 73.56505114708973
8
8
  },
9
9
  "bmj-49KB": {
10
- "avg_time": 0.0723875503333223,
11
- "min_time": 0.07129628399991361,
12
- "max_time": 0.07395147099998667,
13
- "allocations": 72378,
14
- "ips": 13.814530197462258
10
+ "avg_time": 0.11374726233333377,
11
+ "min_time": 0.11275422599999985,
12
+ "max_time": 0.11427680500000292,
13
+ "allocations": 61623,
14
+ "ips": 8.791420377833118
15
15
  },
16
16
  "pnas-152KB": {
17
- "avg_time": 0.34388590966667226,
18
- "min_time": 0.3415666549999514,
19
- "max_time": 0.34598907000008694,
20
- "allocations": 259437,
21
- "ips": 2.9079411859860658
17
+ "avg_time": 0.49736028199999777,
18
+ "min_time": 0.4924356920000008,
19
+ "max_time": 0.5007860399999942,
20
+ "allocations": 232230,
21
+ "ips": 2.0106149127525317
22
22
  }
23
23
  }
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "feature-doc-23KB": {
3
- "avg_time": 0.05333307866665867,
4
- "min_time": 0.05320582799998874,
5
- "max_time": 0.05343820699999924,
6
- "allocations": 33278,
7
- "ips": 18.750089531680324
3
+ "avg_time": 0.08004215133333521,
4
+ "min_time": 0.07967459299999291,
5
+ "max_time": 0.08053014500001154,
6
+ "allocations": 29557,
7
+ "ips": 12.49341732252428
8
8
  },
9
9
  "tbx-nisosts-101KB": {
10
- "avg_time": 0.1555278476666615,
11
- "min_time": 0.15451645799998914,
12
- "max_time": 0.15693985299998303,
13
- "allocations": 134268,
14
- "ips": 6.429716703488832
10
+ "avg_time": 0.24897703766667215,
11
+ "min_time": 0.24684493000000884,
12
+ "max_time": 0.2523852099999999,
13
+ "allocations": 116651,
14
+ "ips": 4.016434645426176
15
15
  },
16
16
  "iso-13849-1MB": {
17
- "avg_time": 5.377177922666665,
18
- "min_time": 5.361418570000012,
19
- "max_time": 5.4017619889999935,
20
- "allocations": 1735227,
21
- "ips": 0.1859711570607798
17
+ "avg_time": 7.87387151599999,
18
+ "min_time": 7.842370211000002,
19
+ "max_time": 7.923985744999982,
20
+ "allocations": 1679721,
21
+ "ips": 0.12700232636104922
22
22
  },
23
23
  "din-iso-1.1MB": {
24
- "avg_time": 0.5874012496666694,
25
- "min_time": 0.5822755989999848,
26
- "max_time": 0.5889298150000002,
27
- "allocations": 877606,
28
- "ips": 1.7024138109468896
24
+ "avg_time": 0.9811890356666595,
25
+ "min_time": 0.9675218900000004,
26
+ "max_time": 0.9853540780000003,
27
+ "allocations": 846635,
28
+ "ips": 1.0191716006289855
29
29
  }
30
30
  }
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "800-expressions": {
3
- "avg_time": 0.544940995666669,
4
- "min_time": 0.5414146770000059,
5
- "max_time": 0.5454735960000079,
6
- "allocations": 138102,
7
- "ips": 1.835061057897877
3
+ "avg_time": 0.1067107063333405,
4
+ "min_time": 0.10537397500002044,
5
+ "max_time": 0.10742932200000155,
6
+ "allocations": 139002,
7
+ "ips": 9.371130923604072
8
8
  }
9
9
  }
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "ea251": {
3
- "avg_time": 0.17969961900000916,
4
- "min_time": 0.17811001200001897,
5
- "max_time": 0.18114700399999606,
6
- "allocations": 182292,
7
- "ips": 5.564842071256417
3
+ "avg_time": 0.18316421099999994,
4
+ "min_time": 0.18038664600001653,
5
+ "max_time": 0.1853450769999938,
6
+ "allocations": 179861,
7
+ "ips": 5.459581839380185
8
8
  },
9
9
  "medium": {
10
- "avg_time": 0.44894484466666046,
11
- "min_time": 0.43719278499997927,
12
- "max_time": 0.45557199699999273,
13
- "allocations": 471852,
14
- "ips": 2.2274451124224304
10
+ "avg_time": 0.4485483150000012,
11
+ "min_time": 0.44155083600000467,
12
+ "max_time": 0.4588752709999824,
13
+ "allocations": 465885,
14
+ "ips": 2.229414238240974
15
15
  },
16
16
  "citygml": {
17
- "avg_time": 0.506995725666665,
18
- "min_time": 0.503759285000001,
19
- "max_time": 0.5088972249999983,
20
- "allocations": 583260,
21
- "ips": 1.972403216388201
17
+ "avg_time": 0.5182148209999866,
18
+ "min_time": 0.5057585739999979,
19
+ "max_time": 0.5237662360000002,
20
+ "allocations": 577293,
21
+ "ips": 1.929701659381961
22
22
  },
23
23
  "large": {
24
- "avg_time": 4.185453844333334,
25
- "min_time": 4.1155831300000045,
26
- "max_time": 4.230669285999994,
27
- "allocations": 4506877,
28
- "ips": 0.23892271595681203
24
+ "avg_time": 4.232644015666655,
25
+ "min_time": 4.1436827659999835,
26
+ "max_time": 4.302634627999993,
27
+ "allocations": 4449467,
28
+ "ips": 0.23625894270782344
29
29
  }
30
30
  }
@@ -92,6 +92,22 @@ the mapped one — and one stays interpretive for an engine reason: an
92
92
  `xsi:nil` mark on a nested element (below the root's own children),
93
93
  which the engine walk does not surface.
94
94
 
95
+ Round trips keep what the parsed root declared. Every namespace the
96
+ source declared on the root is declared again — an extra vocabulary the
97
+ document carried, the prefix a captured raw fragment spells, or a
98
+ second prefix a default-namespace root bound its own namespace to —
99
+ exactly as the standard pipeline keeps them. A `render_nil` element
100
+ spells `xsi:nil`, and a constructed document declares the `xsi` prefix
101
+ for it on the root (a parsed one already carries the declaration).
102
+
103
+ Parsing strictness is a property of the lane unless you ask for
104
+ otherwise: the leptris plan lane parses strictly (malformed input
105
+ raises `InvalidFormatError`), while the standard pipeline recovers
106
+ whatever its engine recovers — Nokogiri recovers most malformed input
107
+ into a best-effort tree. `from_xml(xml, strict: true)` asks for the
108
+ raising contract on every lane; the `lutaml-model compare` command
109
+ always parses strictly.
110
+
95
111
  Version floor note: leptris 1.9.320+ retains child-row namespace
96
112
  URIs engine-side and matches plain attribute rows against
97
113
  namespace-qualified wire spellings. On older engines, models whose
@@ -233,6 +233,10 @@ Error parsing file data.xml: Invalid XML syntax
233
233
  ----
234
234
  *Solution:* Verify file format and syntax. The CLI will attempt to parse files according to their extension.
235
235
 
236
+ XML files are parsed strictly: a malformed document is reported as above
237
+ rather than compared as a recovered tree, whatever the engine would have
238
+ recovered from it.
239
+
236
240
  == Supported File Formats
237
241
 
238
242
  The compare command automatically detects file format based on extension:
@@ -70,7 +70,13 @@ module Lutaml
70
70
  format = path.split(".").last.downcase
71
71
  format = "yaml" if format == "yml"
72
72
 
73
- model.public_send("from_#{format}", content)
73
+ if format == "xml"
74
+ # A malformed document is reported, never compared as a
75
+ # recovered tree — whatever the engine would recover.
76
+ model.from_xml(content, strict: true)
77
+ else
78
+ model.public_send("from_#{format}", content)
79
+ end
74
80
  rescue StandardError => e
75
81
  raise StandardError, "Error parsing file #{path}: #{e.message}"
76
82
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.206"
5
+ VERSION = "0.8.208"
6
6
  end
7
7
  end
@@ -294,6 +294,7 @@ module Lutaml
294
294
  # Check if element was created from nil value with render_nil option
295
295
  if element.is_a?(Lutaml::Xml::DataModel::XmlElement) && element.xsi_nil
296
296
  attributes["xsi:nil"] = true
297
+ declare_xsi_prefix(xml)
297
298
  end
298
299
 
299
300
  # Create element
@@ -472,6 +473,21 @@ module Lutaml
472
473
 
473
474
  private
474
475
 
476
+ # xsi:nil spells the xsi prefix: the document root declares it,
477
+ # as it does for xsi:schemaLocation (a parsed source's own
478
+ # declaration is kept by the stored plan, so this adds nothing
479
+ # on a round trip).
480
+ def declare_xsi_prefix(xml)
481
+ root = xml.doc.root
482
+ return unless root
483
+ return if root.in_scope_namespaces.any? do |ns|
484
+ ns.prefix == W3c::XsiNamespace.prefix_default
485
+ end
486
+
487
+ root.add_namespace(W3c::XsiNamespace.prefix_default,
488
+ W3c::XsiNamespace.uri)
489
+ end
490
+
475
491
  def build_type_only_element(xml, element, xml_mapping, plan, options,
476
492
  mapper_class)
477
493
  if options[:tag_name]
@@ -17,7 +17,8 @@ module Lutaml
17
17
  parse_encoding = encoding(xml, options)
18
18
  raw_xml = xml
19
19
  xml = normalize_xml_for_parse(xml)
20
- parsed = parse_with_moxml(xml, parse_encoding)
20
+ parsed = parse_with_moxml(xml, parse_encoding,
21
+ strict: options[:strict])
21
22
  root_element = parsed.root
22
23
 
23
24
  raise_empty_document_error(parsed.parse_errors) if root_element.nil?
@@ -46,15 +47,19 @@ module Lutaml
46
47
  replace: "?")
47
48
  end
48
49
 
49
- def parse_with_moxml(xml, parse_encoding)
50
+ # strict: the engine raises on malformed input instead of
51
+ # recovering a best-effort tree (what a `from_xml(strict: true)`
52
+ # caller asks for; the leptris plan lane parses strictly anyway).
53
+ def parse_with_moxml(xml, parse_encoding, strict: nil)
50
54
  parse_error_class = parse_error_class_or_nil
55
+ moxml_options = { encoding: parse_encoding }
56
+ moxml_options[:strict] = true if strict
51
57
  unless parse_error_class
52
- return moxml_adapter_class.parse(xml,
53
- encoding: parse_encoding)
58
+ return moxml_adapter_class.parse(xml, **moxml_options)
54
59
  end
55
60
 
56
61
  begin
57
- moxml_adapter_class.parse(xml, encoding: parse_encoding)
62
+ moxml_adapter_class.parse(xml, **moxml_options)
58
63
  rescue parse_error_class => e
59
64
  raise Lutaml::Model::InvalidFormatError.new(:xml, e.message)
60
65
  end
@@ -264,6 +264,7 @@ module Lutaml
264
264
 
265
265
  if xml_element.is_a?(Lutaml::Xml::DataModel::XmlElement) && xml_element.xsi_nil
266
266
  attributes["xsi:nil"] = "true"
267
+ declare_xsi_prefix(xml)
267
268
  end
268
269
 
269
270
  attributes.merge!(element_node.schema_location_attr) if element_node.schema_location_attr
@@ -60,6 +60,11 @@ module Lutaml
60
60
  # A nil instance runs the static checks only.
61
61
  def compatible?(plan, instance = nil, register = nil)
62
62
  return false if plan.nil?
63
+ # Source declarations the ops layout cannot spell (an extra
64
+ # vocabulary the parsed root declared, a second prefix for a
65
+ # declared namespace): the DOM lane re-declares them.
66
+ return false if instance &&
67
+ PlanSerializer.stored_declarations_uncovered?(plan, instance)
63
68
  # A tracked element_order on an unordered plan (#977) is the
64
69
  # DOM lane's ordered walk; the flat ops layout emits mapping
65
70
  # order.
@@ -19,8 +19,40 @@ module Lutaml
19
19
  # child_row_plans).
20
20
  CHILD_ROW_KINDS = %i[nested ordered_deferred].freeze
21
21
  XSI_URI = "http://www.w3.org/2001/XMLSchema-instance"
22
+ # Fiber-local bridge context and its nesting depth (see call).
23
+ BRIDGE_CONTEXT_KEY = :lutaml_plan_hydrator_bridge_context
24
+ BRIDGE_DEPTH_KEY = :lutaml_plan_hydrator_bridge_depth
22
25
 
23
26
  class << self
27
+ # Wrappers for deferred islands bridge leptris nodes into one
28
+ # moxml context per top-level deserialization, never for the
29
+ # process: on Ruby >= 4.0 (and < 3.4) moxml's wrapper registry
30
+ # is a strong Hash (moxml#303), and a context kept any longer
31
+ # pins every bridged document's whole tree for the life of the
32
+ # process — one registered wrapper references its native, the
33
+ # native its document, the document every node
34
+ # (lutaml-model#1335). Wrappers a model keeps stay alive
35
+ # through the model's own references; the registry's go when
36
+ # the deserialization returns.
37
+ def call(model_class, plan, value, **kwargs)
38
+ depth = (Thread.current[BRIDGE_DEPTH_KEY] || 0) + 1
39
+ Thread.current[BRIDGE_DEPTH_KEY] = depth
40
+ begin
41
+ hydrate(model_class, plan, value, **kwargs)
42
+ ensure
43
+ release_bridge_context(depth)
44
+ end
45
+ end
46
+
47
+ def release_bridge_context(depth)
48
+ if depth > 1
49
+ Thread.current[BRIDGE_DEPTH_KEY] = depth - 1
50
+ else
51
+ Thread.current[BRIDGE_DEPTH_KEY] = nil
52
+ Thread.current[BRIDGE_CONTEXT_KEY] = nil
53
+ end
54
+ end
55
+
24
56
  # plan: the compiled plan entry for model_class
25
57
  # value: the walk root PlanValue (element)
26
58
  # node: the parsed source element (leptris); ordered/mixed
@@ -31,8 +63,8 @@ module Lutaml
31
63
  # :lazy is honored as :eager — the lazy variant pins the
32
64
  # raw engine DOM, the retention the eager default exists
33
65
  # to avoid)
34
- def call(model_class, plan, value, parent: nil, node: nil,
35
- register: nil, declaration_plan_mode: :eager, intern: nil)
66
+ def hydrate(model_class, plan, value, parent: nil, node: nil,
67
+ register: nil, declaration_plan_mode: :eager, intern: nil)
36
68
  # The child model's declared lutaml_default_register takes
37
69
  # precedence over the ambient register — the same
38
70
  # resolve_for_child contract the interpretive path applies.
@@ -108,6 +140,8 @@ module Lutaml
108
140
  instance
109
141
  end
110
142
 
143
+ private :hydrate, :release_bridge_context
144
+
111
145
  # leptris#1874: snapshot rows -> the ns_data hash
112
146
  # ({path => {prefix_key => {uri:, prefix:, format:}}}), the
113
147
  # exact shape the recursion builds. Public: it is the
@@ -666,9 +700,12 @@ module Lutaml
666
700
  # reader's shape: the text runs when the element has
667
701
  # non-text children (validate flags a singular target
668
702
  # holding them), the joined text otherwise.
703
+ # Several runs mean non-text nodes between them (text
704
+ # nodes only split at a child boundary).
669
705
  instance.public_send(:"#{attr.name}=",
670
706
  content_text(node, runs,
671
- element_children))
707
+ element_children ||
708
+ runs.size > 1))
672
709
  when :raw
673
710
  raws = raw_strings(value, wire_name)
674
711
  next if raws.empty?
@@ -801,7 +838,7 @@ module Lutaml
801
838
  end
802
839
 
803
840
  def bridge_context
804
- @bridge_context ||= Moxml::Context.new(:leptris)
841
+ Thread.current[BRIDGE_CONTEXT_KEY] ||= Moxml::Context.new(:leptris)
805
842
  end
806
843
 
807
844
  # Rows carry the delegate NAME (the interpretive
@@ -848,11 +885,11 @@ module Lutaml
848
885
  # The interpretive XmlElement#text shape for a singular
849
886
  # content attribute: the text runs when the source element has
850
887
  # non-text children (mixed content), the joined text otherwise.
851
- # Mapped element children show in the walk's own capture; an
852
- # unmapped one only on the source node, consulted when the
853
- # hydration already holds it (the buckets chain is not kept
854
- # for this — 85 singular content attributes ride every NISO
855
- # JATS paragraph).
888
+ # Mapped element children and child-split runs show in the
889
+ # walk's own capture; a single run beside an unmapped element
890
+ # only on the source node, consulted when the hydration already
891
+ # holds it (the buckets chain is not kept for this — 85
892
+ # singular content attributes ride every NISO JATS paragraph).
856
893
  def content_text(node, runs, element_children)
857
894
  return runs if element_children
858
895
  return runs.join unless node
@@ -119,9 +119,7 @@ module Lutaml
119
119
  if instance.is_a?(::Lutaml::Model::Serialize) &&
120
120
  instance.raw_schema_location &&
121
121
  !instance.raw_schema_location.empty?
122
- root.set_attribute(
123
- "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
124
- )
122
+ ns_ctx.declare_xsi_on(root)
125
123
  root.set_attribute("xsi:schemaLocation",
126
124
  instance.raw_schema_location)
127
125
  end
@@ -161,6 +159,7 @@ module Lutaml
161
159
  @model_prefix = nil
162
160
  @forms = {}
163
161
  @hoisted = {}
162
+ @extra_declarations = []
164
163
  @declared = {}
165
164
  @unspellable = false
166
165
  mapping = plan[:mapping]
@@ -219,25 +218,33 @@ module Lutaml
219
218
  @hoisted[model_uri] = ns_prefix
220
219
  @forms[model_uri] = { prefix: ns_prefix, uri: model_uri }
221
220
  end
222
- # Source declaration preservation (the interpretive
223
- # writer's stored-plan merge): prefixes the parsed source
224
- # declared are kept for URIs the document emits. The
225
- # planner's class-analysis path has no stored-plan
226
- # access, so the merge reads the instance's recorded plan
227
- # directly.
221
+ # Source declaration preservation (the interpretive writer's
222
+ # stored-plan behavior): every namespace the parsed source
223
+ # declared on the root is declared again. The ones the
224
+ # planner recomputed keep the planner's spelling (format
225
+ # preservation); the rest ride along verbatim — an extra
226
+ # vocabulary the root declared, or the prefix a captured
227
+ # raw fragment spells. A default-namespace source that also
228
+ # bound a declared URI to a prefix keeps that second
229
+ # declaration (the doubly-defined root); a prefixed source
230
+ # does not (the interpretive merge skips it).
228
231
  stored = if instance.is_a?(::Lutaml::Model::Serialize)
229
232
  instance.import_declaration_plan
230
233
  end
231
234
  if stored.is_a?(Lutaml::Xml::DeclarationPlan) &&
232
235
  stored.root_node.is_a?(
233
236
  Lutaml::Xml::DeclarationPlan::ElementNode,
234
- ) && (ns_prefix.nil? || ns_prefix.empty?)
235
- original_uris = stored.original_namespace_uris || {}
237
+ )
238
+ prefixed_source = ns_prefix && !ns_prefix.empty?
236
239
  stored.root_node.hoisted_declarations.each do |prefix, uri|
237
- effective_uri = original_uris[uri] || uri
238
- already = @hoisted.value?(effective_uri) ||
239
- @hoisted.value?(uri)
240
- @hoisted[prefix] = uri if already && !@hoisted.key?(prefix)
240
+ if @hoisted.key?(uri)
241
+ next if prefixed_source || prefix.nil? || @hoisted[uri] == prefix
242
+
243
+ @extra_declarations << [prefix, uri]
244
+ else
245
+ @hoisted[uri] = prefix
246
+ @forms[uri] = { prefix: prefix, uri: uri }
247
+ end
241
248
  end
242
249
  end
243
250
  collect(plan[:tree])
@@ -265,6 +272,19 @@ module Lutaml
265
272
  @unspellable
266
273
  end
267
274
 
275
+ # The xsi prefix for a raw xsi:schemaLocation — declared once
276
+ # with the plan's other declarations (a parsed source's own
277
+ # declaration is kept above).
278
+ def declare_xsi_on(root)
279
+ uri = Lutaml::Xml::W3c::XsiNamespace.uri
280
+ return if @declared[uri]
281
+
282
+ root.add_namespace_definition(
283
+ Lutaml::Xml::W3c::XsiNamespace.prefix_default, uri
284
+ )
285
+ @declared[uri] = true
286
+ end
287
+
268
288
  # Whether the root declares a default namespace — every
269
289
  # element then has one in scope until a :blank child opts
270
290
  # out with xmlns="".
@@ -335,6 +355,13 @@ module Lutaml
335
355
  @declared[uri] = true
336
356
  root.add_namespace_definition(prefix, uri)
337
357
  end
358
+ @extra_declarations.each do |prefix, uri|
359
+ next if uri.nil? || uri == XML_NAMESPACE_URI ||
360
+ @declared[[prefix, uri]]
361
+
362
+ @declared[[prefix, uri]] = true
363
+ root.add_namespace_definition(prefix, uri)
364
+ end
338
365
  end
339
366
  end
340
367
 
@@ -765,6 +792,44 @@ module Lutaml
765
792
  RECONCILER.order_entry?(object)
766
793
  end
767
794
 
795
+ # Whether the parsed source declared anything the plan's own
796
+ # namespaces do not cover — an extra vocabulary on the root, or
797
+ # a second prefix for a declared namespace. The ops layout
798
+ # cannot spell those; the DOM lane re-declares them (the
799
+ # interpretive writer's stored-plan behavior).
800
+ def stored_declarations_uncovered?(plan, instance)
801
+ return false unless instance.is_a?(::Lutaml::Model::Serialize)
802
+
803
+ stored = instance.import_declaration_plan
804
+ return false unless stored.is_a?(Lutaml::Xml::DeclarationPlan) &&
805
+ stored.root_node.is_a?(Lutaml::Xml::DeclarationPlan::ElementNode)
806
+
807
+ declared = stored.root_node.hoisted_declarations
808
+ return false if declared.empty?
809
+
810
+ uris = declared_uris(plan)
811
+ declared.values.uniq.size != declared.size ||
812
+ declared.values.any? { |uri| !uris.include?(uri) }
813
+ end
814
+
815
+ # The namespace URIs the plan's emission declares on its root.
816
+ def declared_uris(plan)
817
+ plan[:declared_uris] ||= tree_uris(plan[:tree], {}).freeze
818
+ end
819
+
820
+ def tree_uris(tree, uris)
821
+ collect_uri = lambda do |ns|
822
+ uris[ns[:exact]] = true if ns.is_a?(::Hash) && ns[:exact]
823
+ end
824
+ collect_uri.call(tree[:ns])
825
+ (tree[:attributes] || []).each { |a| collect_uri.call(a[:ns]) }
826
+ (tree[:children] || []).each do |row|
827
+ collect_uri.call(row[:ns])
828
+ tree_uris(row[:plan], uris) if row[:plan]
829
+ end
830
+ uris
831
+ end
832
+
768
833
  # Whether an instance serializes through the ordered walk: an
769
834
  # ordered/mixed plan, or an instance carrying a programmatically
770
835
  # tracked element_order (the order-tracker pattern, lutaml-model
@@ -1048,7 +1113,10 @@ module Lutaml
1048
1113
  xsi_nil: false)
1049
1114
  child = element.create_child(name)
1050
1115
  attrs&.each { |k, v| child[k.to_s] = v.to_s }
1051
- child.set_attribute("xsi:nil", "true") if xsi_nil
1116
+ if xsi_nil
1117
+ child.set_attribute("xsi:nil", "true")
1118
+ declare_xsi_prefix(doc)
1119
+ end
1052
1120
  # An uninitialized value rendered under its value_map is an
1053
1121
  # empty element (the interpretive serialize_value yields no
1054
1122
  # text for it).
@@ -1058,6 +1126,21 @@ module Lutaml
1058
1126
  child
1059
1127
  end
1060
1128
 
1129
+ # xsi:nil spells the xsi prefix: the document root declares it,
1130
+ # as a parsed source's own declaration is kept (the stored
1131
+ # plan re-emits it) — a constructed tree carries none.
1132
+ def declare_xsi_prefix(doc)
1133
+ root = doc.root
1134
+ return unless root
1135
+
1136
+ prefix = Lutaml::Xml::W3c::XsiNamespace.prefix_default
1137
+ return if root["xmlns:#{prefix}"] ||
1138
+ root.namespace_definitions.any? { |ns| ns.prefix == prefix }
1139
+
1140
+ root.add_namespace_definition(prefix,
1141
+ Lutaml::Xml::W3c::XsiNamespace.uri)
1142
+ end
1143
+
1061
1144
  # A collection row's emission (the interpretive
1062
1145
  # apply_collection_element contract): one leaf per item — a
1063
1146
  # nil item goes unrendered when the value_map omits nil — and
@@ -1112,7 +1195,8 @@ module Lutaml
1112
1195
  # Shared with NativeEmission's gate: the order-entry predicate,
1113
1196
  # the render-policy placeholder and the coverage pre-check
1114
1197
  # decide what the ops lane cannot serve.
1115
- public :order_entry?, :ordered_walk?, :placeholder_for, :order_uncovered?
1198
+ public :order_entry?, :ordered_walk?, :placeholder_for, :order_uncovered?,
1199
+ :stored_declarations_uncovered?
1116
1200
 
1117
1201
  # Whether a row's value reaches the output at all — the
1118
1202
  # interpretive should_skip_value? contract (RenderPolicy): an