lutaml 0.11.6 → 0.11.8

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.
Files changed (881) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +20 -0
  3. data/.rubocop_todo.yml +0 -1
  4. data/Gemfile +32 -5
  5. data/README.adoc +3 -3
  6. data/README.md +6 -8
  7. data/Rakefile +41 -1
  8. data/bin/console +0 -1
  9. data/exe/lutaml-lml +6 -0
  10. data/exe/lutaml-lml-lsp +11 -0
  11. data/lib/compat/opal/generate_boot.rb +132 -0
  12. data/lib/compat/opal/js_bundle_entry.rb +42 -0
  13. data/lib/compat/opal/lutaml_model_boot.rb +540 -0
  14. data/lib/compat/opal/monitor.rb +31 -0
  15. data/lib/compat/opal/moxml/adapter/customized_rexml/entity_reference.rb +9 -0
  16. data/lib/compat/opal/moxml/adapter/customized_rexml/formatter.rb +225 -0
  17. data/lib/compat/opal/moxml/adapter/customized_rexml.rb +11 -0
  18. data/lib/compat/opal/moxml_boot.rb +68 -0
  19. data/lib/compat/opal/objectspace_shim.rb +23 -0
  20. data/lib/compat/opal/rexml/namespace.rb +59 -0
  21. data/lib/compat/opal/rexml/parsers/baseparser.rb +1016 -0
  22. data/lib/compat/opal/rexml/source.rb +214 -0
  23. data/lib/compat/opal/rexml/text.rb +426 -0
  24. data/lib/compat/opal/rexml/xmltokens.rb +45 -0
  25. data/lib/compat/opal/rexml_compat.rb +85 -0
  26. data/lib/compat/opal/yaml_compat.rb +32 -0
  27. data/lib/lutaml/hash_format/adapter/document.rb +10 -0
  28. data/lib/lutaml/hash_format/adapter/mapping.rb +17 -0
  29. data/lib/lutaml/hash_format/adapter/mapping_rule.rb +9 -0
  30. data/lib/lutaml/hash_format/adapter/standard_adapter.rb +17 -0
  31. data/lib/lutaml/hash_format/adapter/transform.rb +9 -0
  32. data/lib/lutaml/hash_format/adapter.rb +13 -0
  33. data/lib/lutaml/hash_format/format.rb +29 -0
  34. data/lib/lutaml/hash_format/type/serializers.rb +24 -0
  35. data/lib/lutaml/hash_format.rb +7 -0
  36. data/lib/lutaml/json/adapter/document.rb +13 -0
  37. data/lib/lutaml/json/adapter/mapping.rb +17 -0
  38. data/lib/lutaml/json/adapter/mapping_rule.rb +9 -0
  39. data/lib/lutaml/json/adapter/multi_json_adapter.rb +45 -0
  40. data/lib/lutaml/json/adapter/oj_adapter.rb +41 -0
  41. data/lib/lutaml/json/adapter/standard_adapter.rb +43 -0
  42. data/lib/lutaml/json/adapter/transform.rb +9 -0
  43. data/lib/lutaml/json/adapter/yeptris_adapter.rb +74 -0
  44. data/lib/lutaml/json/adapter.rb +21 -0
  45. data/lib/lutaml/json/format.rb +61 -0
  46. data/lib/lutaml/json/generator_options.rb +62 -0
  47. data/lib/lutaml/json/schema/json_schema.rb +62 -0
  48. data/lib/lutaml/json/schema.rb +9 -0
  49. data/lib/lutaml/json/type/serializers.rb +72 -0
  50. data/lib/lutaml/json.rb +7 -0
  51. data/lib/lutaml/jsonl/adapter/document.rb +14 -0
  52. data/lib/lutaml/jsonl/adapter/mapping.rb +17 -0
  53. data/lib/lutaml/jsonl/adapter/mapping_rule.rb +9 -0
  54. data/lib/lutaml/jsonl/adapter/standard_adapter.rb +34 -0
  55. data/lib/lutaml/jsonl/adapter/transform.rb +26 -0
  56. data/lib/lutaml/jsonl/adapter.rb +13 -0
  57. data/lib/lutaml/jsonl/format.rb +22 -0
  58. data/lib/lutaml/jsonl.rb +7 -0
  59. data/lib/lutaml/jsonld/adapter.rb +31 -0
  60. data/lib/lutaml/jsonld.rb +20 -0
  61. data/lib/lutaml/key_value/adapter/hash/document.rb +13 -0
  62. data/lib/lutaml/key_value/adapter/hash/mapping.rb +17 -0
  63. data/lib/lutaml/key_value/adapter/hash/mapping_rule.rb +9 -0
  64. data/lib/lutaml/key_value/adapter/hash/standard_adapter.rb +19 -0
  65. data/lib/lutaml/key_value/adapter/hash/transform.rb +10 -0
  66. data/lib/lutaml/key_value/adapter/hash.rb +15 -0
  67. data/lib/lutaml/key_value/adapter/json/document.rb +16 -0
  68. data/lib/lutaml/key_value/adapter/json/mapping.rb +22 -0
  69. data/lib/lutaml/key_value/adapter/json/mapping_rule.rb +14 -0
  70. data/lib/lutaml/key_value/adapter/json/multi_json_adapter.rb +54 -0
  71. data/lib/lutaml/key_value/adapter/json/oj_adapter.rb +46 -0
  72. data/lib/lutaml/key_value/adapter/json/standard_adapter.rb +47 -0
  73. data/lib/lutaml/key_value/adapter/json/transform.rb +14 -0
  74. data/lib/lutaml/key_value/adapter/json/yeptris_adapter.rb +15 -0
  75. data/lib/lutaml/key_value/adapter/json.rb +24 -0
  76. data/lib/lutaml/key_value/adapter/jsonl/document.rb +16 -0
  77. data/lib/lutaml/key_value/adapter/jsonl/mapping.rb +17 -0
  78. data/lib/lutaml/key_value/adapter/jsonl/mapping_rule.rb +9 -0
  79. data/lib/lutaml/key_value/adapter/jsonl/standard_adapter.rb +36 -0
  80. data/lib/lutaml/key_value/adapter/jsonl/transform.rb +26 -0
  81. data/lib/lutaml/key_value/adapter/jsonl.rb +17 -0
  82. data/lib/lutaml/key_value/adapter/toml/document.rb +13 -0
  83. data/lib/lutaml/key_value/adapter/toml/mapping.rb +26 -0
  84. data/lib/lutaml/key_value/adapter/toml/mapping_rule.rb +9 -0
  85. data/lib/lutaml/key_value/adapter/toml/teptris_adapter.rb +36 -0
  86. data/lib/lutaml/key_value/adapter/toml/toml_rb_adapter.rb +34 -0
  87. data/lib/lutaml/key_value/adapter/toml/tomlib_adapter.rb +40 -0
  88. data/lib/lutaml/key_value/adapter/toml/transform.rb +10 -0
  89. data/lib/lutaml/key_value/adapter/toml.rb +22 -0
  90. data/lib/lutaml/key_value/adapter/yaml/document.rb +12 -0
  91. data/lib/lutaml/key_value/adapter/yaml/mapping.rb +17 -0
  92. data/lib/lutaml/key_value/adapter/yaml/mapping_rule.rb +9 -0
  93. data/lib/lutaml/key_value/adapter/yaml/standard_adapter.rb +35 -0
  94. data/lib/lutaml/key_value/adapter/yaml/transform.rb +10 -0
  95. data/lib/lutaml/key_value/adapter/yaml/yeptris_adapter.rb +15 -0
  96. data/lib/lutaml/key_value/adapter/yaml.rb +17 -0
  97. data/lib/lutaml/key_value/adapter/yamls/document.rb +16 -0
  98. data/lib/lutaml/key_value/adapter/yamls/mapping.rb +17 -0
  99. data/lib/lutaml/key_value/adapter/yamls/mapping_rule.rb +9 -0
  100. data/lib/lutaml/key_value/adapter/yamls/standard_adapter.rb +37 -0
  101. data/lib/lutaml/key_value/adapter/yamls/transform.rb +26 -0
  102. data/lib/lutaml/key_value/adapter/yamls.rb +17 -0
  103. data/lib/lutaml/key_value/adapter.rb +14 -0
  104. data/lib/lutaml/key_value/data_model/element.rb +225 -0
  105. data/lib/lutaml/key_value/data_model.rb +14 -0
  106. data/lib/lutaml/key_value/document.rb +28 -0
  107. data/lib/lutaml/key_value/format.rb +26 -0
  108. data/lib/lutaml/key_value/mapping.rb +393 -0
  109. data/lib/lutaml/key_value/mapping_rule.rb +95 -0
  110. data/lib/lutaml/key_value/transform.rb +1223 -0
  111. data/lib/lutaml/key_value/transformation/collection_serializer.rb +572 -0
  112. data/lib/lutaml/key_value/transformation/rule_compiler.rb +299 -0
  113. data/lib/lutaml/key_value/transformation/value_serializer.rb +399 -0
  114. data/lib/lutaml/key_value/transformation.rb +1276 -0
  115. data/lib/lutaml/key_value/transformation_builder.rb +30 -0
  116. data/lib/lutaml/key_value.rb +7 -0
  117. data/lib/lutaml/lml/association_label_resolver.rb +52 -0
  118. data/lib/lutaml/lml/cli.rb +506 -0
  119. data/lib/lutaml/lml/data_processor/attribute_processing.rb +133 -0
  120. data/lib/lutaml/lml/data_processor/collection_processing.rb +37 -0
  121. data/lib/lutaml/lml/data_processor/instance_processing.rb +77 -0
  122. data/lib/lutaml/lml/data_processor/value_declaration_processing.rb +70 -0
  123. data/lib/lutaml/lml/data_processor/value_processing.rb +103 -0
  124. data/lib/lutaml/lml/data_processor/view_processing.rb +26 -0
  125. data/lib/lutaml/lml/data_processor.rb +59 -0
  126. data/lib/lutaml/lml/diagnostic.rb +149 -0
  127. data/lib/lutaml/lml/document_builder.rb +481 -0
  128. data/lib/lutaml/lml/entity_types.rb +30 -0
  129. data/lib/lutaml/lml/executor/adapter_helpers.rb +45 -0
  130. data/lib/lutaml/lml/executor/condition_evaluator.rb +169 -0
  131. data/lib/lutaml/lml/executor/csv_adapter.rb +88 -0
  132. data/lib/lutaml/lml/executor/format_adapter.rb +54 -0
  133. data/lib/lutaml/lml/executor/xml_adapter.rb +102 -0
  134. data/lib/lutaml/lml/executor.rb +96 -0
  135. data/lib/lutaml/lml/format/adapter/document.rb +11 -0
  136. data/lib/lutaml/lml/format/adapter/mapping.rb +31 -0
  137. data/lib/lutaml/lml/format/adapter/standard_adapter.rb +162 -0
  138. data/lib/lutaml/lml/format/adapter/transform.rb +11 -0
  139. data/lib/lutaml/lml/format.rb +29 -0
  140. data/lib/lutaml/lml/formatter/base.rb +79 -0
  141. data/lib/lutaml/lml/formatter/canonical.rb +736 -0
  142. data/lib/lutaml/lml/formatter/elk.rb +45 -0
  143. data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +134 -0
  144. data/lib/lutaml/lml/formatter/svg_pdf.rb +285 -0
  145. data/lib/lutaml/lml/formatter.rb +11 -0
  146. data/lib/lutaml/lml/grammar/lml.artifact.json +14993 -0
  147. data/lib/lutaml/lml/grammar/lml.parg +389 -0
  148. data/lib/lutaml/lml/grammar.rb +60 -0
  149. data/lib/lutaml/lml/has_attributes.rb +14 -0
  150. data/lib/lutaml/lml/import_resolver.rb +96 -0
  151. data/lib/lutaml/lml/layout/elk_engine.rb +15 -0
  152. data/lib/lutaml/lml/layout/elk_graph_builder.rb +129 -0
  153. data/lib/lutaml/lml/layout/elk_svg_markers.rb +45 -0
  154. data/lib/lutaml/lml/layout/elk_svg_renderer.rb +119 -0
  155. data/lib/lutaml/lml/layout/engine.rb +17 -0
  156. data/lib/lutaml/lml/layout.rb +12 -0
  157. data/lib/lutaml/lml/literal_value.rb +32 -0
  158. data/lib/lutaml/lml/lsp/conversions.rb +78 -0
  159. data/lib/lutaml/lml/lsp/keywords.rb +43 -0
  160. data/lib/lutaml/lml/lsp/server.rb +378 -0
  161. data/lib/lutaml/lml/lsp/store.rb +68 -0
  162. data/lib/lutaml/lml/lsp/transport.rb +53 -0
  163. data/lib/lutaml/lml/lsp.rb +16 -0
  164. data/lib/lutaml/lml/model_compiler/enums.rb +172 -0
  165. data/lib/lutaml/lml/model_compiler/hydration.rb +123 -0
  166. data/lib/lutaml/lml/model_compiler/serialization_mappings.rb +188 -0
  167. data/lib/lutaml/lml/model_compiler/string_formats.rb +42 -0
  168. data/lib/lutaml/lml/model_compiler/validation.rb +65 -0
  169. data/lib/lutaml/lml/model_compiler.rb +323 -0
  170. data/lib/lutaml/lml/models/action.rb +10 -0
  171. data/lib/lutaml/lml/models/association.rb +29 -0
  172. data/lib/lutaml/lml/models/cardinality.rb +10 -0
  173. data/lib/lutaml/lml/models/collection.rb +11 -0
  174. data/lib/lutaml/lml/models/constraint.rb +23 -0
  175. data/lib/lutaml/lml/models/data_type.rb +41 -0
  176. data/lib/lutaml/lml/models/derived_field.rb +14 -0
  177. data/lib/lutaml/lml/models/diagram.rb +20 -0
  178. data/lib/lutaml/lml/models/document.rb +84 -0
  179. data/lib/lutaml/lml/models/enum.rb +38 -0
  180. data/lib/lutaml/lml/models/fidelity.rb +10 -0
  181. data/lib/lutaml/lml/models/group.rb +11 -0
  182. data/lib/lutaml/lml/models/instance.rb +26 -0
  183. data/lib/lutaml/lml/models/instance_collection.rb +16 -0
  184. data/lib/lutaml/lml/models/instances_export.rb +10 -0
  185. data/lib/lutaml/lml/models/instances_import.rb +11 -0
  186. data/lib/lutaml/lml/models/mapping.rb +15 -0
  187. data/lib/lutaml/lml/models/mapping_rule.rb +16 -0
  188. data/lib/lutaml/lml/models/member_field.rb +14 -0
  189. data/lib/lutaml/lml/models/namespace.rb +16 -0
  190. data/lib/lutaml/lml/models/operation.rb +26 -0
  191. data/lib/lutaml/lml/models/operation_parameter.rb +12 -0
  192. data/lib/lutaml/lml/models/package.rb +26 -0
  193. data/lib/lutaml/lml/models/primitive_type.rb +33 -0
  194. data/lib/lutaml/lml/models/reference.rb +23 -0
  195. data/lib/lutaml/lml/models/serialization_mapping.rb +19 -0
  196. data/lib/lutaml/lml/models/string_format.rb +16 -0
  197. data/lib/lutaml/lml/models/top_element_attribute.rb +116 -0
  198. data/lib/lutaml/lml/models/uml_class.rb +85 -0
  199. data/lib/lutaml/lml/models/value.rb +15 -0
  200. data/lib/lutaml/lml/models/view_filter.rb +11 -0
  201. data/lib/lutaml/lml/models/view_import.rb +11 -0
  202. data/lib/lutaml/lml/parser.rb +19 -0
  203. data/lib/lutaml/lml/pipeline.rb +73 -0
  204. data/lib/lutaml/lml/positioned.rb +13 -0
  205. data/lib/lutaml/lml/preprocessor.rb +130 -0
  206. data/lib/lutaml/lml/quoting.rb +60 -0
  207. data/lib/lutaml/lml/source.rb +49 -0
  208. data/lib/lutaml/lml/source_position.rb +20 -0
  209. data/lib/lutaml/lml/symbol_index.rb +200 -0
  210. data/lib/lutaml/lml/transform.rb +119 -0
  211. data/lib/lutaml/lml/types/text_type.rb +28 -0
  212. data/lib/lutaml/lml/types.rb +10 -0
  213. data/lib/lutaml/lml/validator.rb +458 -0
  214. data/lib/lutaml/lml/version.rb +10 -0
  215. data/lib/lutaml/lml/view_resolution.rb +32 -0
  216. data/lib/lutaml/lml/view_resolver.rb +48 -0
  217. data/lib/lutaml/lml/yaml_parser.rb +17 -0
  218. data/lib/lutaml/lml.rb +130 -0
  219. data/lib/lutaml/model/adapter_resolver.rb +487 -0
  220. data/lib/lutaml/model/adapter_scope.rb +64 -0
  221. data/lib/lutaml/model/attribute.rb +1511 -0
  222. data/lib/lutaml/model/attribute_validator.rb +290 -0
  223. data/lib/lutaml/model/cached_type_resolver/concurrent_map_cache.rb +48 -0
  224. data/lib/lutaml/model/cached_type_resolver/mutex_hash_cache.rb +53 -0
  225. data/lib/lutaml/model/cached_type_resolver.rb +145 -0
  226. data/lib/lutaml/model/choice.rb +343 -0
  227. data/lib/lutaml/model/cli.rb +85 -0
  228. data/lib/lutaml/model/collection.rb +740 -0
  229. data/lib/lutaml/model/collection_handler.rb +157 -0
  230. data/lib/lutaml/model/comparable_model.rb +710 -0
  231. data/lib/lutaml/model/comparable_nil.rb +9 -0
  232. data/lib/lutaml/model/comparison.rb +15 -0
  233. data/lib/lutaml/model/compiled_rule.rb +242 -0
  234. data/lib/lutaml/model/config.rb +169 -0
  235. data/lib/lutaml/model/configuration.rb +138 -0
  236. data/lib/lutaml/model/consolidation/attribute_grouper.rb +44 -0
  237. data/lib/lutaml/model/consolidation/dispatcher.rb +45 -0
  238. data/lib/lutaml/model/consolidation/engine.rb +33 -0
  239. data/lib/lutaml/model/consolidation/pattern_chunker.rb +87 -0
  240. data/lib/lutaml/model/consolidation_map.rb +68 -0
  241. data/lib/lutaml/model/consolidation_rule/dispatch_block.rb +34 -0
  242. data/lib/lutaml/model/consolidation_rule/gather_rule.rb +17 -0
  243. data/lib/lutaml/model/consolidation_rule/pattern_content_rule.rb +15 -0
  244. data/lib/lutaml/model/consolidation_rule/pattern_element_rule.rb +17 -0
  245. data/lib/lutaml/model/consolidation_rule.rb +27 -0
  246. data/lib/lutaml/model/constants.rb +11 -0
  247. data/lib/lutaml/model/context_registry.rb +208 -0
  248. data/lib/lutaml/model/deep_dupable.rb +16 -0
  249. data/lib/lutaml/model/error/choice_lower_bound_error.rb +9 -0
  250. data/lib/lutaml/model/error/choice_upper_bound_error.rb +9 -0
  251. data/lib/lutaml/model/error/collection_count_out_of_range_error.rb +29 -0
  252. data/lib/lutaml/model/error/collection_true_missing_error.rb +16 -0
  253. data/lib/lutaml/model/error/element_count_out_of_range_error.rb +35 -0
  254. data/lib/lutaml/model/error/format_adapter_not_specified_error.rb +25 -0
  255. data/lib/lutaml/model/error/fraction_digits_error.rb +20 -0
  256. data/lib/lutaml/model/error/import_model_with_root_error.rb +9 -0
  257. data/lib/lutaml/model/error/incorrect_mapping_argument_error.rb +6 -0
  258. data/lib/lutaml/model/error/incorrect_sequence_error.rb +9 -0
  259. data/lib/lutaml/model/error/invalid_attribute_name_error.rb +15 -0
  260. data/lib/lutaml/model/error/invalid_attribute_options_error.rb +16 -0
  261. data/lib/lutaml/model/error/invalid_attribute_type_error.rb +98 -0
  262. data/lib/lutaml/model/error/invalid_choice_range_error.rb +18 -0
  263. data/lib/lutaml/model/error/invalid_format_error.rb +11 -0
  264. data/lib/lutaml/model/error/invalid_value_error.rb +18 -0
  265. data/lib/lutaml/model/error/length_error.rb +19 -0
  266. data/lib/lutaml/model/error/liquid_class_not_found_error.rb +9 -0
  267. data/lib/lutaml/model/error/liquid_drop_already_registered_error.rb +11 -0
  268. data/lib/lutaml/model/error/liquid_not_enabled_error.rb +9 -0
  269. data/lib/lutaml/model/error/mapping_already_exists_error.rb +6 -0
  270. data/lib/lutaml/model/error/mapping_attribute_missing_error.rb +6 -0
  271. data/lib/lutaml/model/error/mapping_attribute_type_error.rb +6 -0
  272. data/lib/lutaml/model/error/max_exclusive_error.rb +19 -0
  273. data/lib/lutaml/model/error/max_inclusive_error.rb +20 -0
  274. data/lib/lutaml/model/error/max_length_error.rb +20 -0
  275. data/lib/lutaml/model/error/min_exclusive_error.rb +19 -0
  276. data/lib/lutaml/model/error/min_inclusive_error.rb +20 -0
  277. data/lib/lutaml/model/error/min_length_error.rb +20 -0
  278. data/lib/lutaml/model/error/mixed_content_collection_error.rb +17 -0
  279. data/lib/lutaml/model/error/multiple_mappings_error.rb +6 -0
  280. data/lib/lutaml/model/error/no_attributes_defined_liquid_error.rb +9 -0
  281. data/lib/lutaml/model/error/no_mapping_found_error.rb +9 -0
  282. data/lib/lutaml/model/error/no_root_mapping_error.rb +10 -0
  283. data/lib/lutaml/model/error/no_root_namespace_error.rb +10 -0
  284. data/lib/lutaml/model/error/ordered_content_mapping_error.rb +17 -0
  285. data/lib/lutaml/model/error/pattern_not_matched_error.rb +17 -0
  286. data/lib/lutaml/model/error/polymorphic_error.rb +14 -0
  287. data/lib/lutaml/model/error/register/not_registrable_class_error.rb +11 -0
  288. data/lib/lutaml/model/error/required_attribute_missing_error.rb +9 -0
  289. data/lib/lutaml/model/error/restriction_error.rb +8 -0
  290. data/lib/lutaml/model/error/reverse_transform_block_not_defined_error.rb +6 -0
  291. data/lib/lutaml/model/error/reverse_transformation_declaration_error.rb +6 -0
  292. data/lib/lutaml/model/error/sorting_configuration_conflict_error.rb +9 -0
  293. data/lib/lutaml/model/error/total_digits_error.rb +20 -0
  294. data/lib/lutaml/model/error/transform_block_not_defined_error.rb +6 -0
  295. data/lib/lutaml/model/error/type/invalid_value_error.rb +21 -0
  296. data/lib/lutaml/model/error/type/max_bound_error.rb +20 -0
  297. data/lib/lutaml/model/error/type/max_exclusive_error.rb +20 -0
  298. data/lib/lutaml/model/error/type/max_length_error.rb +20 -0
  299. data/lib/lutaml/model/error/type/min_bound_error.rb +20 -0
  300. data/lib/lutaml/model/error/type/min_exclusive_error.rb +20 -0
  301. data/lib/lutaml/model/error/type/min_length_error.rb +20 -0
  302. data/lib/lutaml/model/error/type/pattern_not_matched_error.rb +18 -0
  303. data/lib/lutaml/model/error/type.rb +19 -0
  304. data/lib/lutaml/model/error/type_error.rb +9 -0
  305. data/lib/lutaml/model/error/type_not_enabled_error.rb +9 -0
  306. data/lib/lutaml/model/error/type_only_mapping_error.rb +13 -0
  307. data/lib/lutaml/model/error/type_only_namespace_error.rb +12 -0
  308. data/lib/lutaml/model/error/undefined_attribute_error.rb +9 -0
  309. data/lib/lutaml/model/error/union_schema_unsupported_error.rb +11 -0
  310. data/lib/lutaml/model/error/unknown_adapter_type_error.rb +95 -0
  311. data/lib/lutaml/model/error/unknown_discriminator_error.rb +11 -0
  312. data/lib/lutaml/model/error/unknown_mapping_error.rb +18 -0
  313. data/lib/lutaml/model/error/unknown_sequence_mapping_error.rb +9 -0
  314. data/lib/lutaml/model/error/unknown_type_error.rb +32 -0
  315. data/lib/lutaml/model/error/unresolvable_type_error.rb +27 -0
  316. data/lib/lutaml/model/error/unsupported_version_error.rb +14 -0
  317. data/lib/lutaml/model/error/validation_error.rb +20 -0
  318. data/lib/lutaml/model/error/validation_failed_error.rb +9 -0
  319. data/lib/lutaml/model/error.rb +8 -0
  320. data/lib/lutaml/model/errors.rb +38 -0
  321. data/lib/lutaml/model/evolution_steps.rb +85 -0
  322. data/lib/lutaml/model/finalization_cache.rb +51 -0
  323. data/lib/lutaml/model/format_registry.rb +218 -0
  324. data/lib/lutaml/model/global_context.rb +476 -0
  325. data/lib/lutaml/model/global_register.rb +142 -0
  326. data/lib/lutaml/model/hash.rb +15 -0
  327. data/lib/lutaml/model/import_registry.rb +250 -0
  328. data/lib/lutaml/model/instrumentation.rb +304 -0
  329. data/lib/lutaml/model/json.rb +25 -0
  330. data/lib/lutaml/model/json_adapter.rb +8 -0
  331. data/lib/lutaml/model/jsonl.rb +15 -0
  332. data/lib/lutaml/model/liquefiable.rb +198 -0
  333. data/lib/lutaml/model/liquid/indexed_access.rb +33 -0
  334. data/lib/lutaml/model/liquid/mapping.rb +30 -0
  335. data/lib/lutaml/model/liquid.rb +10 -0
  336. data/lib/lutaml/model/lml/document.rb +109 -0
  337. data/lib/lutaml/model/lml/exporter.rb +328 -0
  338. data/lib/lutaml/model/lml/rejected_error.rb +32 -0
  339. data/lib/lutaml/model/lml/round_trip.rb +49 -0
  340. data/lib/lutaml/model/lml.rb +40 -0
  341. data/lib/lutaml/model/mapping/listener.rb +126 -0
  342. data/lib/lutaml/model/mapping/mapping.rb +209 -0
  343. data/lib/lutaml/model/mapping/mapping_rule.rb +673 -0
  344. data/lib/lutaml/model/mapping/model_mapping.rb +133 -0
  345. data/lib/lutaml/model/mapping/model_mapping_rule.rb +57 -0
  346. data/lib/lutaml/model/mapping_hash.rb +80 -0
  347. data/lib/lutaml/model/model_transformer.rb +101 -0
  348. data/lib/lutaml/model/one_entry_cache.rb +57 -0
  349. data/lib/lutaml/model/organization.rb +29 -0
  350. data/lib/lutaml/model/register/model_tree_importer.rb +88 -0
  351. data/lib/lutaml/model/register/namespace_binding.rb +92 -0
  352. data/lib/lutaml/model/register.rb +589 -0
  353. data/lib/lutaml/model/registrable.rb +8 -0
  354. data/lib/lutaml/model/render_policy.rb +166 -0
  355. data/lib/lutaml/model/restriction_validation.rb +437 -0
  356. data/lib/lutaml/model/runtime_compatibility.rb +144 -0
  357. data/lib/lutaml/model/schema/base_schema.rb +77 -0
  358. data/lib/lutaml/model/schema/class_loader.rb +59 -0
  359. data/lib/lutaml/model/schema/compiled_output.rb +83 -0
  360. data/lib/lutaml/model/schema/decorators/attribute.rb +116 -0
  361. data/lib/lutaml/model/schema/decorators/choices.rb +29 -0
  362. data/lib/lutaml/model/schema/decorators/class_definition.rb +84 -0
  363. data/lib/lutaml/model/schema/decorators/definition_collection.rb +97 -0
  364. data/lib/lutaml/model/schema/decorators.rb +15 -0
  365. data/lib/lutaml/model/schema/definitions/attribute.rb +32 -0
  366. data/lib/lutaml/model/schema/definitions/choice.rb +20 -0
  367. data/lib/lutaml/model/schema/definitions/facet.rb +40 -0
  368. data/lib/lutaml/model/schema/definitions/group_import.rb +22 -0
  369. data/lib/lutaml/model/schema/definitions/member_walk.rb +33 -0
  370. data/lib/lutaml/model/schema/definitions/model.rb +40 -0
  371. data/lib/lutaml/model/schema/definitions/namespace.rb +24 -0
  372. data/lib/lutaml/model/schema/definitions/restricted_type.rb +32 -0
  373. data/lib/lutaml/model/schema/definitions/sequence.rb +19 -0
  374. data/lib/lutaml/model/schema/definitions/simple_content.rb +22 -0
  375. data/lib/lutaml/model/schema/definitions/transform_facet.rb +21 -0
  376. data/lib/lutaml/model/schema/definitions/type_ref.rb +20 -0
  377. data/lib/lutaml/model/schema/definitions/union_type.rb +28 -0
  378. data/lib/lutaml/model/schema/definitions/xml_root.rb +20 -0
  379. data/lib/lutaml/model/schema/definitions.rb +24 -0
  380. data/lib/lutaml/model/schema/file_writer.rb +73 -0
  381. data/lib/lutaml/model/schema/generator/definition.rb +98 -0
  382. data/lib/lutaml/model/schema/generator/definitions_collection.rb +117 -0
  383. data/lib/lutaml/model/schema/generator/properties_collection.rb +65 -0
  384. data/lib/lutaml/model/schema/generator/property.rb +182 -0
  385. data/lib/lutaml/model/schema/generator/ref.rb +24 -0
  386. data/lib/lutaml/model/schema/generator.rb +15 -0
  387. data/lib/lutaml/model/schema/helpers/template_helper.rb +49 -0
  388. data/lib/lutaml/model/schema/helpers.rb +11 -0
  389. data/lib/lutaml/model/schema/json_schema.rb +17 -0
  390. data/lib/lutaml/model/schema/json_schema_parser.rb +91 -0
  391. data/lib/lutaml/model/schema/module_nesting.rb +31 -0
  392. data/lib/lutaml/model/schema/namespace_naming.rb +46 -0
  393. data/lib/lutaml/model/schema/registry_generator.rb +115 -0
  394. data/lib/lutaml/model/schema/renderer.rb +38 -0
  395. data/lib/lutaml/model/schema/renderers/base.rb +41 -0
  396. data/lib/lutaml/model/schema/renderers/mappings.rb +78 -0
  397. data/lib/lutaml/model/schema/renderers/member_decls.rb +121 -0
  398. data/lib/lutaml/model/schema/renderers/model.rb +133 -0
  399. data/lib/lutaml/model/schema/renderers/namespace.rb +30 -0
  400. data/lib/lutaml/model/schema/renderers/registration.rb +64 -0
  401. data/lib/lutaml/model/schema/renderers/required_files_calculator.rb +94 -0
  402. data/lib/lutaml/model/schema/renderers/restricted_type.rb +218 -0
  403. data/lib/lutaml/model/schema/renderers/union.rb +83 -0
  404. data/lib/lutaml/model/schema/renderers.rb +20 -0
  405. data/lib/lutaml/model/schema/rnc_compiler/source_resolver.rb +51 -0
  406. data/lib/lutaml/model/schema/rnc_compiler.rb +64 -0
  407. data/lib/lutaml/model/schema/rng_compiler/define_classifier.rb +94 -0
  408. data/lib/lutaml/model/schema/rng_compiler/element_visitor.rb +412 -0
  409. data/lib/lutaml/model/schema/rng_compiler/member_collector.rb +30 -0
  410. data/lib/lutaml/model/schema/rng_compiler/rng_helpers.rb +160 -0
  411. data/lib/lutaml/model/schema/rng_compiler/value_type_resolver.rb +129 -0
  412. data/lib/lutaml/model/schema/rng_compiler.rb +225 -0
  413. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +41 -0
  414. data/lib/lutaml/model/schema/schema_builder/oga.rb +92 -0
  415. data/lib/lutaml/model/schema/shared_methods.rb +24 -0
  416. data/lib/lutaml/model/schema/templates/model.erb +9 -0
  417. data/lib/lutaml/model/schema/templates.rb +164 -0
  418. data/lib/lutaml/model/schema/xml_compiler/element_order.rb +42 -0
  419. data/lib/lutaml/model/schema/xml_compiler/registry_generator.rb +51 -0
  420. data/lib/lutaml/model/schema/xml_compiler/spec_builder/complex_types.rb +198 -0
  421. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +249 -0
  422. data/lib/lutaml/model/schema/xml_compiler/spec_builder/simple_types.rb +185 -0
  423. data/lib/lutaml/model/schema/xml_compiler/spec_builder.rb +186 -0
  424. data/lib/lutaml/model/schema/xml_compiler/supported_data_types.rb +100 -0
  425. data/lib/lutaml/model/schema/xml_compiler.rb +109 -0
  426. data/lib/lutaml/model/schema/yaml_schema.rb +17 -0
  427. data/lib/lutaml/model/schema.rb +88 -0
  428. data/lib/lutaml/model/schema_location.rb +18 -0
  429. data/lib/lutaml/model/sequence.rb +220 -0
  430. data/lib/lutaml/model/serializable.rb +46 -0
  431. data/lib/lutaml/model/serialization_adapter.rb +24 -0
  432. data/lib/lutaml/model/serialize/attribute_definition.rb +897 -0
  433. data/lib/lutaml/model/serialize/builder.rb +258 -0
  434. data/lib/lutaml/model/serialize/conversion_caching.rb +129 -0
  435. data/lib/lutaml/model/serialize/deserialization_context.rb +51 -0
  436. data/lib/lutaml/model/serialize/enum_handling.rb +171 -0
  437. data/lib/lutaml/model/serialize/format_conversion.rb +624 -0
  438. data/lib/lutaml/model/serialize/initialization.rb +825 -0
  439. data/lib/lutaml/model/serialize/model_import.rb +425 -0
  440. data/lib/lutaml/model/serialize/transformation_builder.rb +128 -0
  441. data/lib/lutaml/model/serialize/value_mapping.rb +173 -0
  442. data/lib/lutaml/model/serialize.rb +533 -0
  443. data/lib/lutaml/model/services/base.rb +13 -0
  444. data/lib/lutaml/model/services/logger.rb +57 -0
  445. data/lib/lutaml/model/services/rule_value_extractor.rb +95 -0
  446. data/lib/lutaml/model/services/transformer.rb +143 -0
  447. data/lib/lutaml/model/services/type/validator/number.rb +25 -0
  448. data/lib/lutaml/model/services/type/validator/string.rb +53 -0
  449. data/lib/lutaml/model/services/type/validator/symbol.rb +30 -0
  450. data/lib/lutaml/model/services/type/validator.rb +69 -0
  451. data/lib/lutaml/model/services/type.rb +9 -0
  452. data/lib/lutaml/model/services/validator.rb +143 -0
  453. data/lib/lutaml/model/services.rb +10 -0
  454. data/lib/lutaml/model/store.rb +239 -0
  455. data/lib/lutaml/model/toml.rb +40 -0
  456. data/lib/lutaml/model/transform/xml_transform.rb +5 -0
  457. data/lib/lutaml/model/transform.rb +229 -0
  458. data/lib/lutaml/model/transformation.rb +89 -0
  459. data/lib/lutaml/model/transformation_builder.rb +55 -0
  460. data/lib/lutaml/model/transformation_registry.rb +327 -0
  461. data/lib/lutaml/model/type/base64_binary.rb +58 -0
  462. data/lib/lutaml/model/type/boolean.rb +35 -0
  463. data/lib/lutaml/model/type/date.rb +75 -0
  464. data/lib/lutaml/model/type/date_time.rb +60 -0
  465. data/lib/lutaml/model/type/date_time_with_timezone.rb +28 -0
  466. data/lib/lutaml/model/type/date_with_timezone.rb +28 -0
  467. data/lib/lutaml/model/type/day_time_duration.rb +35 -0
  468. data/lib/lutaml/model/type/decimal.rb +64 -0
  469. data/lib/lutaml/model/type/duration.rb +82 -0
  470. data/lib/lutaml/model/type/email_address.rb +28 -0
  471. data/lib/lutaml/model/type/float.rb +33 -0
  472. data/lib/lutaml/model/type/hash.rb +74 -0
  473. data/lib/lutaml/model/type/hex_binary.rb +56 -0
  474. data/lib/lutaml/model/type/hostname.rb +30 -0
  475. data/lib/lutaml/model/type/integer.rb +62 -0
  476. data/lib/lutaml/model/type/ip_v4_address.rb +27 -0
  477. data/lib/lutaml/model/type/ip_v6_address.rb +47 -0
  478. data/lib/lutaml/model/type/metaschema_pattern.rb +34 -0
  479. data/lib/lutaml/model/type/non_negative_integer.rb +25 -0
  480. data/lib/lutaml/model/type/positive_integer.rb +25 -0
  481. data/lib/lutaml/model/type/qname.rb +85 -0
  482. data/lib/lutaml/model/type/reference.rb +76 -0
  483. data/lib/lutaml/model/type/string.rb +62 -0
  484. data/lib/lutaml/model/type/symbol.rb +67 -0
  485. data/lib/lutaml/model/type/time.rb +52 -0
  486. data/lib/lutaml/model/type/time_without_date.rb +50 -0
  487. data/lib/lutaml/model/type/token.rb +25 -0
  488. data/lib/lutaml/model/type/uninitialized_class_guard.rb +36 -0
  489. data/lib/lutaml/model/type/uri.rb +51 -0
  490. data/lib/lutaml/model/type/uri_reference.rb +29 -0
  491. data/lib/lutaml/model/type/uuid.rb +25 -0
  492. data/lib/lutaml/model/type/value.rb +364 -0
  493. data/lib/lutaml/model/type/year_month_duration.rb +29 -0
  494. data/lib/lutaml/model/type.rb +199 -0
  495. data/lib/lutaml/model/type_context.rb +294 -0
  496. data/lib/lutaml/model/type_registry.rb +165 -0
  497. data/lib/lutaml/model/type_resolver.rb +187 -0
  498. data/lib/lutaml/model/type_substitution.rb +117 -0
  499. data/lib/lutaml/model/uninitialized_class.rb +70 -0
  500. data/lib/lutaml/model/union.rb +363 -0
  501. data/lib/lutaml/model/utils.rb +352 -0
  502. data/lib/lutaml/model/validation/concerns/has_issues.rb +27 -0
  503. data/lib/lutaml/model/validation/context.rb +36 -0
  504. data/lib/lutaml/model/validation/issue.rb +62 -0
  505. data/lib/lutaml/model/validation/layer_result.rb +34 -0
  506. data/lib/lutaml/model/validation/profile.rb +66 -0
  507. data/lib/lutaml/model/validation/registry.rb +60 -0
  508. data/lib/lutaml/model/validation/remediation.rb +33 -0
  509. data/lib/lutaml/model/validation/remediation_result.rb +20 -0
  510. data/lib/lutaml/model/validation/report.rb +39 -0
  511. data/lib/lutaml/model/validation/rule.rb +59 -0
  512. data/lib/lutaml/model/validation.rb +137 -0
  513. data/lib/lutaml/model/validation_framework.rb +77 -0
  514. data/lib/lutaml/model/value_policy.rb +27 -0
  515. data/lib/lutaml/model/value_transformer.rb +25 -0
  516. data/lib/lutaml/model/version.rb +7 -0
  517. data/lib/lutaml/model/yaml.rb +15 -0
  518. data/lib/lutaml/model/yamls.rb +15 -0
  519. data/lib/lutaml/model.rb +309 -0
  520. data/lib/lutaml/rdf/context.rb +69 -0
  521. data/lib/lutaml/rdf/error.rb +7 -0
  522. data/lib/lutaml/rdf/iri.rb +44 -0
  523. data/lib/lutaml/rdf/language_tagged.rb +11 -0
  524. data/lib/lutaml/rdf/linked_data_transform.rb +249 -0
  525. data/lib/lutaml/rdf/literal.rb +62 -0
  526. data/lib/lutaml/rdf/mapping.rb +86 -0
  527. data/lib/lutaml/rdf/mapping_rule.rb +52 -0
  528. data/lib/lutaml/rdf/member_rule.rb +55 -0
  529. data/lib/lutaml/rdf/namespace.rb +58 -0
  530. data/lib/lutaml/rdf/namespace_set.rb +69 -0
  531. data/lib/lutaml/rdf/namespaces/dcterms_namespace.rb +12 -0
  532. data/lib/lutaml/rdf/namespaces/owl_namespace.rb +12 -0
  533. data/lib/lutaml/rdf/namespaces/rdf_namespace.rb +14 -0
  534. data/lib/lutaml/rdf/namespaces/rdfs_namespace.rb +12 -0
  535. data/lib/lutaml/rdf/namespaces/skos_namespace.rb +12 -0
  536. data/lib/lutaml/rdf/namespaces/xsd_namespace.rb +12 -0
  537. data/lib/lutaml/rdf/namespaces.rb +14 -0
  538. data/lib/lutaml/rdf/term_definition.rb +39 -0
  539. data/lib/lutaml/rdf/transform.rb +45 -0
  540. data/lib/lutaml/rdf.rb +22 -0
  541. data/lib/lutaml/toml/adapter/document.rb +10 -0
  542. data/lib/lutaml/toml/adapter/mapping.rb +26 -0
  543. data/lib/lutaml/toml/adapter/mapping_rule.rb +9 -0
  544. data/lib/lutaml/toml/adapter/teptris_adapter.rb +14 -0
  545. data/lib/lutaml/toml/adapter/toml_rb_adapter.rb +32 -0
  546. data/lib/lutaml/toml/adapter/tomlib_adapter.rb +48 -0
  547. data/lib/lutaml/toml/adapter/transform.rb +9 -0
  548. data/lib/lutaml/toml/adapter.rb +20 -0
  549. data/lib/lutaml/toml/format.rb +48 -0
  550. data/lib/lutaml/toml/type/serializers.rb +71 -0
  551. data/lib/lutaml/toml.rb +7 -0
  552. data/lib/lutaml/turtle/adapter.rb +35 -0
  553. data/lib/lutaml/turtle/mapping.rb +7 -0
  554. data/lib/lutaml/turtle/transform.rb +250 -0
  555. data/lib/lutaml/turtle.rb +22 -0
  556. data/lib/lutaml/version.rb +1 -1
  557. data/lib/lutaml/xml/adapter/adapter_helpers.rb +108 -0
  558. data/lib/lutaml/xml/adapter/base_adapter.rb +249 -0
  559. data/lib/lutaml/xml/adapter/leptris_adapter.rb +16 -0
  560. data/lib/lutaml/xml/adapter/namespace_data.rb +56 -0
  561. data/lib/lutaml/xml/adapter/namespace_uri_collector.rb +71 -0
  562. data/lib/lutaml/xml/adapter/nokogiri_adapter.rb +16 -0
  563. data/lib/lutaml/xml/adapter/oga_adapter.rb +20 -0
  564. data/lib/lutaml/xml/adapter/ox_adapter.rb +16 -0
  565. data/lib/lutaml/xml/adapter/plan_based_builder.rb +944 -0
  566. data/lib/lutaml/xml/adapter/rexml_adapter.rb +25 -0
  567. data/lib/lutaml/xml/adapter/xml_parser.rb +89 -0
  568. data/lib/lutaml/xml/adapter/xml_serializer.rb +334 -0
  569. data/lib/lutaml/xml/adapter.rb +24 -0
  570. data/lib/lutaml/xml/adapter_element.rb +339 -0
  571. data/lib/lutaml/xml/adapter_loader.rb +56 -0
  572. data/lib/lutaml/xml/attribute_namespace_resolver.rb +139 -0
  573. data/lib/lutaml/xml/blank_namespace.rb +51 -0
  574. data/lib/lutaml/xml/blank_namespace_handler.rb +50 -0
  575. data/lib/lutaml/xml/builder/base.rb +258 -0
  576. data/lib/lutaml/xml/builder/leptris.rb +13 -0
  577. data/lib/lutaml/xml/builder/nokogiri.rb +13 -0
  578. data/lib/lutaml/xml/builder/oga.rb +13 -0
  579. data/lib/lutaml/xml/builder/ox.rb +13 -0
  580. data/lib/lutaml/xml/builder/rexml.rb +13 -0
  581. data/lib/lutaml/xml/builder.rb +18 -0
  582. data/lib/lutaml/xml/configurable.rb +215 -0
  583. data/lib/lutaml/xml/data_model.rb +295 -0
  584. data/lib/lutaml/xml/decisions/decision.rb +120 -0
  585. data/lib/lutaml/xml/decisions/decision_context.rb +225 -0
  586. data/lib/lutaml/xml/decisions/decision_engine.rb +67 -0
  587. data/lib/lutaml/xml/decisions/decision_rule.rb +54 -0
  588. data/lib/lutaml/xml/decisions/element_prefix_resolver.rb +85 -0
  589. data/lib/lutaml/xml/decisions/rules/attribute_usage_rule.rb +37 -0
  590. data/lib/lutaml/xml/decisions/rules/default_preference_rule.rb +62 -0
  591. data/lib/lutaml/xml/decisions/rules/element_form_default_rule.rb +66 -0
  592. data/lib/lutaml/xml/decisions/rules/element_form_default_unqualified_rule.rb +73 -0
  593. data/lib/lutaml/xml/decisions/rules/element_form_option_rule.rb +83 -0
  594. data/lib/lutaml/xml/decisions/rules/explicit_option_rule.rb +57 -0
  595. data/lib/lutaml/xml/decisions/rules/format_preservation_rule.rb +74 -0
  596. data/lib/lutaml/xml/decisions/rules/hoisted_on_parent_rule.rb +60 -0
  597. data/lib/lutaml/xml/decisions/rules/inherit_from_parent_rule.rb +52 -0
  598. data/lib/lutaml/xml/decisions/rules/inherit_parent_prefix_rule.rb +69 -0
  599. data/lib/lutaml/xml/decisions/rules/namespace_scope_rule.rb +54 -0
  600. data/lib/lutaml/xml/decisions/rules/reuse_parent_prefix_rule.rb +78 -0
  601. data/lib/lutaml/xml/decisions/rules/used_prefix_rule.rb +70 -0
  602. data/lib/lutaml/xml/decisions/rules.rb +39 -0
  603. data/lib/lutaml/xml/decisions.rb +15 -0
  604. data/lib/lutaml/xml/declaration_handler.rb +115 -0
  605. data/lib/lutaml/xml/declaration_plan/attribute_node.rb +81 -0
  606. data/lib/lutaml/xml/declaration_plan/element_node.rb +101 -0
  607. data/lib/lutaml/xml/declaration_plan.rb +480 -0
  608. data/lib/lutaml/xml/declaration_plan_query.rb +105 -0
  609. data/lib/lutaml/xml/declaration_planner.rb +1914 -0
  610. data/lib/lutaml/xml/doctype_extractor.rb +51 -0
  611. data/lib/lutaml/xml/document.rb +385 -0
  612. data/lib/lutaml/xml/element.rb +108 -0
  613. data/lib/lutaml/xml/element_prefix_resolver.rb +50 -0
  614. data/lib/lutaml/xml/encoding_normalizer.rb +54 -0
  615. data/lib/lutaml/xml/error/invalid_namespace_error.rb +46 -0
  616. data/lib/lutaml/xml/error/invalid_xsd_type_error.rb +43 -0
  617. data/lib/lutaml/xml/error/namespace_mismatch_error.rb +53 -0
  618. data/lib/lutaml/xml/error/schema_validation_error.rb +25 -0
  619. data/lib/lutaml/xml/error/xml_configuration_error.rb +17 -0
  620. data/lib/lutaml/xml/error/xml_error.rb +21 -0
  621. data/lib/lutaml/xml/format.rb +364 -0
  622. data/lib/lutaml/xml/format_chooser.rb +214 -0
  623. data/lib/lutaml/xml/hoisting_algorithm.rb +234 -0
  624. data/lib/lutaml/xml/html_entities.rb +290 -0
  625. data/lib/lutaml/xml/leptris/element.rb +34 -0
  626. data/lib/lutaml/xml/leptris/native_emission.rb +295 -0
  627. data/lib/lutaml/xml/leptris/plan_compiler.rb +652 -0
  628. data/lib/lutaml/xml/leptris/plan_hydrator.rb +827 -0
  629. data/lib/lutaml/xml/leptris/plan_order.rb +72 -0
  630. data/lib/lutaml/xml/leptris/plan_serializer.rb +711 -0
  631. data/lib/lutaml/xml/leptris/plan_walk.rb +36 -0
  632. data/lib/lutaml/xml/leptris.rb +48 -0
  633. data/lib/lutaml/xml/listener.rb +33 -0
  634. data/lib/lutaml/xml/mapping.rb +1746 -0
  635. data/lib/lutaml/xml/mapping_rule.rb +688 -0
  636. data/lib/lutaml/xml/model_transform.rb +1824 -0
  637. data/lib/lutaml/xml/namespace.rb +445 -0
  638. data/lib/lutaml/xml/namespace_class_registry.rb +144 -0
  639. data/lib/lutaml/xml/namespace_collector.rb +649 -0
  640. data/lib/lutaml/xml/namespace_declaration.rb +202 -0
  641. data/lib/lutaml/xml/namespace_declaration_builder.rb +67 -0
  642. data/lib/lutaml/xml/namespace_declaration_data.rb +179 -0
  643. data/lib/lutaml/xml/namespace_inheritance_resolver.rb +142 -0
  644. data/lib/lutaml/xml/namespace_inheritance_strategy.rb +26 -0
  645. data/lib/lutaml/xml/namespace_needs.rb +255 -0
  646. data/lib/lutaml/xml/namespace_resolution_strategy.rb +230 -0
  647. data/lib/lutaml/xml/namespace_resolver.rb +219 -0
  648. data/lib/lutaml/xml/namespace_scope_config.rb +110 -0
  649. data/lib/lutaml/xml/namespace_type_resolver.rb +135 -0
  650. data/lib/lutaml/xml/namespace_usage.rb +121 -0
  651. data/lib/lutaml/xml/nested_collection_attribute.rb +95 -0
  652. data/lib/lutaml/xml/nokogiri/element.rb +34 -0
  653. data/lib/lutaml/xml/nokogiri.rb +10 -0
  654. data/lib/lutaml/xml/oga/element.rb +15 -0
  655. data/lib/lutaml/xml/oga.rb +10 -0
  656. data/lib/lutaml/xml/ox/element.rb +15 -0
  657. data/lib/lutaml/xml/ox.rb +10 -0
  658. data/lib/lutaml/xml/parse_session.rb +139 -0
  659. data/lib/lutaml/xml/parsed_namespace_declaration.rb +129 -0
  660. data/lib/lutaml/xml/parsed_namespace_set.rb +149 -0
  661. data/lib/lutaml/xml/polymorphic_value_handler.rb +41 -0
  662. data/lib/lutaml/xml/qualified_inheritance_strategy.rb +27 -0
  663. data/lib/lutaml/xml/rexml/element.rb +15 -0
  664. data/lib/lutaml/xml/rexml.rb +10 -0
  665. data/lib/lutaml/xml/schema/builder/nokogiri.rb +41 -0
  666. data/lib/lutaml/xml/schema/builder/oga.rb +92 -0
  667. data/lib/lutaml/xml/schema/builder.rb +59 -0
  668. data/lib/lutaml/xml/schema/builtin_types.rb +112 -0
  669. data/lib/lutaml/xml/schema/relaxng_schema.rb +83 -0
  670. data/lib/lutaml/xml/schema/xsd/all.rb +31 -0
  671. data/lib/lutaml/xml/schema/xsd/annotation.rb +31 -0
  672. data/lib/lutaml/xml/schema/xsd/any.rb +33 -0
  673. data/lib/lutaml/xml/schema/xsd/any_attribute.rb +29 -0
  674. data/lib/lutaml/xml/schema/xsd/appinfo.rb +25 -0
  675. data/lib/lutaml/xml/schema/xsd/attribute.rb +78 -0
  676. data/lib/lutaml/xml/schema/xsd/attribute_group.rb +89 -0
  677. data/lib/lutaml/xml/schema/xsd/base.rb +270 -0
  678. data/lib/lutaml/xml/schema/xsd/choice.rb +68 -0
  679. data/lib/lutaml/xml/schema/xsd/complex_content.rb +39 -0
  680. data/lib/lutaml/xml/schema/xsd/complex_type.rb +159 -0
  681. data/lib/lutaml/xml/schema/xsd/documentation.rb +26 -0
  682. data/lib/lutaml/xml/schema/xsd/element.rb +117 -0
  683. data/lib/lutaml/xml/schema/xsd/enumeration.rb +25 -0
  684. data/lib/lutaml/xml/schema/xsd/errors/enhanced_error.rb +142 -0
  685. data/lib/lutaml/xml/schema/xsd/errors/error_context.rb +67 -0
  686. data/lib/lutaml/xml/schema/xsd/errors/message_builder.rb +103 -0
  687. data/lib/lutaml/xml/schema/xsd/errors/suggestion.rb +67 -0
  688. data/lib/lutaml/xml/schema/xsd/errors/troubleshooters/namespace_troubleshooter.rb +96 -0
  689. data/lib/lutaml/xml/schema/xsd/errors/troubleshooters/troubleshooting_handler.rb +70 -0
  690. data/lib/lutaml/xml/schema/xsd/errors.rb +148 -0
  691. data/lib/lutaml/xml/schema/xsd/extension_complex_content.rb +63 -0
  692. data/lib/lutaml/xml/schema/xsd/extension_simple_content.rb +48 -0
  693. data/lib/lutaml/xml/schema/xsd/field.rb +25 -0
  694. data/lib/lutaml/xml/schema/xsd/file_validation_result.rb +55 -0
  695. data/lib/lutaml/xml/schema/xsd/fraction_digits.rb +29 -0
  696. data/lib/lutaml/xml/schema/xsd/glob.rb +105 -0
  697. data/lib/lutaml/xml/schema/xsd/group.rb +79 -0
  698. data/lib/lutaml/xml/schema/xsd/import.rb +33 -0
  699. data/lib/lutaml/xml/schema/xsd/include.rb +31 -0
  700. data/lib/lutaml/xml/schema/xsd/key.rb +32 -0
  701. data/lib/lutaml/xml/schema/xsd/keyref.rb +34 -0
  702. data/lib/lutaml/xml/schema/xsd/length.rb +27 -0
  703. data/lib/lutaml/xml/schema/xsd/list.rb +29 -0
  704. data/lib/lutaml/xml/schema/xsd/max_exclusive.rb +25 -0
  705. data/lib/lutaml/xml/schema/xsd/max_inclusive.rb +23 -0
  706. data/lib/lutaml/xml/schema/xsd/max_length.rb +25 -0
  707. data/lib/lutaml/xml/schema/xsd/min_exclusive.rb +25 -0
  708. data/lib/lutaml/xml/schema/xsd/min_inclusive.rb +23 -0
  709. data/lib/lutaml/xml/schema/xsd/min_length.rb +25 -0
  710. data/lib/lutaml/xml/schema/xsd/namespace_mapping.rb +37 -0
  711. data/lib/lutaml/xml/schema/xsd/namespace_uri_remapping.rb +79 -0
  712. data/lib/lutaml/xml/schema/xsd/notation.rb +31 -0
  713. data/lib/lutaml/xml/schema/xsd/pattern.rb +25 -0
  714. data/lib/lutaml/xml/schema/xsd/redefine.rb +35 -0
  715. data/lib/lutaml/xml/schema/xsd/restriction_complex_content.rb +56 -0
  716. data/lib/lutaml/xml/schema/xsd/restriction_simple_content.rb +84 -0
  717. data/lib/lutaml/xml/schema/xsd/restriction_simple_type.rb +64 -0
  718. data/lib/lutaml/xml/schema/xsd/schema.rb +379 -0
  719. data/lib/lutaml/xml/schema/xsd/schema_file_validation_results.rb +55 -0
  720. data/lib/lutaml/xml/schema/xsd/schema_location_mapping.rb +74 -0
  721. data/lib/lutaml/xml/schema/xsd/schema_name_resolver.rb +32 -0
  722. data/lib/lutaml/xml/schema/xsd/schema_path.rb +207 -0
  723. data/lib/lutaml/xml/schema/xsd/schema_validator.rb +259 -0
  724. data/lib/lutaml/xml/schema/xsd/selector.rb +27 -0
  725. data/lib/lutaml/xml/schema/xsd/sequence.rb +68 -0
  726. data/lib/lutaml/xml/schema/xsd/serialized_schema.rb +185 -0
  727. data/lib/lutaml/xml/schema/xsd/simple_content.rb +61 -0
  728. data/lib/lutaml/xml/schema/xsd/simple_type.rb +37 -0
  729. data/lib/lutaml/xml/schema/xsd/total_digits.rb +29 -0
  730. data/lib/lutaml/xml/schema/xsd/type_index_entry.rb +40 -0
  731. data/lib/lutaml/xml/schema/xsd/type_resolution_result.rb +112 -0
  732. data/lib/lutaml/xml/schema/xsd/union.rb +30 -0
  733. data/lib/lutaml/xml/schema/xsd/unique.rb +31 -0
  734. data/lib/lutaml/xml/schema/xsd/validation_error.rb +48 -0
  735. data/lib/lutaml/xml/schema/xsd/validation_result.rb +86 -0
  736. data/lib/lutaml/xml/schema/xsd/version.rb +11 -0
  737. data/lib/lutaml/xml/schema/xsd/white_space.rb +27 -0
  738. data/lib/lutaml/xml/schema/xsd.rb +164 -0
  739. data/lib/lutaml/xml/schema/xsd_namespace.rb +29 -0
  740. data/lib/lutaml/xml/schema/xsd_schema.rb +1002 -0
  741. data/lib/lutaml/xml/schema.rb +16 -0
  742. data/lib/lutaml/xml/schema_location.rb +155 -0
  743. data/lib/lutaml/xml/serialization/collection_ext.rb +61 -0
  744. data/lib/lutaml/xml/serialization/format_conversion.rb +433 -0
  745. data/lib/lutaml/xml/serialization/instance_methods.rb +323 -0
  746. data/lib/lutaml/xml/serialization/model_import_ext.rb +40 -0
  747. data/lib/lutaml/xml/serialization.rb +15 -0
  748. data/lib/lutaml/xml/shared_dsl.rb +119 -0
  749. data/lib/lutaml/xml/transform.rb +6 -0
  750. data/lib/lutaml/xml/transformation/custom_method_wrapper.rb +258 -0
  751. data/lib/lutaml/xml/transformation/element_builder.rb +697 -0
  752. data/lib/lutaml/xml/transformation/order_reconciler.rb +282 -0
  753. data/lib/lutaml/xml/transformation/ordered_applier.rb +529 -0
  754. data/lib/lutaml/xml/transformation/rule_applier.rb +403 -0
  755. data/lib/lutaml/xml/transformation/rule_compiler.rb +556 -0
  756. data/lib/lutaml/xml/transformation/skip_logic.rb +70 -0
  757. data/lib/lutaml/xml/transformation/value_serializer.rb +137 -0
  758. data/lib/lutaml/xml/transformation.rb +454 -0
  759. data/lib/lutaml/xml/transformation_builder.rb +28 -0
  760. data/lib/lutaml/xml/transformation_support.rb +15 -0
  761. data/lib/lutaml/xml/type/configurable.rb +153 -0
  762. data/lib/lutaml/xml/type/serializers.rb +105 -0
  763. data/lib/lutaml/xml/type/value_xml_mapping.rb +94 -0
  764. data/lib/lutaml/xml/type_namespace/collector.rb +101 -0
  765. data/lib/lutaml/xml/type_namespace/declaration.rb +61 -0
  766. data/lib/lutaml/xml/type_namespace/planner.rb +121 -0
  767. data/lib/lutaml/xml/type_namespace/reference.rb +71 -0
  768. data/lib/lutaml/xml/type_namespace/resolver.rb +43 -0
  769. data/lib/lutaml/xml/type_namespace.rb +13 -0
  770. data/lib/lutaml/xml/type_namespace_resolver.rb +76 -0
  771. data/lib/lutaml/xml/unqualified_inheritance_strategy.rb +34 -0
  772. data/lib/lutaml/xml/w3c/registration.rb +66 -0
  773. data/lib/lutaml/xml/w3c.rb +398 -0
  774. data/lib/lutaml/xml/xml_attribute.rb +58 -0
  775. data/lib/lutaml/xml/xml_element.rb +498 -0
  776. data/lib/lutaml/xml/xml_namespace.rb +72 -0
  777. data/lib/lutaml/xml/xml_orderable.rb +17 -0
  778. data/lib/lutaml/xml/xsd_validator.rb +66 -0
  779. data/lib/lutaml/xml.rb +7 -0
  780. data/lib/lutaml/yaml/adapter/document.rb +12 -0
  781. data/lib/lutaml/yaml/adapter/mapping.rb +17 -0
  782. data/lib/lutaml/yaml/adapter/mapping_rule.rb +9 -0
  783. data/lib/lutaml/yaml/adapter/standard_adapter.rb +35 -0
  784. data/lib/lutaml/yaml/adapter/transform.rb +9 -0
  785. data/lib/lutaml/yaml/adapter/yeptris_adapter.rb +107 -0
  786. data/lib/lutaml/yaml/adapter.rb +18 -0
  787. data/lib/lutaml/yaml/format.rb +31 -0
  788. data/lib/lutaml/yaml/schema/yaml_schema.rb +65 -0
  789. data/lib/lutaml/yaml/schema.rb +9 -0
  790. data/lib/lutaml/yaml/type/serializers.rb +60 -0
  791. data/lib/lutaml/yaml.rb +7 -0
  792. data/lib/lutaml/yamlld/adapter.rb +20 -0
  793. data/lib/lutaml/yamlld.rb +25 -0
  794. data/lib/lutaml/yamls/adapter/document.rb +14 -0
  795. data/lib/lutaml/yamls/adapter/mapping.rb +30 -0
  796. data/lib/lutaml/yamls/adapter/mapping_rule.rb +9 -0
  797. data/lib/lutaml/yamls/adapter/standard_adapter.rb +56 -0
  798. data/lib/lutaml/yamls/adapter/transform.rb +124 -0
  799. data/lib/lutaml/yamls/adapter/yamls_sequence.rb +20 -0
  800. data/lib/lutaml/yamls/adapter/yamls_sequence_rule.rb +48 -0
  801. data/lib/lutaml/yamls/adapter.rb +15 -0
  802. data/lib/lutaml/yamls/format.rb +22 -0
  803. data/lib/lutaml/yamls.rb +7 -0
  804. data/lib/lutaml.rb +11 -5
  805. data/lib/tasks/benchmark_runner.rb +161 -0
  806. data/lib/tasks/memory_profile.rb +176 -0
  807. data/lib/tasks/performance.rake +80 -0
  808. data/lib/tasks/performance_benchmark.rb +499 -0
  809. data/lib/tasks/performance_comparator.rb +130 -0
  810. data/lib/tasks/performance_helpers.rb +149 -0
  811. data/lib/tasks/performance_probe.rake +33 -0
  812. data/lib/tasks/performance_probe.rb +106 -0
  813. data/lib/tasks/performance_probe_plan_walk.rb +58 -0
  814. data/lib/tasks/performance_probe_xml.rb +54 -0
  815. data/lib/tasks/xml_compiler_benchmark.rb +226 -0
  816. data/lib/tasks/xml_optimization_benchmark.rb +178 -0
  817. data/lutaml.gemspec +16 -8
  818. metadata +902 -83
  819. data/.github/workflows/docs.yml +0 -86
  820. data/.github/workflows/links.yml +0 -113
  821. data/.github/workflows/rake.yml +0 -23
  822. data/.github/workflows/release.yml +0 -37
  823. data/.gitignore +0 -29
  824. data/CLAUDE.md +0 -62
  825. data/docs/.github/workflows/docs.yml +0 -66
  826. data/docs/.github/workflows/links.yml +0 -78
  827. data/docs/CONTINUATION_PLAN.md +0 -168
  828. data/docs/Gemfile +0 -13
  829. data/docs/MIGRATION_GUIDE.md +0 -202
  830. data/docs/README.md +0 -252
  831. data/docs/REFACTOR_PLAN.md +0 -214
  832. data/docs/_config.yml +0 -144
  833. data/docs/_guides/cli-usage.adoc +0 -43
  834. data/docs/_guides/diagram-generation.adoc +0 -316
  835. data/docs/_guides/index.adoc +0 -136
  836. data/docs/_guides/parsing/express-parsing.adoc +0 -42
  837. data/docs/_guides/parsing/index.adoc +0 -78
  838. data/docs/_guides/parsing/qea-parsing.adoc +0 -47
  839. data/docs/_guides/parsing/xmi-parsing.adoc +0 -40
  840. data/docs/_guides/repository-management.adoc +0 -44
  841. data/docs/_guides/sysml-modeling.adoc +0 -37
  842. data/docs/_guides/uml-modeling.adoc +0 -115
  843. data/docs/_pages/concepts/diagram-generation.adoc +0 -337
  844. data/docs/_pages/concepts/lur-format.adoc +0 -711
  845. data/docs/_pages/concepts/qea-parsing.adoc +0 -418
  846. data/docs/_pages/concepts/uml-models.adoc +0 -43
  847. data/docs/_pages/core/architecture.adoc +0 -329
  848. data/docs/_pages/core/installation.adoc +0 -321
  849. data/docs/_pages/core/overview.adoc +0 -197
  850. data/docs/_pages/index.adoc +0 -49
  851. data/docs/_pages/uml-syntax.adoc +0 -1011
  852. data/docs/_references/api/parsers.adoc +0 -61
  853. data/docs/_references/api/presenters.adoc +0 -43
  854. data/docs/_references/api/repository.adoc +0 -67
  855. data/docs/_references/cli/commands.adoc +0 -72
  856. data/docs/_references/configuration/diagram-styles.adoc +0 -716
  857. data/docs/_references/configuration/index.adoc +0 -53
  858. data/docs/_references/configuration/package-metadata.adoc +0 -416
  859. data/docs/_references/formats/index.adoc +0 -43
  860. data/docs/_references/formats/lur-format.adoc +0 -39
  861. data/docs/_references/formats/lutaml-syntax.adoc +0 -379
  862. data/docs/_references/formats/qea-structure.adoc +0 -1364
  863. data/docs/_references/index.adoc +0 -198
  864. data/docs/_tutorials/documentation-site.adoc +0 -47
  865. data/docs/_tutorials/first-lur-package.adoc +0 -45
  866. data/docs/_tutorials/generating-diagrams.adoc +0 -42
  867. data/docs/_tutorials/getting-started.adoc +0 -45
  868. data/docs/_tutorials/index.adoc +0 -100
  869. data/docs/_tutorials/querying-models.adoc +0 -44
  870. data/docs/examples/build_package.rb +0 -12
  871. data/docs/examples/build_site.rb +0 -12
  872. data/docs/examples/city_model.rb +0 -21
  873. data/docs/examples/present_class.rb +0 -10
  874. data/docs/examples/query_model.rb +0 -12
  875. data/docs/examples/render_diagram.rb +0 -11
  876. data/docs/index.adoc +0 -64
  877. data/docs/lychee.toml +0 -50
  878. data/lib/lutaml/express/parsers/exp.rb +0 -28
  879. data/lib/lutaml/express.rb +0 -10
  880. data/scripts/check-site-urls.rb +0 -54
  881. data/scripts/check-vendor-link.py +0 -64
@@ -0,0 +1,1914 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Xml
5
+ # Phase 2: Declaration Planning
6
+ #
7
+ # Builds ElementNode tree with W3C-compliant attribute prefix decisions.
8
+ # The tree is isomorphic to XmlDataModel for index-based parallel traversal.
9
+ #
10
+ # CRITICAL: This planner ONLY builds trees. NO flat mode.
11
+ #
12
+ class DeclarationPlanner
13
+ # Initialize planner with register for type resolution
14
+ #
15
+ # @param register [Symbol] the register ID for type resolution
16
+ def initialize(register = nil)
17
+ @register = register || Lutaml::Model::Config.default_register
18
+ end
19
+
20
+ # Create declaration plan tree for XmlElement
21
+ #
22
+ # @param root_element [XmlDataModel::XmlElement, Model, nil, Class] root element, model instance, or nil/Class for unit testing
23
+ # @param mapping [Xml::Mapping] the XML mapping
24
+ # @param needs [NamespaceNeeds] namespace needs from collector
25
+ # @param options [Hash] serialization options (may contain :stored_xml_declaration_plan with input_formats)
26
+ # @return [DeclarationPlan] declaration plan with tree structure
27
+ def plan(root_element, mapping, needs, parent_plan: nil, options: {},
28
+ visited_types: Set.new)
29
+ # Normalize root_element: transform Model instances to XmlElement
30
+ root_element = normalize_root_element(root_element, mapping, options)
31
+
32
+ # Allow nil and Class for unit testing (type analysis without element instance)
33
+ if root_element && !root_element.is_a?(Lutaml::Xml::DataModel::XmlElement) && !root_element.is_a?(Class)
34
+ raise ArgumentError,
35
+ "DeclarationPlanner ONLY works with XmlElement trees. Got: #{root_element.class}"
36
+ end
37
+
38
+ # Handle nil or Class root_element for unit testing
39
+ if root_element.nil? || root_element.is_a?(Class)
40
+ # CRITICAL: Resolve type namespace refs BEFORE using type_attribute_namespaces
41
+ TypeNamespaceResolver.new(@register).resolve(needs)
42
+
43
+ # Build namespace_classes hash from needs for unit testing
44
+ namespace_classes = {}
45
+ needs.all_namespace_classes.each do |ns_class|
46
+ namespace_classes[ns_class.uri] = ns_class
47
+ end
48
+
49
+ # CRITICAL: Add namespace_scope namespaces to namespace_classes
50
+ # (even if not used, :always mode requires them to be declared)
51
+ needs.namespace_scope_configs.each do |scope_config|
52
+ ns_class = scope_config.namespace_class
53
+ namespace_classes[ns_class.uri] ||= ns_class
54
+ end
55
+
56
+ # Get element's own namespace (from mapping)
57
+ element_namespace = mapping&.namespace_class
58
+
59
+ # Create minimal root node with namespace hoisting info
60
+ # W3C rule: Namespaces used in attributes MUST use prefix format
61
+ hoisted = {}
62
+
63
+ # FIRST: Process namespace_scope configurations (root-only)
64
+ needs.namespace_scope_configs.each do |scope_config|
65
+ ns_class = scope_config.namespace_class
66
+ next if ns_class == element_namespace # Don't add root's own namespace here
67
+
68
+ # Check :always mode or :auto mode with usage
69
+ ns_usage = needs.namespace(ns_class.to_key)
70
+ should_declare = scope_config.always_mode? ||
71
+ (scope_config.auto_mode? && ns_usage&.used_in&.any?)
72
+
73
+ if should_declare
74
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
75
+ hoisted[prefix] = ns_class.uri
76
+ end
77
+ end
78
+
79
+ # SECOND: Add element's own namespace (if not already added)
80
+ if element_namespace && !hoisted.value?(element_namespace.uri)
81
+ # Check if element's namespace is used in type attributes
82
+ # If so, use prefix format (W3C rule: namespaces in attributes MUST use prefix)
83
+ element_ns_in_attributes = needs.type_attribute_namespaces.any? do |ns|
84
+ ns.uri == element_namespace.uri
85
+ end
86
+
87
+ # Check use_prefix option (Tier 1 priority)
88
+ use_prefix_option = options[:use_prefix]
89
+
90
+ if element_ns_in_attributes
91
+ # Namespace used in attributes - MUST use prefix format
92
+ prefix = element_namespace.prefix_default || "ns#{hoisted.keys.length}"
93
+ hoisted[prefix] = element_namespace.uri
94
+ elsif use_prefix_option == true
95
+ # Force prefix format when use_prefix: true
96
+ prefix = element_namespace.prefix_default || "ns#{hoisted.keys.length}"
97
+ hoisted[prefix] = element_namespace.uri
98
+ elsif use_prefix_option.is_a?(String)
99
+ # Use custom prefix string
100
+ hoisted[use_prefix_option] = element_namespace.uri
101
+ elsif use_prefix_option == false
102
+ # Force default format when use_prefix: false
103
+ hoisted[nil] = element_namespace.uri
104
+ elsif element_namespace.element_form_default_set? &&
105
+ element_namespace.element_form_default == :unqualified
106
+ # W3C elementFormDefault="unqualified": local elements should be unqualified.
107
+ # When parent uses prefix format, children can simply omit xmlns (blank namespace).
108
+ # When parent uses default format, children need xmlns="" to opt out.
109
+ # Prefer prefix format so children can be truly blank (no xmlns attribute).
110
+ # CRITICAL: Only applies when explicitly set, not when defaulted to :unqualified.
111
+ prefix = element_namespace.prefix_default || "ns#{hoisted.keys.length}"
112
+ hoisted[prefix] = element_namespace.uri
113
+ else
114
+ # Default: prefer default format (cleaner)
115
+ hoisted[nil] = element_namespace.uri
116
+ end
117
+ end
118
+
119
+ # THIRD: Add type namespaces (W3C: type namespaces MUST use prefix)
120
+ # CRITICAL: Type namespaces respect namespace_scope directive.
121
+ # When namespace_scope is configured, only hoist type namespaces in scope.
122
+ #
123
+ # Check if namespace_scope is configured
124
+ has_namespace_scope = !needs.namespace_scope_configs.empty?
125
+
126
+ # Type attribute namespaces
127
+ needs.type_attribute_namespaces.each do |ns_class|
128
+ ns_uri = ns_class.uri
129
+ next if hoisted.value?(ns_uri) # Skip if already added
130
+
131
+ # If namespace_scope is configured, only hoist if in scope
132
+ if has_namespace_scope
133
+ scope_config = needs.scope_config_for(ns_class)
134
+ next unless scope_config
135
+ end
136
+
137
+ # Namespaces in attributes MUST use prefix format (W3C rule)
138
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
139
+ hoisted[prefix] = ns_class.uri
140
+ end
141
+
142
+ # Type element namespaces
143
+ needs.type_element_namespaces.each do |ns_class|
144
+ ns_uri = ns_class.uri
145
+ next if hoisted.value?(ns_uri) # Skip if already added
146
+ next if ns_class == element_namespace # Skip element's own namespace
147
+
148
+ # NOTE: Type namespaces are different from child element namespaces.
149
+ # Type namespaces are declared on Type::Value subclasses and used as
150
+ # prefixes on child elements. They MUST be hoisted to root with prefix
151
+ # format (W3C constraint: only one default namespace per element).
152
+ #
153
+ # The condition below only applies to child element namespaces, NOT Type
154
+ # namespaces. Type namespaces should ALWAYS be hoisted.
155
+ #
156
+ # Examples of child element namespaces (should NOT be hoisted if different):
157
+ # - Root has XMI namespace, child has XMI_NEW namespace → child declares
158
+ # - Root has NO namespace, child has XMI namespace → child declares
159
+ #
160
+ # Examples of Type namespaces (should ALWAYS be hoisted):
161
+ # - Root has any namespace, attribute type has XMI namespace → hoist XMI
162
+ # - Root has NO namespace, attribute type has XMI namespace → hoist XMI
163
+ # Type namespaces are NOT about element structure, they're about type identity.
164
+
165
+ # If namespace_scope is configured, only hoist if in scope
166
+ if has_namespace_scope
167
+ scope_config = needs.scope_config_for(ns_class)
168
+ next unless scope_config
169
+ end
170
+
171
+ # Type element namespaces MUST use prefix format
172
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
173
+ hoisted[prefix] = ns_class.uri
174
+ end
175
+
176
+ # FOURTH: Add remaining namespaces not in namespace_scope
177
+ needs.all_namespace_classes.each do |ns_class|
178
+ ns_uri = ns_class.uri
179
+ next if hoisted.value?(ns_uri) # Skip if already added
180
+
181
+ # Check if this namespace is in namespace_scope (skip if yes)
182
+ scope_config = needs.scope_config_for(ns_class)
183
+ next if scope_config
184
+
185
+ # Add remaining namespace (default format preferred)
186
+ hoisted[nil] = ns_class.uri
187
+ end
188
+
189
+ root_node = DeclarationPlan::ElementNode.new(
190
+ qualified_name: "",
191
+ use_prefix: nil,
192
+ hoisted_declarations: hoisted,
193
+ )
194
+
195
+ # Build children_plans for attributes with Serializable types
196
+ children_plans = build_children_plans_from_metadata(mapping, needs,
197
+ options)
198
+
199
+ return DeclarationPlan.new(
200
+ root_node: root_node,
201
+ global_prefix_registry: build_prefix_registry(needs),
202
+ input_formats: {},
203
+ namespace_classes: namespace_classes,
204
+ children_plans: children_plans,
205
+ original_namespace_uris: options[:__original_namespace_uris] || {},
206
+ )
207
+ end
208
+
209
+ # TREE PATH: XmlElement tree path
210
+ # CRITICAL: Resolve type namespace refs BEFORE using type_attribute_namespaces
211
+ TypeNamespaceResolver.new(@register).resolve(needs)
212
+
213
+ # Extract input_formats from stored plan if present (format preservation)
214
+ input_formats = options[:stored_xml_declaration_plan]&.input_formats || {}
215
+ build_options = options.merge(input_formats: input_formats)
216
+
217
+ # Build namespace_classes hash for unit testing compatibility
218
+ namespace_classes = {}
219
+ needs.all_namespace_classes.each do |ns_class|
220
+ namespace_classes[ns_class.uri] = ns_class
221
+ end
222
+
223
+ # Build the element node tree recursively (mark root, no parent context)
224
+ root_node = build_element_node(
225
+ root_element, mapping, needs, build_options,
226
+ is_root: true,
227
+ parent_format: nil,
228
+ parent_namespace_class: nil,
229
+ parent_namespace_prefix: nil,
230
+ parent_hoisted: {}
231
+ )
232
+
233
+ # Build children_plans for each child element (for child_plan() method)
234
+ children_plans = build_children_plans(root_element, mapping, needs,
235
+ build_options)
236
+
237
+ # Create DeclarationPlan with tree and input_formats
238
+ DeclarationPlan.new(
239
+ root_node: root_node,
240
+ global_prefix_registry: build_prefix_registry(needs),
241
+ input_formats: input_formats,
242
+ namespace_classes: namespace_classes,
243
+ children_plans: children_plans,
244
+ original_namespace_uris: options[:__original_namespace_uris] || {},
245
+ )
246
+ end
247
+
248
+ # Create declaration plan for a collection
249
+ #
250
+ # @param collection [Collection] the collection object
251
+ # @param mapping [Xml::Mapping] the XML mapping
252
+ # @param needs [NamespaceNeeds] namespace needs from collector
253
+ # @return [DeclarationPlan] declaration plan with children_plans
254
+ def plan_collection(collection, mapping, needs)
255
+ # For collections, create a plan with children_plans for each item
256
+ root_node = DeclarationPlan::ElementNode.new(
257
+ qualified_name: mapping.root_element || "",
258
+ use_prefix: nil,
259
+ hoisted_declarations: {},
260
+ )
261
+
262
+ # Build namespace_classes from needs
263
+ namespace_classes = {}
264
+ needs.all_namespace_classes.each do |ns_class|
265
+ namespace_classes[ns_class.uri] = ns_class
266
+ end
267
+
268
+ # Build individual child plans for collection items
269
+ children_plans = build_collection_item_plans(collection, mapping, needs)
270
+
271
+ DeclarationPlan.new(
272
+ root_node: root_node,
273
+ global_prefix_registry: build_prefix_registry(needs),
274
+ input_formats: {},
275
+ namespace_classes: namespace_classes,
276
+ children_plans: children_plans,
277
+ )
278
+ end
279
+
280
+ private
281
+
282
+ attr_reader :register
283
+
284
+ # Resolve the correct register for a child mapper class.
285
+ # Hoisted per (mapper class) on the planner: resolve_for_child is
286
+ # globally cached but each call still allocates its key — the
287
+ # serialization hot path resolves the same handful of child
288
+ # classes per element.
289
+ #
290
+ # @param mapper_class [Class] The child's mapper class
291
+ # @return [Symbol] The resolved register ID
292
+ def register_for(mapper_class)
293
+ @register_for ||= {}
294
+ @register_for[mapper_class] ||=
295
+ Lutaml::Model::Register.resolve_for_child(mapper_class, @register)
296
+ end
297
+
298
+ # Build individual child plans for collection items
299
+ #
300
+ # @param collection [Collection] the collection object
301
+ # @param mapping [Xml::Mapping] the XML mapping
302
+ # @param needs [NamespaceNeeds] namespace needs from collector
303
+ # @return [Hash<Integer, DeclarationPlan>] Children plans by item index
304
+ def build_collection_item_plans(collection, _mapping, _needs)
305
+ children_plans = {}
306
+
307
+ return children_plans unless collection.is_a?(Array)
308
+
309
+ # Get the item type from the collection class
310
+ item_type = begin
311
+ collection.class.instance_type
312
+ rescue StandardError
313
+ nil
314
+ end
315
+
316
+ collection.each_with_index do |item, index|
317
+ next unless item
318
+
319
+ # Get the item's mapper class
320
+ item_mapper_class = if item.is_a?(Lutaml::Model::Serializable)
321
+ item.class
322
+ elsif item_type.is_a?(Class)
323
+ item_type
324
+ else
325
+ next
326
+ end
327
+
328
+ # Get the item's XML mapping
329
+ item_reg = register_for(item_mapper_class)
330
+ item_mapping = item_mapper_class.mappings_for(:xml, item_reg)
331
+ next unless item_mapping
332
+
333
+ # Collect namespace needs for this item
334
+ collector = NamespaceCollector.new(item_reg)
335
+ item_needs = collector.collect(item, item_mapping)
336
+
337
+ # Build a plan for this item
338
+ item_plan = plan(item, item_mapping, item_needs)
339
+ children_plans[index] = item_plan if item_plan
340
+ end
341
+
342
+ children_plans
343
+ end
344
+
345
+ # Normalize root element: transform Model instances to XmlElement
346
+ #
347
+ # @param root_element [XmlDataModel::XmlElement, Model, nil, Class] Root element
348
+ # @param mapping [Xml::Mapping] XML mapping
349
+ # @param options [Hash] Serialization options
350
+ # @return [XmlDataModel::XmlElement, nil, Class] Normalized root element
351
+ def normalize_root_element(root_element, _mapping, options)
352
+ return root_element if root_element.nil? || root_element.is_a?(Class)
353
+ return root_element if root_element.is_a?(Lutaml::Xml::DataModel::XmlElement)
354
+
355
+ # Check if root_element is a Model instance (has xml mapping)
356
+ if root_element.is_a?(Lutaml::Model::Serialize)
357
+ # Get mapper_class from options or infer from root_element
358
+ mapper_class = options[:mapper_class] || root_element.class
359
+
360
+ # Get mapping for the model class
361
+ root_reg = register_for(mapper_class)
362
+ mapping_dsl = mapper_class.mappings_for(:xml, root_reg)
363
+
364
+ # Use Xml::Transformation to convert model to XmlElement
365
+ # Pass register ID directly (Transformation handles Symbol)
366
+ transformation = Xml::Transformation.new(mapper_class, mapping_dsl,
367
+ :xml, root_reg)
368
+ transformed = transformation.transform(root_element, options)
369
+
370
+ # Return transformed XmlElement
371
+ return transformed if transformed.is_a?(Lutaml::Xml::DataModel::XmlElement)
372
+ end
373
+
374
+ # Return as-is if no transformation needed
375
+ root_element
376
+ end
377
+
378
+ # Build children_plans hash for child_plan() method
379
+ #
380
+ # @param xml_element [XmlDataModel::XmlElement] Parent element
381
+ # @param mapping [Xml::Mapping] XML mapping
382
+ # @param needs [NamespaceNeeds] Namespace needs
383
+ # @param options [Hash] Serialization options
384
+ # @return [Hash<Symbol, DeclarationPlan>] Children plans by attribute name
385
+ def build_children_plans(xml_element, mapping, needs, options)
386
+ children_plans = {}
387
+
388
+ # Get mapper_class to find child attributes
389
+ mapper_class = options[:mapper_class]
390
+ return children_plans unless mapper_class.is_a?(Class) &&
391
+ mapper_class.include?(Lutaml::Model::Serialize)
392
+
393
+ # Build namespace_classes hash for child plans
394
+ namespace_classes = {}
395
+ needs.all_namespace_classes.each do |ns_class|
396
+ namespace_classes[ns_class.uri] = ns_class
397
+ end
398
+
399
+ # Iterate through XmlElement children
400
+ xml_element.children.each do |xml_child|
401
+ next unless xml_child.is_a?(Lutaml::Xml::DataModel::XmlElement)
402
+
403
+ # Find the matching mapping rule for this child
404
+ child_name = xml_child.name
405
+ matching_rule = mapping.elements.find do |rule|
406
+ rule.name.to_s == child_name
407
+ end
408
+ next unless matching_rule
409
+
410
+ # Get the attribute definition for this child
411
+ attr_def = mapper_class.attributes[matching_rule.to]
412
+ next unless attr_def
413
+
414
+ # Build child's hoisted declarations
415
+ # CRITICAL: Child elements with type namespace attributes need those
416
+ # namespaces declared on themselves. Get type attribute namespaces from
417
+ # child's own needs (stored in needs.children)
418
+ child_hoisted = build_child_hoisted_declarations(attr_def, needs,
419
+ options,
420
+ mapper_class: mapper_class)
421
+
422
+ # Create child DeclarationPlan with the child's namespace info
423
+ # Children inherit parent's namespace_classes
424
+ child_plan = DeclarationPlan.new(
425
+ root_node: DeclarationPlan::ElementNode.new(
426
+ qualified_name: xml_child.name,
427
+ use_prefix: nil,
428
+ hoisted_declarations: child_hoisted,
429
+ ),
430
+ global_prefix_registry: build_prefix_registry(needs),
431
+ input_formats: {},
432
+ namespace_classes: namespace_classes,
433
+ )
434
+
435
+ # Store by attribute name
436
+ children_plans[attr_def.name] = child_plan
437
+ end
438
+
439
+ children_plans
440
+ end
441
+
442
+ # Build children_plans from mapper metadata (for nil/Class root_element)
443
+ #
444
+ # @param mapping [Xml::Mapping] XML mapping
445
+ # @param needs [NamespaceNeeds] Namespace needs
446
+ # @param options [Hash] Serialization options
447
+ # @return [Hash<Symbol, DeclarationPlan>] Children plans by attribute name
448
+ def build_children_plans_from_metadata(mapping, needs, options)
449
+ children_plans = {}
450
+
451
+ # Get mapper_class to find child attributes
452
+ mapper_class = options[:mapper_class]
453
+ return children_plans unless mapper_class.is_a?(Class) &&
454
+ mapper_class.include?(Lutaml::Model::Serialize)
455
+
456
+ # Build namespace_classes hash for child plans
457
+ namespace_classes = {}
458
+ needs.all_namespace_classes.each do |ns_class|
459
+ namespace_classes[ns_class.uri] = ns_class
460
+ end
461
+
462
+ # Get parent's hoisted declarations for child inheritance
463
+ parent_hoisted = build_parent_hoisted_for_children(mapping, needs,
464
+ options)
465
+
466
+ # Iterate through mapper_class attributes
467
+ mapper_class.attributes.each_value do |attr_def|
468
+ # Check if attribute has a Serializable type
469
+ attr_type = attr_def.type(register_for(mapper_class))
470
+ next unless attr_type
471
+ next unless attr_type.is_a?(Class)
472
+ next unless attr_type < Lutaml::Model::Serialize
473
+
474
+ # Create child DeclarationPlan with parent's namespace_classes
475
+ # Child inherits parent's hoisted declarations
476
+ child_plan = DeclarationPlan.new(
477
+ root_node: DeclarationPlan::ElementNode.new(
478
+ qualified_name: "",
479
+ use_prefix: nil,
480
+ hoisted_declarations: parent_hoisted,
481
+ ),
482
+ global_prefix_registry: build_prefix_registry(needs),
483
+ input_formats: {},
484
+ namespace_classes: namespace_classes,
485
+ )
486
+
487
+ # Store by attribute name
488
+ children_plans[attr_def.name] = child_plan
489
+ end
490
+
491
+ children_plans
492
+ end
493
+
494
+ # Build parent's hoisted declarations for child inheritance
495
+ #
496
+ # @param mapping [Xml::Mapping] XML mapping
497
+ # @param needs [NamespaceNeeds] Namespace needs
498
+ # @param options [Hash] Serialization options
499
+ # @return [Hash<String|nil, String>] Hoisted declarations {prefix => uri}
500
+ def build_parent_hoisted_for_children(mapping, needs, options)
501
+ hoisted = {}
502
+
503
+ # Get element's own namespace (from mapping's namespace_class)
504
+ element_namespace = mapping&.namespace_class
505
+
506
+ # Process namespace_scope configurations
507
+ needs.namespace_scope_configs.each do |scope_config|
508
+ ns_class = scope_config.namespace_class
509
+ next if ns_class == element_namespace
510
+
511
+ ns_usage = needs.namespace(ns_class.to_key)
512
+ should_declare = scope_config.always_mode? ||
513
+ (scope_config.auto_mode? && ns_usage&.used_in&.any?)
514
+
515
+ if should_declare
516
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
517
+ hoisted[prefix] = ns_class.uri
518
+ end
519
+ end
520
+
521
+ # Add element's own namespace (if not already added)
522
+ if element_namespace && !hoisted.value?(element_namespace.uri)
523
+ use_prefix_option = options[:use_prefix]
524
+
525
+ case use_prefix_option
526
+ when true
527
+ prefix = element_namespace.prefix_default || "ns#{hoisted.keys.length}"
528
+ hoisted[prefix] = element_namespace.uri
529
+ when String
530
+ hoisted[use_prefix_option] = element_namespace.uri
531
+ when false
532
+ hoisted[nil] = element_namespace.uri
533
+ else
534
+ # Default: prefer default format (cleaner)
535
+ hoisted[nil] = element_namespace.uri
536
+ end
537
+ end
538
+
539
+ # Add type namespaces
540
+ # Type attribute namespaces
541
+ needs.type_attribute_namespaces.each do |ns_class|
542
+ ns_uri = ns_class.uri
543
+ next if hoisted.value?(ns_uri)
544
+
545
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
546
+ hoisted[prefix] = ns_class.uri
547
+ end
548
+
549
+ # Type element namespaces
550
+ needs.type_element_namespaces.each do |ns_class|
551
+ ns_uri = ns_class.uri
552
+ next if hoisted.value?(ns_uri)
553
+
554
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
555
+ hoisted[prefix] = ns_class.uri
556
+ end
557
+
558
+ # Add remaining namespaces
559
+ needs.all_namespace_classes.each do |ns_class|
560
+ ns_uri = ns_class.uri
561
+ next if hoisted.value?(ns_uri)
562
+
563
+ scope_config = needs.scope_config_for(ns_class)
564
+ next if scope_config
565
+
566
+ hoisted[nil] = ns_class.uri
567
+ end
568
+
569
+ hoisted
570
+ end
571
+
572
+ # Build hoisted declarations for a child element
573
+ #
574
+ # When a child element has attributes with type namespaces, those namespaces
575
+ # must be declared on the child element itself (W3C compliance).
576
+ #
577
+ # @param attr_def [Attribute] The attribute definition for the child
578
+ # @param needs [NamespaceNeeds] Namespace needs
579
+ # @param options [Hash] Serialization options
580
+ # @return [Hash<String|nil, String>] Hoisted declarations {prefix => uri}
581
+ def build_child_hoisted_declarations(attr_def, needs, _options,
582
+ mapper_class: nil)
583
+ hoisted = {}
584
+
585
+ # Get the child's own namespace needs
586
+ child_needs = needs.child(attr_def.name)
587
+ return hoisted unless child_needs
588
+
589
+ # Add type attribute namespaces for the child element
590
+ # CRITICAL: Type attribute namespaces MUST use prefix format (W3C rule)
591
+ # NOTE: We only add type ATTRIBUTE namespaces, not type ELEMENT namespaces.
592
+ # Type element namespaces are used by child elements and should be declared
593
+ # on the parent element (or root, depending on namespace_scope).
594
+ child_needs.type_attribute_namespaces.each do |ns_class|
595
+ ns_uri = ns_class.uri
596
+ next if hoisted.value?(ns_uri)
597
+
598
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
599
+ hoisted[prefix] = ns_class.uri
600
+ end
601
+
602
+ # Get child's element namespace if available
603
+ child_type = attr_def.type(mapper_class ? register_for(mapper_class) : @register)
604
+ if child_type.is_a?(Class) && child_type < Lutaml::Model::Serialize
605
+ child_reg = register_for(child_type)
606
+ child_mapping = child_type.mappings_for(:xml, child_reg)
607
+ if child_mapping&.namespace_class
608
+ element_namespace = child_mapping.namespace_class
609
+ # Only add if not already present
610
+ unless hoisted.value?(element_namespace.uri)
611
+ # For child elements, prefer default format (cleaner)
612
+ hoisted[nil] = element_namespace.uri
613
+ end
614
+ end
615
+ end
616
+
617
+ hoisted
618
+ end
619
+
620
+ # Build ElementNode for an XmlElement (recursive)
621
+ #
622
+ # @param xml_element [XmlDataModel::XmlElement] Element to plan
623
+ # @param mapping [Xml::Mapping] XML mapping
624
+ # @param needs [NamespaceNeeds] Namespace needs
625
+ # @param options [Hash] Serialization options (may contain :input_formats)
626
+ # @param parent_node [ElementNode, nil] Parent element node
627
+ # @param is_root [Boolean] Whether this is the root element
628
+ # @param parent_format [Symbol, nil] Parent's namespace format (:default or :prefix)
629
+ # @param parent_namespace_class [Class, nil] Parent's namespace class
630
+ # @param parent_namespace_prefix [String, nil] Parent's actual namespace prefix
631
+ # @param parent_hoisted [Hash] Namespaces hoisted on parent {prefix => uri}
632
+ # @return [ElementNode] Element node with all decisions
633
+ def build_element_node(xml_element, mapping, needs, options,
634
+ parent_node: nil, is_root: false, parent_format: nil, parent_namespace_class: nil, parent_namespace_prefix: nil, parent_hoisted: {}, element_path: [])
635
+ # Determine element's prefix (checks input_formats, parent context for preservation)
636
+ # Priority:
637
+ # 1. Lutaml::Xml::XmlElement (from parsed XML): has namespace_prefix_explicit
638
+ # 2. DataModel::XmlElement (from serialization): has @__xml_namespace_prefix
639
+ element_prefix_explicit = xml_element.is_a?(Lutaml::Xml::XmlElement) &&
640
+ xml_element.namespace_prefix_explicit
641
+ # For ROOT elements: don't use @__xml_namespace_prefix from XmlElement.
642
+ # The root's prefix should be determined by the DecisionEngine (model's prefix_default).
643
+ # This ensures mixed content roots use their namespace's default prefix, not the input prefix.
644
+ # For NESTED elements: use @__xml_namespace_prefix if set (supports doubly-defined namespaces).
645
+ element_used_prefix = if is_root
646
+ nil
647
+ elsif xml_element.is_a?(Lutaml::Xml::XmlElement)
648
+ xml_element.namespace_prefix
649
+ else
650
+ xml_element.xml_namespace_prefix
651
+ end
652
+
653
+ element_prefix = determine_element_prefix(
654
+ xml_element, mapping, needs, options,
655
+ is_root: is_root,
656
+ parent_format: parent_format,
657
+ parent_namespace_class: parent_namespace_class,
658
+ parent_namespace_prefix: parent_namespace_prefix,
659
+ parent_hoisted: parent_hoisted,
660
+ element_prefix_explicit: element_prefix_explicit,
661
+ element_used_prefix: element_used_prefix
662
+ )
663
+
664
+ # Determine hoisted xmlns declarations at this element
665
+ # CRITICAL: Pass element_prefix to avoid calling determine_element_prefix twice
666
+ # which could return different results due to context differences
667
+ hoisted = determine_hoisted_declarations(xml_element, mapping, needs,
668
+ options, is_root: is_root, parent_hoisted: parent_hoisted, element_prefix: element_prefix, element_path: element_path, parent_namespace_class: parent_namespace_class, parent_namespace_prefix: parent_namespace_prefix)
669
+
670
+ # For ROOT element with a stored input plan, merge additional declarations
671
+ # from the stored plan that the normal computation didn't include.
672
+ # This preserves namespace prefix declarations from input XML that
673
+ # the recomputation would not produce. Specifically:
674
+ # - Doubly-defined: xmlns="..." AND xmlns:b="..." on the same element
675
+ # - Non-default prefix: xmlns:xyzabc="..." when model uses prefix_default "a:"
676
+ #
677
+ # Only merge when the root element itself did NOT use a prefix in the input.
678
+ # When the root's element name had a prefix (e.g., <examplecom:schema>),
679
+ # the deserialization sets @__xml_namespace_prefix on the root XmlElement,
680
+ # and element_builder clears child prefixes — so the stored plan's
681
+ # prefix declarations are NOT needed by any child element.
682
+ if is_root && !options.key?(:use_prefix)
683
+ stored_plan = options[:stored_xml_declaration_plan]
684
+ if stored_plan.is_a?(Lutaml::Xml::DeclarationPlan) && stored_plan.root_node.is_a?(Lutaml::Xml::DeclarationPlan::ElementNode)
685
+ root_has_input_prefix = xml_element.xml_namespace_prefix.to_s != ""
686
+
687
+ unless root_has_input_prefix
688
+ stored_hoisted = stored_plan.root_node.hoisted_declarations
689
+ stored_original_uris = stored_plan.original_namespace_uris || {}
690
+
691
+ stored_hoisted.each do |prefix, uri|
692
+ effective_uri = stored_original_uris[uri] || uri
693
+ already_present = hoisted.any? do |_k, v|
694
+ v == effective_uri || v == uri
695
+ end
696
+ if already_present && !hoisted.key?(prefix)
697
+ hoisted[prefix] = uri
698
+ end
699
+ end
700
+ end
701
+ end
702
+ end
703
+ # Determine if child needs xmlns="" (W3C compliance)
704
+ # W3C XML Namespaces 1.0 §6.2: When parent has default namespace and child
705
+ # has NO namespace (namespace_class is nil), child MUST explicitly opt out
706
+ # with xmlns="" to prevent inheriting parent's default namespace.
707
+ #
708
+ # This applies when:
709
+ # 1. Transformation marked the element as needing xmlns="" (explicit :blank)
710
+ # 2. OR element has no namespace AND parent uses default format AND the default
711
+ # namespace does NOT have element_form_default :qualified
712
+ #
713
+ # Note: The transformation sets @needs_xmlns_blank on the XmlElement when the child
714
+ # model has explicit `namespace :blank` declaration.
715
+ element_marked_blank = xml_element.is_a?(Lutaml::Xml::DataModel::XmlElement) && xml_element.needs_xmlns_blank
716
+
717
+ # Add xmlns="" when parent's effective namespace form is :qualified.
718
+ # This implements the W3C XML Schema default behavior:
719
+ # - element_form_default :qualified (explicit) → children with no namespace get xmlns=""
720
+ # to opt out of parent's default namespace
721
+ # - element_form_default :unqualified (explicit) → children inherit parent's ns, no xmlns=""
722
+ # - not set (nil) → W3C default is :qualified, BUT children opt out when parent's
723
+ # namespace doesn't have an explicit form set (nested conflict case)
724
+ #
725
+ # The parent's effective namespace form (default_ns_form) is:
726
+ # - nil when no form was explicitly set on the parent's namespace
727
+ # - :qualified when explicitly set to qualified
728
+ # - :unqualified when explicitly set to unqualified
729
+ default_ns_form = options[:default_ns_element_form_default]
730
+ parent_ns_class = parent_namespace_class
731
+
732
+ # Skip xmlns="" when parent explicitly set :unqualified.
733
+ # Override original default_ns_form.nil? with parent_explicitly_unqualified check.
734
+ parent_explicitly_unqualified = parent_ns_class&.element_form_default_set? &&
735
+ parent_ns_class.element_form_default == :unqualified
736
+
737
+ # Original: default_ns_form.nil? (W3C default is :qualified, opt out with xmlns="")
738
+ # Override: if parent explicitly set :unqualified, don't opt out (inherit parent's ns)
739
+ implicit_blank_needs_xmlns = xml_element.namespace_class.nil? &&
740
+ parent_hoisted&.key?(nil) &&
741
+ default_ns_form.nil? && !parent_explicitly_unqualified
742
+
743
+ child_needs_xmlns_blank = element_marked_blank || implicit_blank_needs_xmlns
744
+
745
+ # Build schema_location_attr if root element and any namespace has schema_location
746
+ schema_location_attr = nil
747
+ if is_root
748
+ schema_location_attr = build_schema_location_attr_for_needs(needs)
749
+ end
750
+
751
+ # Create ElementNode
752
+ element_node = DeclarationPlan::ElementNode.new(
753
+ qualified_name: build_qualified_element_name(xml_element,
754
+ element_prefix),
755
+ use_prefix: element_prefix,
756
+ hoisted_declarations: hoisted,
757
+ needs_xmlns_blank: child_needs_xmlns_blank,
758
+ schema_location_attr: schema_location_attr,
759
+ )
760
+
761
+ # Calculate this element's format for passing to children
762
+ this_format = element_prefix.nil? ? :default : :prefix
763
+ this_namespace = xml_element.namespace_class
764
+
765
+ # Get this element's element_form_default for passing to children
766
+ # This determines whether child elements inherit this element's namespace
767
+ this_element_form_default = this_namespace&.element_form_default
768
+
769
+ # Track the effective default namespace's element_form_default
770
+ # This is used to determine if children should inherit the default namespace
771
+ # If this element declares a default namespace (hoisted[nil]), use its element_form_default
772
+ # ONLY if it was explicitly set. Otherwise use the parent's effective value.
773
+ effective_default_ns_form = if hoisted.key?(nil) && this_namespace&.element_form_default_set?
774
+ this_namespace.element_form_default
775
+ else
776
+ options[:default_ns_element_form_default]
777
+ end
778
+
779
+ # Plan ALL attributes (PRESERVES ORDER)
780
+ xml_element.attributes.each do |xml_attr|
781
+ attr_node = plan_attribute(xml_attr, xml_element, mapping, options)
782
+ element_node.add_attribute_node(attr_node)
783
+ end
784
+
785
+ # Get mapper_class from options to match children to mapping rules
786
+ mapper_class = options[:mapper_class]
787
+ attributes = if mapper_class.is_a?(Class) && mapper_class.include?(Lutaml::Model::Serialize)
788
+ mapper_class.attributes
789
+ else
790
+ {}
791
+ end
792
+
793
+ # Recursively plan ALL children (PRESERVES ORDER, mark as NOT root, pass parent context)
794
+ xml_element.children.each do |xml_child|
795
+ next unless xml_child.is_a?(Lutaml::Xml::DataModel::XmlElement)
796
+
797
+ # Match child XmlElement to its mapping rule to get correct child mapping
798
+ child_name = xml_child.name
799
+ matching_rule = mapping.elements.find do |rule|
800
+ rule.name.to_s == child_name
801
+ end
802
+
803
+ child_mapping = mapping # Default to parent mapping
804
+ child_options = options
805
+
806
+ if matching_rule && attributes.any?
807
+ # Get child's mapper_class and mapping
808
+ attr_def = attributes[matching_rule.to]
809
+ if attr_def
810
+ child_reg = mapper_class ? register_for(mapper_class) : @register
811
+ child_type = attr_def.type(child_reg)
812
+ if child_type.is_a?(Class) && child_type < Lutaml::Model::Serialize
813
+ child_type_reg = register_for(child_type)
814
+ child_mapping_obj = child_type.mappings_for(:xml,
815
+ child_type_reg)
816
+ if child_mapping_obj
817
+ child_mapping = child_mapping_obj
818
+ # CRITICAL: Pass parent's mapping so child can find its attribute name
819
+ # NOTE: do NOT propagate use_prefix to child elements
820
+ # use_prefix only applies to root element, not to children
821
+ # Children should use their own namespace's default presentation
822
+ use_prefix_value = options[:use_prefix]
823
+ base_child_options = {
824
+ mapper_class: child_type,
825
+ parent_mapping: mapping,
826
+ parent_element_form_default: this_element_form_default,
827
+ default_ns_element_form_default: effective_default_ns_form,
828
+ }
829
+ if use_prefix_value == true
830
+ # Do NOT propagate use_prefix: true to children
831
+ # Children should use their own namespace's default presentation
832
+ elsif use_prefix_value.is_a?(String)
833
+ # Custom string prefix is specific to root's namespace - don't propagate
834
+ else
835
+ # use_prefix: false or nil - don't propagate
836
+ end
837
+ child_options = options.except(:use_prefix).merge(base_child_options)
838
+ end
839
+ end
840
+ end
841
+ end
842
+
843
+ child_node = build_element_node(
844
+ xml_child, child_mapping, needs, child_options,
845
+ parent_node: element_node,
846
+ is_root: false,
847
+ parent_format: this_format,
848
+ parent_namespace_class: this_namespace,
849
+ parent_namespace_prefix: element_prefix,
850
+ parent_hoisted: parent_hoisted.merge(hoisted),
851
+ element_path: element_path + [xml_child.name]
852
+ )
853
+ element_node.add_element_node(child_node)
854
+ end
855
+
856
+ element_node
857
+ end
858
+
859
+ # Plan attribute prefix using W3C attributeFormDefault semantics
860
+ #
861
+ # W3C Rules (MECE):
862
+ # 1. Same namespace + unqualified → NO prefix
863
+ # 2. Different namespace OR qualified → YES prefix
864
+ # 3. Only attribute has namespace → YES prefix
865
+ # 4. No namespace but qualified → inherit element prefix
866
+ # 5. No namespace, unqualified → NO prefix (W3C default)
867
+ #
868
+ # @param xml_attr [XmlDataModel::XmlAttribute] Attribute to plan
869
+ # @param xml_element [XmlDataModel::XmlElement] Parent element
870
+ # @param mapping [Xml::Mapping] XML mapping
871
+ # @param options [Hash] Serialization options
872
+ # @return [AttributeNode] Attribute decision node
873
+ def plan_attribute(xml_attr, xml_element, mapping, options)
874
+ attr_ns_class = xml_attr.namespace_class
875
+ element_ns_class = xml_element.namespace_class
876
+
877
+ # Get W3C attributeFormDefault setting
878
+ attribute_form_default = element_ns_class&.attribute_form_default || :unqualified
879
+
880
+ # Compare by URI since namespace classes can be different instances
881
+ # with the same URI (e.g., dynamically created in tests with Class.new)
882
+ same_namespace = attr_ns_class && element_ns_class &&
883
+ attr_ns_class.uri == element_ns_class.uri
884
+
885
+ # Get the attribute's mapping rule to check form option
886
+ mapper_class = options[:mapper_class]
887
+ attr_mapping_rule = nil
888
+ if mapper_class.is_a?(Class) && mapper_class.include?(Lutaml::Model::Serialize)
889
+ attrs = mapper_class.attributes
890
+ attr_def = attrs[xml_attr.name.to_sym] || attrs[xml_attr.name.to_s]
891
+ if attr_def
892
+ attr_mapping_rule = mapping.attributes.find do |r|
893
+ r.to == attr_def.name
894
+ end
895
+ end
896
+ end
897
+
898
+ # W3C Attribute Prefix Decision (MECE)
899
+ # Priority: form option on mapping > same_namespace + attribute_form_default > type namespace
900
+ use_prefix = if attr_mapping_rule&.form == :qualified
901
+ # Priority 1: Explicit form: :qualified → YES prefix
902
+ attr_ns_class&.prefix_default || element_ns_class&.prefix_default
903
+ elsif same_namespace && attribute_form_default == :unqualified
904
+ # Priority 2: Same namespace + unqualified → NO prefix
905
+ nil
906
+ elsif attr_ns_class && element_ns_class
907
+ # Priority 3: Different namespace OR qualified → YES prefix
908
+ attr_ns_class.prefix_default
909
+ elsif attr_ns_class
910
+ # Priority 4: Only attribute has namespace → YES prefix
911
+ attr_ns_class.prefix_default
912
+ elsif attribute_form_default == :qualified
913
+ # Priority 5: No namespace but qualified → inherit element prefix
914
+ # CRITICAL: xmlns declarations (xmlns, xmlns:*) must NEVER have a prefix.
915
+ # The "xmlns" part is part of the declaration syntax, not a namespace prefix.
916
+ # These declarations are handled separately by the XML processor.
917
+ # Also, xsi:* attributes (xsi:schemaLocation, xsi:type, xsi:nil) must
918
+ # ALWAYS use the "xsi" prefix. They should never inherit element prefix.
919
+ # They conventionally belong to the XSI namespace.
920
+ attr_name = xml_attr.name.to_s
921
+ if attr_name.start_with?("xmlns", "xsi:", "xml:")
922
+ nil
923
+ else
924
+ element_ns_class&.prefix_default
925
+ end
926
+ else
927
+ # Priority 6: No namespace, unqualified → NO prefix (W3C default)
928
+ nil
929
+ end
930
+
931
+ DeclarationPlan::AttributeNode.new(
932
+ local_name: xml_attr.name,
933
+ use_prefix: use_prefix,
934
+ namespace_uri: attr_ns_class&.uri,
935
+ )
936
+ end
937
+
938
+ # Determine element's prefix using the OOP decision system
939
+ #
940
+ # Uses ElementPrefixResolver instead of procedural if-else chain.
941
+ #
942
+ # @param xml_element [XmlDataModel::XmlElement] Element
943
+ # @param mapping [Xml::Mapping] XML mapping
944
+ # @param needs [NamespaceNeeds] Namespace needs
945
+ # @param options [Hash] Serialization options
946
+ # @param is_root [Boolean] Whether this is the root element
947
+ # @param parent_format [Symbol, nil] Parent's format (:prefix or :default)
948
+ # @param parent_namespace_class [Class, nil] Parent's namespace class
949
+ # @param parent_hoisted [Hash] Namespaces hoisted on parent {prefix => uri}
950
+ # @return [String, nil] The prefix to use, or nil for default format
951
+ def find_type_namespace_for_element(xml_element, mapping, needs,
952
+ options)
953
+ # Get mapper_class from options
954
+ mapper_class = options[:mapper_class]
955
+ return nil unless mapper_class
956
+
957
+ # Get attributes from mapper_class
958
+ attributes = if mapper_class.is_a?(Class) && mapper_class.include?(Lutaml::Model::Serialize)
959
+ mapper_class.attributes
960
+ else
961
+ {}
962
+ end
963
+ return nil unless attributes.any?
964
+
965
+ # Find matching element rule
966
+ element_name = xml_element.name.to_s
967
+ matching_rule = mapping.elements.find do |rule|
968
+ rule.name.to_s == element_name
969
+ end
970
+ return nil unless matching_rule
971
+
972
+ # Get attribute definition
973
+ attr_def = attributes[matching_rule.to]
974
+ return nil unless attr_def
975
+
976
+ # Look up type namespace from needs.type_namespaces
977
+ needs.type_namespaces[attr_def&.name]
978
+ end
979
+
980
+ def determine_element_prefix(xml_element, mapping, needs, options,
981
+ is_root: false,
982
+ parent_format: nil,
983
+ parent_namespace_class: nil,
984
+ parent_namespace_prefix: nil,
985
+ parent_hoisted: {},
986
+ element_prefix_explicit: false,
987
+ element_used_prefix: nil)
988
+ # CRITICAL: Check for Type namespace FIRST
989
+ # Type namespaces are declared on Type::Value subclasses and used as
990
+ # prefixes on child elements. They take precedence over element namespace.
991
+ type_ns_class = find_type_namespace_for_element(xml_element, mapping,
992
+ needs, options)
993
+ if type_ns_class
994
+ # If parent uses default format AND Type namespace matches parent's namespace,
995
+ # inherit parent's namespace (return nil for prefix)
996
+ if parent_format == :default && parent_namespace_class&.uri == type_ns_class.uri
997
+ return nil
998
+ end
999
+
1000
+ # CRITICAL: Check if parent hoisted this type namespace with a custom prefix
1001
+ # When user specifies prefix: "custom", the parent hoists the namespace
1002
+ # with that custom prefix, and child elements must use the same prefix
1003
+ parent_prefix = parent_hoisted.find do |_prefix, uri|
1004
+ uri == type_ns_class.uri
1005
+ end&.first
1006
+ if parent_prefix
1007
+ return parent_prefix
1008
+ end
1009
+
1010
+ # CRITICAL: Check if user specified an explicit custom prefix option
1011
+ # This handles the case where parent hasn't hoisted yet AND the type namespace
1012
+ # matches the parent's namespace (i.e., they share the same namespace)
1013
+ use_prefix_option = options[:use_prefix]
1014
+ if use_prefix_option && parent_namespace_class && parent_namespace_class.uri == type_ns_class.uri
1015
+ # Parent has the same namespace as the type namespace
1016
+ # Use the prefix option to maintain consistency
1017
+ case use_prefix_option
1018
+ when String
1019
+ return use_prefix_option
1020
+ when true
1021
+ return parent_namespace_class.prefix_default
1022
+ when false
1023
+ return nil
1024
+ end
1025
+ end
1026
+
1027
+ # Default: use the Type namespace's prefix_default
1028
+ return type_ns_class.prefix_default
1029
+ end
1030
+
1031
+ # If no Type namespace, check element's own namespace
1032
+ return nil unless xml_element.namespace_class
1033
+
1034
+ # NEW: If element used explicit prefix during deserialization, use that prefix.
1035
+ # This handles doubly-defined namespaces where <a:item> and <b:item> both
1036
+ # map to same URI but need different prefixes.
1037
+ # Check: (a) from XmlElement (root), (b) from NamespaceUsage (children)
1038
+ used_prefix = element_used_prefix
1039
+ unless used_prefix
1040
+ # Look up from NamespaceUsage in needs (set during collection from model instance).
1041
+ # CRITICAL: Only use NamespaceUsage.used_prefix when the XmlElement is a
1042
+ # Lutaml::Xml::XmlElement wrapper (from original parsed XML, has namespace_prefix_explicit).
1043
+ # Do NOT use it when the XmlElement is a DataModel::XmlElement (from transformation)
1044
+ # because NamespaceUsage.used_prefix may have been set from a parent/r sibling element.
1045
+ # For DataModel::XmlElement, the prefix must come from @__xml_namespace_prefix on the
1046
+ # XmlElement itself (which is set during transformation for doubly-defined case).
1047
+ ns_key = xml_element.namespace_class.to_key
1048
+ ns_usage = needs.namespaces[ns_key]
1049
+ ns_from_wrapper = xml_element.is_a?(Lutaml::Xml::XmlElement) &&
1050
+ xml_element.namespace_prefix_explicit
1051
+ if ns_from_wrapper
1052
+ used_prefix = ns_usage&.used_prefix
1053
+ end
1054
+ end
1055
+
1056
+ # For the ROOT element: always use DecisionEngine (model's prefix_default).
1057
+ # The root's format should be determined by the namespace's default, not the input.
1058
+ # This prevents mixed content roots from using input prefixes like "examplecom:".
1059
+ #
1060
+ # For CHILD elements: use input prefix when it differs from model default.
1061
+ # This handles doubly-defined namespaces where input uses "xyzabc:" but
1062
+ # the input uses a different prefix than the model defines (e.g., input has
1063
+ # "xyzabc:" but model has prefix_default "a:").
1064
+ #
1065
+ # When used_prefix == model_default_prefix, fall through to DecisionEngine
1066
+ # to preserve the original format decision (default vs prefix).
1067
+ if used_prefix && !is_root
1068
+ ns_class = xml_element.namespace_class
1069
+ model_default_prefix = ns_class.prefix_default
1070
+
1071
+ # Only use used_prefix when it differs from model default
1072
+ # This preserves arbitrary input prefixes (xyzabc:) while allowing
1073
+ # the DecisionEngine to decide format when input matches model default.
1074
+ if model_default_prefix.nil? || used_prefix != model_default_prefix
1075
+ return used_prefix
1076
+ end
1077
+ end
1078
+
1079
+ # Use the OOP decision resolver
1080
+ @prefix_resolver ||= Decisions::ElementPrefixResolver.new
1081
+ # The decision is a pure function of the walk-invariant facts:
1082
+ # static per-model objects (mapping, needs, namespace class),
1083
+ # the element's own spelling, the parent-chain inputs, and the
1084
+ # two projections of the mutating parent_hoisted set that the
1085
+ # rules can see for this element's namespace. Same-shaped
1086
+ # elements repeat by the thousands in real documents, so the
1087
+ # memoized verdict replaces the 12-rule engine execution per
1088
+ # element (lutaml-model#1064). Per-parse memo: the planner is
1089
+ # built once per serialize run, so entries die with the parse.
1090
+ memo_key = [
1091
+ mapping, needs,
1092
+ xml_element.namespace_class, xml_element.name,
1093
+ input_prefix_of(xml_element), element_used_prefix,
1094
+ is_root, parent_format, parent_namespace_class,
1095
+ parent_namespace_prefix,
1096
+ hoisted_facts_for(xml_element, parent_hoisted),
1097
+ options[:use_prefix],
1098
+ options[:input_formats].object_id,
1099
+ options[:stored_xml_declaration_plan].object_id
1100
+ ]
1101
+ (@element_prefix_memo ||= {})[memo_key] ||= @prefix_resolver.resolve_with_decision(
1102
+ xml_element, mapping, needs, options,
1103
+ is_root: is_root,
1104
+ parent_format: parent_format,
1105
+ parent_namespace_class: parent_namespace_class,
1106
+ parent_namespace_prefix: parent_namespace_prefix,
1107
+ parent_hoisted: parent_hoisted,
1108
+ element_used_prefix: element_used_prefix
1109
+ ).prefix
1110
+ end
1111
+
1112
+ # The element's own input prefix, the same lookup
1113
+ # DecisionContext#element_namespace_prefix performs.
1114
+ def input_prefix_of(xml_element)
1115
+ return nil unless xml_element
1116
+
1117
+ if xml_element.is_a?(Lutaml::Xml::XmlElement)
1118
+ xml_element.namespace_prefix
1119
+ else
1120
+ prefix = xml_element.xml_namespace_prefix
1121
+ return prefix if prefix && !prefix.empty?
1122
+
1123
+ original = xml_element.original_xml_element
1124
+ original.is_a?(Lutaml::Xml::XmlElement) ? original.namespace_prefix : nil
1125
+ end
1126
+ end
1127
+
1128
+ # The projections of the walk-mutating parent_hoisted set that the
1129
+ # decision rules read for this element's namespace: the prefix
1130
+ # (if any) this namespace is hoisted under, and whether the
1131
+ # parent's default namespace matches this element's namespace.
1132
+ def hoisted_facts_for(xml_element, parent_hoisted)
1133
+ ns_class = xml_element&.namespace_class
1134
+ uri = ns_class&.uri
1135
+ return [nil, false] unless uri
1136
+
1137
+ all_uris = if ns_class.is_a?(Class) && ns_class < Lutaml::Xml::Namespace
1138
+ ns_class.all_uris
1139
+ else
1140
+ [uri]
1141
+ end
1142
+
1143
+ hoisted_prefix = nil
1144
+ parent_hoisted.each do |prefix, hoisted_uri|
1145
+ if all_uris.include?(hoisted_uri)
1146
+ hoisted_prefix = prefix
1147
+ break
1148
+ end
1149
+ end
1150
+ default_matches = parent_hoisted.key?(nil) && all_uris.include?(parent_hoisted[nil])
1151
+ [hoisted_prefix, default_matches]
1152
+ end
1153
+
1154
+ # Build qualified element name with prefix
1155
+ #
1156
+ # @param xml_element [XmlDataModel::XmlElement] Element
1157
+ # @param prefix [String, nil] Prefix (nil = no prefix)
1158
+ # @return [String] Qualified name: "prefix:name" or "name"
1159
+ def build_qualified_element_name(xml_element, prefix)
1160
+ if prefix
1161
+ "#{prefix}:#{xml_element.name}"
1162
+ else
1163
+ xml_element.name
1164
+ end
1165
+ end
1166
+
1167
+ # Determine which xmlns declarations to hoist at this element
1168
+ #
1169
+ # Checks input_formats to preserve input format during round-trip.
1170
+ #
1171
+ # @param xml_element [XmlDataModel::XmlElement] Element
1172
+ # @param mapping [Xml::Mapping] XML mapping
1173
+ # @param needs [NamespaceNeeds] Namespace needs
1174
+ # @param options [Hash] Serialization options (may contain :input_formats)
1175
+ # @param is_root [Boolean] Whether this is the root element
1176
+ # @param parent_hoisted [Hash] Namespaces hoisted on parent {prefix => uri}
1177
+ # @param element_path [Array<String>] Element path in the tree for namespace_locations lookup
1178
+ # @param parent_namespace_class [Class, nil] Parent's namespace class
1179
+ # @param parent_namespace_prefix [String, nil] Parent's actual namespace prefix
1180
+ # @return [Hash<String|nil, String>] xmlns attributes: {prefix_or_nil => uri}
1181
+ def determine_hoisted_declarations(xml_element, mapping, needs,
1182
+ options, is_root: false, parent_hoisted: {}, element_prefix: nil, element_path: [], parent_namespace_class: nil, parent_namespace_prefix: nil)
1183
+ hoisted = {}
1184
+
1185
+ # Compute element_used_prefix the same way it's done in build_element_node.
1186
+ # This is needed because determine_element_prefix is called again inside this method,
1187
+ # and it needs element_used_prefix to correctly determine the prefix.
1188
+ element_used_prefix = if is_root
1189
+ nil
1190
+ elsif xml_element.is_a?(Lutaml::Xml::XmlElement)
1191
+ xml_element.namespace_prefix
1192
+ else
1193
+ xml_element.xml_namespace_prefix
1194
+ end
1195
+
1196
+ # CRITICAL: Get the current element's namespace_scope_configs.
1197
+ # For child elements, use their own namespace_scope, not the parent's.
1198
+ current_scope_configs = get_element_namespace_scope_configs(
1199
+ xml_element, mapping, needs, options
1200
+ )
1201
+
1202
+ # Check if element's namespace is hoisted to root via namespace_scope
1203
+ element_ns_hoisted_to_root = false
1204
+ if xml_element.namespace_class
1205
+ scope_config = find_scope_config_for(xml_element.namespace_class,
1206
+ current_scope_configs)
1207
+ if scope_config
1208
+ ns_usage = needs.namespaces[xml_element.namespace_class.to_key]
1209
+ element_ns_hoisted_to_root = scope_config.always_mode? ||
1210
+ (scope_config.auto_mode? && ns_usage&.used_in&.any?)
1211
+ end
1212
+ end
1213
+
1214
+ # Check if element's namespace was already hoisted on parent (locally)
1215
+ if xml_element.namespace_class
1216
+ ns_uri = xml_element.namespace_class.uri
1217
+ parent_hoisted.value?(ns_uri)
1218
+ end
1219
+
1220
+ # FIRST: Add element's OWN namespace if it has one
1221
+ # This ensures the namespace is declared with the correct format (prefix or default)
1222
+ # even when it's already hoisted by parent, to maintain consistency.
1223
+ if xml_element.namespace_class
1224
+ ns_class = xml_element.namespace_class
1225
+ ns_uri = ns_class.uri
1226
+
1227
+ # Check if namespace_locations has an original URI that should be used instead.
1228
+ # namespace_locations is keyed by element path (e.g., "child/grandchild")
1229
+ # matching the key format from collect_element_namespaces in model_transform.rb.
1230
+ # Use element_path for lookup, not xml_element.name (which is just the local name).
1231
+ #
1232
+ # For round-trip fidelity, ALWAYS use the alias URI from stored plan when available.
1233
+ # The use_prefix option only affects element format (prefix vs default), not the URI.
1234
+ # Users expect round-trip to preserve the exact namespace URI from input.
1235
+ stored_plan = options[:stored_xml_declaration_plan]
1236
+ if stored_plan
1237
+ # For child elements, look up by element path
1238
+ # For root elements (empty path), use namespaces_at_path([]) to get root declarations
1239
+ loc_key = element_path.join("/")
1240
+ element_ns_loc = if is_root
1241
+ stored_plan.namespaces_at_path([])
1242
+ else
1243
+ stored_plan.namespace_locations&.dig(loc_key)
1244
+ end
1245
+ # Check if any URI in the stored location is an alias of the canonical URI
1246
+ element_ns_loc&.each_value do |stored_uri|
1247
+ if ns_class.is_alias?(stored_uri)
1248
+ # Use the original alias URI instead of canonical
1249
+ ns_uri = stored_uri
1250
+ break
1251
+ end
1252
+ end
1253
+ end
1254
+ # Check if namespace is hoisted on parent
1255
+ # When namespace has uri_aliases, the parent may have declared a different
1256
+ # URI (alias or canonical) than what this element wants to use.
1257
+ # Rules:
1258
+ # 1. Exact match → already hoisted, don't re-declare
1259
+ # 2. Parent declared alias, child wants canonical → already hoisted (alias covers canonical)
1260
+ # 3. Parent declared canonical/alias-A, child wants alias-B → NOT hoisted (child must declare its alias)
1261
+ # This ensures round-trip fidelity: if input used alias URI, output preserves it.
1262
+ ns_hoisted_by_parent = parent_hoisted.value?(ns_uri)
1263
+ if !ns_hoisted_by_parent && ns_class.is_a?(Class) && ns_class < Lutaml::Xml::Namespace &&
1264
+ ns_uri == ns_class.uri # child is using canonical URI
1265
+ # Parent declared an alias of this namespace — child doesn't need to re-declare
1266
+ # because the alias already establishes the namespace binding
1267
+ ns_hoisted_by_parent = ns_class.uri_aliases.any? do |alias_uri|
1268
+ parent_hoisted.value?(alias_uri)
1269
+ end
1270
+ end
1271
+
1272
+ # Check if namespace is hoisted to root via namespace_scope (for non-root elements)
1273
+ element_ns_hoisted_to_root && !is_root
1274
+
1275
+ # Determine the prefix to use for this namespace
1276
+ if !ns_hoisted_by_parent
1277
+ # Namespace is NOT already hoisted by parent - need to determine prefix
1278
+ element_prefix = determine_element_prefix(xml_element, mapping,
1279
+ needs, options, is_root: is_root, parent_hoisted: parent_hoisted, element_used_prefix: element_used_prefix, parent_namespace_class: parent_namespace_class, parent_namespace_prefix: parent_namespace_prefix)
1280
+
1281
+ # W3C elementFormDefault="unqualified": prefer prefix format so children
1282
+ # can be in blank namespace (no xmlns attribute). Only for non-root elements
1283
+ # since elementFormDefault only applies to local elements.
1284
+ # CRITICAL: Only applies when explicitly set, not when defaulted to :unqualified.
1285
+ if element_prefix.nil? && ns_class.element_form_default_set? &&
1286
+ ns_class.element_form_default == :unqualified && !is_root
1287
+ element_prefix = ns_class.prefix_default || "ns"
1288
+ end
1289
+
1290
+ hoisted[element_prefix] = ns_uri
1291
+ elsif element_prefix
1292
+ # Namespace is already hoisted by parent, and we have an explicit prefix
1293
+ # Check if parent has the SAME prefix declaration (accounting for URI aliases)
1294
+ parent_uri_for_prefix = parent_hoisted[element_prefix]
1295
+ all_uris = ns_class.is_a?(Class) && ns_class < Lutaml::Xml::Namespace ? ns_class.all_uris : [ns_uri]
1296
+ parent_has_same_prefix = parent_uri_for_prefix && all_uris.include?(parent_uri_for_prefix)
1297
+ if parent_has_same_prefix
1298
+ # Parent already declared this namespace with the same prefix - don't re-declare
1299
+ # Just keep track that we're using the parent's prefix (no need to add to hoisted)
1300
+ else
1301
+ # Parent has different prefix or no prefix - add our declaration
1302
+ hoisted[element_prefix] = ns_uri
1303
+ end
1304
+ else
1305
+ # Namespace is hoisted by parent, and we don't have an explicit prefix
1306
+ # W3C XML Namespaces 1.0 §6.2: Child elements inherit namespace from parent
1307
+ # DO NOT re-declare the namespace - the child will inherit it
1308
+ # This prevents redundant xmlns declarations on nested elements with same namespace
1309
+ end
1310
+
1311
+ # CRITICAL FIX: If root element uses default format (nil prefix) and child elements
1312
+ # need prefix format for the same namespace (due to form: :qualified), also declare
1313
+ # the namespace with prefix format on the root element.
1314
+ # This allows child elements to use the prefix without declaring it locally.
1315
+ # IMPORTANT: This only applies to the element's OWN namespace, not type namespaces.
1316
+ # Type namespaces should be declared locally on the element that uses the type.
1317
+ # Check if any child elements have form: :qualified for this namespace
1318
+ # by checking the children's form attribute in the XmlElement tree
1319
+ #
1320
+ # ADDITIONAL CHECK: Skip if namespace was NOT declared at root in input XML.
1321
+ # Use namespaces_at_path([]) to verify this namespace was actually at root level.
1322
+ # Note: namespace_locations only has child paths, not root. Root namespaces
1323
+ # are stored in root_node.hoisted_declarations, which namespaces_at_path returns.
1324
+ if is_root && hoisted.key?(nil) && hoisted[nil] == ns_uri && xml_element.is_a?(Lutaml::Xml::DataModel::XmlElement)
1325
+ stored_plan = options[:stored_xml_declaration_plan]
1326
+ root_ns_at_input = stored_plan&.namespaces_at_path([])
1327
+ namespace_was_at_root = if root_ns_at_input
1328
+ root_ns_at_input.value?(ns_uri)
1329
+ else
1330
+ true # No location data, assume it was at root
1331
+ end
1332
+
1333
+ if namespace_was_at_root
1334
+ child_needs_prefix = xml_element.children.any? do |child|
1335
+ next unless child.is_a?(Lutaml::Xml::DataModel::XmlElement)
1336
+
1337
+ # Check if child has form: :qualified and same namespace
1338
+ child.form == :qualified && child.namespace_class&.uri == ns_uri
1339
+ end
1340
+ if child_needs_prefix
1341
+ prefix = ns_class.prefix_default
1342
+ hoisted[prefix] = ns_uri
1343
+ end
1344
+ end
1345
+ end
1346
+ end
1347
+
1348
+ # SECOND: Add namespace_scope namespaces (ONLY at root!)
1349
+ if is_root
1350
+ stored_plan = options[:stored_xml_declaration_plan]
1351
+ current_scope_configs.each do |scope_config|
1352
+ ns_class = scope_config.namespace_class
1353
+ ns_uri = ns_class.uri
1354
+
1355
+ # Skip if already added (element's own namespace)
1356
+ next if ns_class == xml_element.namespace_class
1357
+
1358
+ # CRITICAL: Skip if namespace URI is already in hoisted hash
1359
+ # An element can declare the same URI with both default and prefix formats
1360
+ next if hoisted.value?(ns_uri) || hoisted.key?(ns_class.prefix_default)
1361
+
1362
+ # NEW: Skip if namespace was NOT declared at root in input XML.
1363
+ # Use namespace_locations to verify this namespace was actually at root level.
1364
+ # If namespace_locations shows it was only at child paths, do NOT hoist to root.
1365
+ root_ns_at_input = stored_plan&.namespaces_at_path([])
1366
+ if root_ns_at_input && !root_ns_at_input.value?(ns_uri)
1367
+ # Namespace was not declared at root in input - skip hoisting
1368
+ next
1369
+ end
1370
+
1371
+ # Check :always mode or :auto mode with usage
1372
+ ns_usage = needs.namespace(ns_class.to_key)
1373
+ should_declare_here = scope_config.always_mode? ||
1374
+ (scope_config.auto_mode? && ns_usage&.used_in&.any?)
1375
+
1376
+ if should_declare_here
1377
+ prefix = ns_class.prefix_default
1378
+ hoisted[prefix] = ns_uri
1379
+ end
1380
+ end
1381
+ end
1382
+
1383
+ # THIRD: Add type namespaces
1384
+ # Type namespaces are declared on PARENT elements and used by child elements.
1385
+ # W3C rule: Namespaces in attributes MUST use prefix format.
1386
+ # Type namespaces for elements MUST also be declared at root with prefix.
1387
+ # Example: ContactInfo declares xmlns:name for personName's name:prefix attribute.
1388
+ # Example: Document declares xmlns:dc for title's dc:title element.
1389
+ #
1390
+ # CRITICAL: Type namespaces respect namespace_scope directive.
1391
+ # When namespace_scope is configured, hoist type namespaces to root.
1392
+ # When namespace_scope is NOT configured, type namespaces are declared
1393
+ # locally on the elements that use them.
1394
+ if is_root
1395
+ # Check if namespace_scope is configured
1396
+ has_namespace_scope = !current_scope_configs.empty?
1397
+
1398
+ # Only hoist type namespaces to root when namespace_scope is configured
1399
+ if has_namespace_scope
1400
+ # Type attribute namespaces
1401
+ needs.type_attribute_namespaces.each do |ns_class|
1402
+ ns_uri = ns_class.uri
1403
+ next if hoisted.value?(ns_uri) # Skip if already declared
1404
+ next if ns_class == xml_element.namespace_class # Skip element's own namespace
1405
+
1406
+ # If namespace_scope is configured, only hoist if in scope
1407
+ scope_config = find_scope_config_for(ns_class,
1408
+ current_scope_configs)
1409
+ next unless scope_config
1410
+
1411
+ # Type attribute namespaces MUST use prefix format (W3C rule)
1412
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
1413
+ hoisted[prefix] = ns_uri
1414
+ end
1415
+
1416
+ # Type element namespaces
1417
+ needs.type_element_namespaces.each do |ns_class|
1418
+ ns_uri = ns_class.uri
1419
+ next if hoisted.value?(ns_uri) # Skip if already declared
1420
+ next if ns_class == xml_element.namespace_class # Skip element's own namespace
1421
+
1422
+ # CRITICAL: Type namespaces are different from child element namespaces.
1423
+ # Type namespaces are declared on Type::Value subclasses (via xml_namespace
1424
+ # directive) and MUST ALWAYS be hoisted to root with prefix format (W3C
1425
+ # constraint: only one default namespace per element).
1426
+ #
1427
+ # The restriction below applies ONLY to child element namespaces (from nested
1428
+ # models), NOT to Type namespaces. Type namespaces are about TYPE identity,
1429
+ # not about element structure.
1430
+ #
1431
+ # Examples of child element namespaces (should NOT be hoisted if different):
1432
+ # - Root has XMI namespace, child model has XMI_NEW namespace → child declares
1433
+ # - Root has NO namespace, child model has XMI namespace → child declares
1434
+ #
1435
+ # Examples of Type namespaces (should ALWAYS be hoisted):
1436
+ # - Root has any namespace, attribute type has XMI namespace → hoist XMI
1437
+ # - Root has NO namespace, attribute type has XMI namespace → hoist XMI
1438
+
1439
+ # If namespace_scope is configured, only hoist if in scope
1440
+ scope_config = find_scope_config_for(ns_class,
1441
+ current_scope_configs)
1442
+ next unless scope_config
1443
+
1444
+ # Type element namespaces MUST use prefix format
1445
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
1446
+ hoisted[prefix] = ns_uri
1447
+ end
1448
+ else
1449
+ # Root element without namespace_scope: hoist type namespaces
1450
+ # Type namespaces MUST use prefix format (W3C rule)
1451
+ #
1452
+ # CRITICAL: Don't hoist type namespaces that are also child element
1453
+ # namespaces. When a type namespace is also a child's element namespace, it
1454
+ # should be declared on that child element (not hoisted to root).
1455
+
1456
+ # Type element namespaces (for Type::Value classes used by root's attributes)
1457
+ needs.type_element_namespaces.each do |ns_class|
1458
+ ns_uri = ns_class.uri
1459
+ next if hoisted.value?(ns_uri) # Skip if already declared
1460
+ next if ns_class == xml_element.namespace_class # Skip element's own namespace
1461
+
1462
+ # Check if this namespace is used by any child element as its element namespace
1463
+ # If so, don't hoist - let the child declare it locally
1464
+ # UNLESS the parent also uses this namespace for its attributes
1465
+ child_uses_namespace = needs.children&.any? do |_attr_name, child_needs|
1466
+ child_needs.namespaces.any? do |_key, ns_usage|
1467
+ ns_usage.used_in_elements? && ns_usage.namespace_class.uri == ns_uri
1468
+ end
1469
+ end
1470
+
1471
+ # Only skip hoisting if child uses namespace AND parent doesn't use it for attributes
1472
+ # Check if parent element instance has attributes with this namespace
1473
+ parent_has_attr_with_ns = if xml_element.is_a?(Lutaml::Xml::DataModel::XmlElement)
1474
+ xml_element.attributes.any? do |xml_attr|
1475
+ next false unless xml_attr.namespace_class
1476
+
1477
+ xml_attr.namespace_class.uri == ns_uri
1478
+ end
1479
+ else
1480
+ # For nil/Class root_element, check if mapping has attributes with this namespace
1481
+ # This handles the case where parent doesn't have the attribute set
1482
+ # Only hoist if parent's MAPPING declares attributes with this namespace
1483
+ mapping.attributes.any? do |attr_rule|
1484
+ next false unless attr_rule.attribute?
1485
+
1486
+ # Get the mapper_class to find attribute definition
1487
+ mapper_class = options[:mapper_class] || mapping.owner
1488
+ next false unless mapper_class
1489
+ next false unless mapper_class.is_a?(Class) &&
1490
+ mapper_class.include?(Lutaml::Model::Serialize)
1491
+
1492
+ attr_def = mapper_class.attributes[attr_rule.to]
1493
+ next false unless attr_def
1494
+
1495
+ # Check if this attribute's type has the namespace
1496
+ type_ns_class = attr_def.type_namespace_class(register_for(mapper_class))
1497
+ type_ns_class&.uri == ns_uri
1498
+ end
1499
+ end
1500
+ next if child_uses_namespace && !parent_has_attr_with_ns
1501
+
1502
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
1503
+ hoisted[prefix] = ns_uri
1504
+ end
1505
+
1506
+ # Type attribute namespaces (for Type::Value classes used in child element attributes)
1507
+ # CRITICAL: Don't hoist if namespace is also child element's namespace
1508
+ needs.type_attribute_namespaces.each do |ns_class|
1509
+ ns_uri = ns_class.uri
1510
+ next if hoisted.value?(ns_uri) # Skip if already declared
1511
+ next if ns_class == xml_element.namespace_class # Skip element's own namespace
1512
+
1513
+ # Check if this namespace is used by any child element as its element namespace
1514
+ # If so, don't hoist - let the child declare it locally
1515
+ child_uses_namespace = needs.children&.any? do |_attr_name, child_needs|
1516
+ child_needs.namespaces.any? do |_key, ns_usage|
1517
+ ns_usage.used_in_elements? && ns_usage.namespace_class.uri == ns_uri
1518
+ end
1519
+ end
1520
+
1521
+ # Only skip hoisting if child uses namespace AND parent doesn't use it for attributes
1522
+ parent_has_attr_with_ns = if xml_element.is_a?(Lutaml::Xml::DataModel::XmlElement)
1523
+ xml_element.attributes.any? do |xml_attr|
1524
+ next false unless xml_attr.namespace_class
1525
+
1526
+ xml_attr.namespace_class.uri == ns_uri
1527
+ end
1528
+ else
1529
+ # For nil/Class root_element, check if mapping has attributes with this namespace
1530
+ mapping.attributes.any? do |attr_rule|
1531
+ next false unless attr_rule.attribute?
1532
+
1533
+ mapper_class = options[:mapper_class] || mapping.owner
1534
+ next false unless mapper_class
1535
+ next false unless mapper_class.is_a?(Class) &&
1536
+ mapper_class.include?(Lutaml::Model::Serialize)
1537
+
1538
+ attr_def = mapper_class.attributes[attr_rule.to]
1539
+ next false unless attr_def
1540
+
1541
+ type_ns_class = attr_def.type_namespace_class(register_for(mapper_class))
1542
+ type_ns_class&.uri == ns_uri
1543
+ end
1544
+ end
1545
+
1546
+ next if child_uses_namespace && !parent_has_attr_with_ns
1547
+
1548
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
1549
+ hoisted[prefix] = ns_uri
1550
+ end
1551
+ end
1552
+ else
1553
+ # For non-root (child) elements, add their own type namespaces
1554
+ # CRITICAL: Child elements with type namespace attributes need those
1555
+ # namespaces declared on themselves (W3C compliance)
1556
+ #
1557
+ # Get the child's own namespace needs by matching the current element
1558
+ # to its corresponding attribute name in the parent's mapping
1559
+ child_attr_name = find_child_attribute_name(xml_element, mapping,
1560
+ options)
1561
+ if child_attr_name
1562
+ child_needs = needs.child(child_attr_name)
1563
+ if child_needs
1564
+ # Add child's type element namespaces (for Type::Value attributes)
1565
+ # These need to be hoisted to the child element
1566
+ child_needs.type_element_namespaces.each do |ns_class|
1567
+ ns_uri = ns_class.uri
1568
+ next if hoisted.value?(ns_uri) # Skip if already declared
1569
+ next if parent_hoisted.value?(ns_uri) # Skip if parent already declared
1570
+ next if ns_class == xml_element.namespace_class # Skip element's own namespace
1571
+
1572
+ # Type namespaces MUST use prefix format (W3C rule)
1573
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
1574
+ hoisted[prefix] = ns_uri
1575
+ end
1576
+
1577
+ # Add child's type attribute namespaces
1578
+ # CRITICAL: Skip if namespace is child's own element namespace
1579
+ # The element's namespace declaration already covers it
1580
+ child_needs.type_attribute_namespaces.each do |ns_class|
1581
+ ns_uri = ns_class.uri
1582
+ next if hoisted.value?(ns_uri) # Skip if already declared
1583
+ next if parent_hoisted.value?(ns_uri) # Skip if parent already declared
1584
+ next if ns_class == xml_element.namespace_class # Skip element's own namespace
1585
+
1586
+ # Type attribute namespaces MUST use prefix format (W3C rule)
1587
+ prefix = ns_class.prefix_default || "ns#{hoisted.keys.length}"
1588
+ hoisted[prefix] = ns_uri
1589
+ end
1590
+ end
1591
+ end
1592
+ end
1593
+
1594
+ # FOURTH: Add namespaces NOT in namespace_scope (LOCAL hoisting)
1595
+ # W3C minimal-subtree principle: declare namespace at first element using it
1596
+ if !is_root
1597
+ needs.namespaces.each_value do |ns_usage|
1598
+ ns_class = ns_usage.namespace_class
1599
+
1600
+ # Skip if already added (element's own namespace)
1601
+ next if ns_class == xml_element.namespace_class
1602
+
1603
+ # Skip if already hoisted on parent (don't redeclare!)
1604
+ next if parent_hoisted.value?(ns_class.uri)
1605
+
1606
+ # CRITICAL: Skip if this namespace is ONLY used by child elements (not by
1607
+ # the current element). Child elements should declare their own namespace
1608
+ # locally using default format. But if the current element uses this namespace
1609
+ # for its attributes, it should be hoisted.
1610
+ # Check: if NOT used in attributes AND used in elements → skip hoisting
1611
+ # NOTE: Also check type_attribute_namespaces (attribute type namespaces)
1612
+ used_in_attributes = ns_usage.used_in_attributes? || needs.type_attribute_namespaces.include?(ns_class)
1613
+ next if !used_in_attributes && ns_usage.used_in_elements?
1614
+
1615
+ scope_config = find_scope_config_for(ns_class,
1616
+ current_scope_configs)
1617
+
1618
+ # Only hoist if NOT in namespace_scope (local hoisting)
1619
+ # Check if any child/grandchild uses this namespace
1620
+ if !scope_config && element_needs_namespace?(xml_element,
1621
+ ns_class)
1622
+ prefix = ns_class.prefix_default
1623
+ hoisted[prefix] = ns_class.uri
1624
+ end
1625
+ end
1626
+ end
1627
+
1628
+ # PRESERVATION: For root element, add stored input namespace declarations
1629
+ # that were declared AT ROOT in the original XML.
1630
+ # This includes namespaces like xmlns:xi that were in input at root level
1631
+ # but are not "needed" by model. These namespaces may be required for
1632
+ # downstream processing (XInclude, XSchema, etc.)
1633
+ #
1634
+ # For doubly-defined namespaces (same URI with different prefixes), we allow
1635
+ # each prefix variant to be preserved if used by children.
1636
+ #
1637
+ # CRITICAL: Only preserve namespaces that were ORIGINALLY declared at root.
1638
+ # Namespaces declared on child elements in the input should remain on children.
1639
+ # NOTE: PRESERVATION should always run when we have a stored plan, because
1640
+ # unused namespaces (like xmlns:xsi for schemaLocation) should be preserved
1641
+ # for round-trip fidelity regardless of prefix formatting options.
1642
+ stored_plan = options[:stored_xml_declaration_plan]
1643
+ if is_root && stored_plan
1644
+
1645
+ # Get root-level namespaces from location tracking.
1646
+ # This properly distinguishes namespaces declared at root vs hoisted there.
1647
+ root_level_namespaces = stored_plan&.namespaces_at_path([])
1648
+
1649
+ # Track namespaces that are in child paths (not root) in namespace_locations
1650
+ # These should NOT be on root in the input XML
1651
+ child_only_namespaces = {}
1652
+ stored_plan&.namespace_locations&.each do |path, ns_hash|
1653
+ next if path.empty? # Skip root path
1654
+
1655
+ ns_hash.each do |prefix, uri|
1656
+ child_only_namespaces[[prefix, uri]] = true
1657
+ end
1658
+ end
1659
+
1660
+ # Get namespace_locations to verify which namespaces were actually declared at root
1661
+ # in the input XML. This prevents hoisting namespaces that were declared on child
1662
+ # elements but got incorrectly added to root's hoisted during FIRST phase.
1663
+ input_root_ns = {}
1664
+ if stored_plan&.namespace_locations
1665
+ # Aggregate all namespaces from child paths
1666
+ child_ns = {}
1667
+ stored_plan.namespace_locations.each_value do |ns_hash|
1668
+ ns_hash.each do |prefix, uri|
1669
+ child_ns[[prefix, uri]] = true
1670
+ end
1671
+ end
1672
+ # Namespaces in root's hoisted but NOT in any child path were actually at root
1673
+ root_level_namespaces&.each do |prefix, uri|
1674
+ if prefix != :not_root && !child_ns.key?([
1675
+ prefix, uri
1676
+ ])
1677
+ input_root_ns[prefix] =
1678
+ uri
1679
+ end
1680
+ end
1681
+ end
1682
+
1683
+ if input_root_ns.any?
1684
+ root_level_namespaces = input_root_ns
1685
+ end
1686
+
1687
+ if root_level_namespaces
1688
+ root_level_namespaces.each do |prefix, uri|
1689
+ # Skip if same prefix already used for different URI
1690
+ next if hoisted.key?(prefix) && hoisted[prefix] != uri
1691
+
1692
+ # Skip if this namespace is a child element's OWN element namespace
1693
+ # (not a type namespace). Children should declare their own namespace
1694
+ # locally rather than having it hoisted to root.
1695
+ child_own_element_ns = needs.children&.any? do |_attr_name, child_needs|
1696
+ child_needs.namespaces.values.any? do |ns_usage|
1697
+ ns_usage.namespace_class.uri == uri &&
1698
+ ns_usage.used_in_elements? &&
1699
+ needs.type_namespace_classes.none? { |tc| tc.uri == uri }
1700
+ end
1701
+ end
1702
+ next if child_own_element_ns
1703
+
1704
+ # Check if children use this specific prefix variant
1705
+ # This enables doubly-defined namespace preservation
1706
+ child_uses_this_prefix = needs.children&.any? do |_attr_name, child_needs|
1707
+ child_needs.namespaces.values.any? do |ns_usage|
1708
+ ns_usage.used_prefix == prefix &&
1709
+ ns_usage.namespace_class.uri == uri
1710
+ end
1711
+ end
1712
+
1713
+ # Hoist if:
1714
+ # - URI not yet declared (no prefix+URI combo), OR
1715
+ # - This specific prefix variant is used by children (doubly-defined ns)
1716
+ uri_already_hoisted = hoisted.value?(uri)
1717
+ if !uri_already_hoisted || child_uses_this_prefix
1718
+ hoisted[prefix] = uri
1719
+ end
1720
+ end
1721
+ elsif stored_plan&.root_node&.hoisted_declarations
1722
+ # Fallback: Legacy behavior for plans without location data
1723
+ # Only add namespaces that are NOT needed by any child
1724
+ #
1725
+ # CRITICAL: If namespace_locations has child entries but root path is empty,
1726
+ # it means the root's own namespaces were NOT captured during parsing.
1727
+ # In this case, we should NOT add anything from fallback to avoid
1728
+ # incorrectly adding child namespaces to root.
1729
+ root_path_in_locations = stored_plan.namespace_locations&.key?("")
1730
+ if root_path_in_locations && stored_plan.namespace_locations[""].to_h.empty?
1731
+ # Root path exists but is empty - skip fallback to avoid adding child namespaces to root
1732
+ else
1733
+ stored_hoisted = stored_plan.root_node.hoisted_declarations
1734
+ stored_hoisted.each do |prefix, uri|
1735
+ # Skip if this namespace is in child_only_namespaces
1736
+ # (declared on a child element in the input)
1737
+ next if child_only_namespaces.key?([prefix, uri])
1738
+
1739
+ # Skip if same prefix already used for different URI
1740
+ next if hoisted.key?(prefix) && hoisted[prefix] != uri
1741
+
1742
+ # Check if any child element needs this namespace
1743
+ child_needs_ns = needs.children&.any? do |_attr_name, child_needs|
1744
+ child_needs.namespaces.any? do |_key, ns_usage|
1745
+ ns_usage.namespace_class&.uri == uri
1746
+ end
1747
+ end
1748
+ next if child_needs_ns
1749
+
1750
+ # Safe to preserve at root
1751
+ hoisted[prefix] = uri
1752
+ end
1753
+ end
1754
+ end
1755
+ end
1756
+
1757
+ # FIFTH: Add XSI namespace if any Namespace in scope has schema_location
1758
+ # Schema location is handled by DeclarationPlan.build_schema_location_attr
1759
+ # which builds xsi:schemaLocation from all Namespace.schema_location values
1760
+ if namespaces_have_schema_location?(needs, options)
1761
+ xsi_uri = W3c::XsiNamespace.uri
1762
+ xsi_prefix = W3c::XsiNamespace.prefix_default || "xsi"
1763
+ hoisted[xsi_prefix] = xsi_uri unless hoisted.value?(xsi_uri)
1764
+ end
1765
+
1766
+ hoisted
1767
+ end
1768
+
1769
+ # Check if any Namespace in scope has schema_location
1770
+ #
1771
+ # @param needs [NamespaceNeeds] Namespace needs
1772
+ # @param options [Hash] Serialization options
1773
+ # @return [Boolean] True if any namespace has schema_location
1774
+ def namespaces_have_schema_location?(needs, _options)
1775
+ return false unless needs
1776
+
1777
+ # Check all namespace classes in needs
1778
+ needs.all_namespace_classes.any? do |ns_class|
1779
+ ns_class.is_a?(Class) && ns_class < Lutaml::Xml::Namespace && ns_class.schema_location
1780
+ end
1781
+ end
1782
+
1783
+ # Find the attribute name for a child XmlElement
1784
+ #
1785
+ # When building child element nodes, we need to match the XmlElement to its
1786
+ # corresponding attribute name in the parent's mapping to access the child's
1787
+ # own namespace needs.
1788
+ #
1789
+ # @param xml_element [XmlDataModel::XmlElement] The child element
1790
+ # @param mapping [Xml::Mapping] The current element's mapping (parent's for child elements)
1791
+ # @param options [Hash] Serialization options (may contain :parent_mapping)
1792
+ # @return [Symbol, nil] The attribute name, or nil if not found
1793
+ def find_child_attribute_name(xml_element, mapping, options = {})
1794
+ # Use parent's mapping if available (for child elements)
1795
+ search_mapping = options[:parent_mapping] || mapping
1796
+
1797
+ element_name = xml_element.name.to_s
1798
+ matching_rule = search_mapping.elements.find do |rule|
1799
+ rule.name.to_s == element_name
1800
+ end
1801
+
1802
+ matching_rule&.to
1803
+ end
1804
+
1805
+ # Get the current element's namespace_scope_configs
1806
+ #
1807
+ # For child elements, use their own namespace_scope, not the parent's.
1808
+ # This ensures that namespace_scope directives are scoped to the model that defines them.
1809
+ #
1810
+ # @param xml_element [XmlDataModel::XmlElement] The element
1811
+ # @param mapping [Xml::Mapping] The element's mapping
1812
+ # @param needs [NamespaceNeeds] Namespace needs
1813
+ # @param options [Hash] Serialization options
1814
+ # @return [Array<NamespaceScopeConfig>] The element's namespace_scope_configs
1815
+ def get_element_namespace_scope_configs(_xml_element, _mapping, needs,
1816
+ _options)
1817
+ # Use the parent's namespace_scope_configs
1818
+ needs.namespace_scope_configs
1819
+ end
1820
+
1821
+ # Find a scope config for a namespace class
1822
+ #
1823
+ # @param ns_class [Class] The namespace class
1824
+ # @param scope_configs [Array<NamespaceScopeConfig>] The scope configs to search
1825
+ # @return [NamespaceScopeConfig, nil] The matching scope config, or nil
1826
+ def find_scope_config_for(ns_class, scope_configs)
1827
+ scope_configs.find { |config| config.namespace_class == ns_class }
1828
+ end
1829
+
1830
+ # Build global prefix registry from needs
1831
+ #
1832
+ # @param needs [NamespaceNeeds] Namespace needs
1833
+ # @return [Hash<String, String>] URI => prefix mapping
1834
+ def build_prefix_registry(needs)
1835
+ registry = {}
1836
+ needs.namespaces.each_value do |ns_usage|
1837
+ ns_class = ns_usage.namespace_class
1838
+ if ns_class.prefix_default
1839
+ registry[ns_class.uri] =
1840
+ ns_class.prefix_default
1841
+ end
1842
+ end
1843
+ registry
1844
+ end
1845
+
1846
+ # Check if element or its descendants use a namespace
1847
+ #
1848
+ # @param xml_element [XmlDataModel::XmlElement] Element to check
1849
+ # @param ns_class [Class] Namespace class to search for
1850
+ # @return [Boolean] true if namespace is used in subtree
1851
+ def element_needs_namespace?(xml_element, ns_class)
1852
+ # Check direct children
1853
+ xml_element.children.each do |child|
1854
+ next unless child.is_a?(Lutaml::Xml::DataModel::XmlElement)
1855
+
1856
+ # Does child use this namespace?
1857
+ return true if child.namespace_class == ns_class
1858
+
1859
+ # Does child have attributes using this namespace?
1860
+ child.attributes.each do |attr|
1861
+ return true if attr.namespace_class == ns_class
1862
+ end
1863
+
1864
+ # Recurse to grandchildren
1865
+ return true if element_needs_namespace?(child, ns_class)
1866
+ end
1867
+
1868
+ false
1869
+ end
1870
+
1871
+ # Check if element is a native type element (leaf with text content)
1872
+ #
1873
+ # Native type elements (like :string, :integer) are simple values
1874
+ # that need xmlns="" when parent uses default namespace format.
1875
+ # Child models are complex elements with their own structure.
1876
+ #
1877
+ # @param xml_element [XmlDataModel::XmlElement] Element to check
1878
+ # @return [Boolean] true if element is a native type element
1879
+ def native_type_element?(xml_element)
1880
+ # Native type elements have:
1881
+ # - text_content (not nil)
1882
+ # - No element children (all children are Strings, not XmlElement)
1883
+ return false unless xml_element.text_content
1884
+
1885
+ xml_element.children.all?(String)
1886
+ end
1887
+
1888
+ # Build schema_location attribute value from all Namespace.schema_location values
1889
+ #
1890
+ # @param needs [NamespaceNeeds] Namespace needs
1891
+ # @return [Hash, nil] { "xsi:schemaLocation" => "uri1 loc1 uri2 loc2" } or nil
1892
+ def build_schema_location_attr_for_needs(needs)
1893
+ return nil unless needs
1894
+
1895
+ # Collect all namespace classes that have schema_location
1896
+ namespaces_with_schema = needs.all_namespace_classes.select do |ns_class|
1897
+ ns_class.is_a?(Class) && ns_class < Lutaml::Xml::Namespace && ns_class.schema_location
1898
+ end
1899
+
1900
+ return nil if namespaces_with_schema.empty?
1901
+
1902
+ # Build xsi:schemaLocation value
1903
+ value = namespaces_with_schema.map do |ns_class|
1904
+ "#{ns_class.uri} #{ns_class.schema_location}"
1905
+ end.join(" ")
1906
+
1907
+ # Get XSI prefix from hoisted declarations or use default
1908
+ xsi_prefix = W3c::XsiNamespace.prefix_default || "xsi"
1909
+
1910
+ { "#{xsi_prefix}:schemaLocation" => value }
1911
+ end
1912
+ end
1913
+ end
1914
+ end