lutaml-model 0.8.191 → 0.8.193

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 (86) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dependent-tests.yml +12 -0
  3. data/README.adoc +9 -0
  4. data/docs/_guides/xml-guide-0.8.0.adoc +1547 -0
  5. data/docs/_guides/xml_mappings/05_common_patterns.adoc +365 -1
  6. data/docs/missing_xml_features.adoc +35 -0
  7. data/docs/xml_1.0.adoc +1989 -0
  8. data/docs/xml_base.adoc +718 -0
  9. data/docs/xml_data_types.adoc +263 -0
  10. data/docs/xml_namespace.adoc +129 -0
  11. data/docs/xml_schema.adoc +500 -0
  12. data/docs/xml_xlink.adoc +539 -0
  13. data/lib/compat/opal/generate_boot.rb +5 -4
  14. data/lib/compat/opal/lutaml_model_boot.rb +0 -7
  15. data/lib/lutaml/model/attribute.rb +55 -0
  16. data/lib/lutaml/model/serialize/format_conversion.rb +4 -6
  17. data/lib/lutaml/model/version.rb +1 -1
  18. data/lib/lutaml/xml/adapter/adapter_helpers.rb +3 -3
  19. data/lib/lutaml/xml/adapter/base_adapter.rb +13 -29
  20. data/lib/lutaml/xml/adapter/moxml_adapter.rb +113 -22
  21. data/lib/lutaml/xml/adapter/xml_parser.rb +6 -8
  22. data/lib/lutaml/xml/adapter/xml_serializer.rb +3 -1
  23. data/lib/lutaml/xml/adapter.rb +0 -4
  24. data/lib/lutaml/xml/adapter_element.rb +19 -9
  25. data/lib/lutaml/xml/adapter_loader.rb +26 -11
  26. data/lib/lutaml/xml/builder/base.rb +6 -7
  27. data/lib/lutaml/xml/document.rb +1 -1
  28. data/lib/lutaml/xml/format.rb +0 -10
  29. data/lib/lutaml/xml/leptris/plan_hydrator.rb +2 -2
  30. data/spec/lutaml/model/cdata_spec.rb +11 -27
  31. data/spec/lutaml/model/encoding_spec.rb +6 -6
  32. data/spec/lutaml/model/except_spec.rb +5 -7
  33. data/spec/lutaml/model/mixed_content_spec.rb +22 -25
  34. data/spec/lutaml/model/multiple_mapping_spec.rb +8 -10
  35. data/spec/lutaml/model/namespace_autoload_spec.rb +86 -0
  36. data/spec/lutaml/model/ordered_content_spec.rb +5 -8
  37. data/spec/lutaml/model/processing_instruction_spec.rb +6 -9
  38. data/spec/lutaml/model/serialize/attribute_definition_block_form_spec.rb +1 -1
  39. data/spec/lutaml/model/serialize/builder_spec.rb +1 -1
  40. data/spec/lutaml/xml/adapter/base_adapter_regression_spec.rb +5 -8
  41. data/spec/lutaml/xml/adapter/empty_document_diagnostics_spec.rb +3 -4
  42. data/spec/lutaml/xml/adapter/nokogiri_adapter_spec.rb +2 -2
  43. data/spec/lutaml/xml/adapter/oga_adapter_spec.rb +2 -2
  44. data/spec/lutaml/xml/adapter/order_spec.rb +8 -22
  45. data/spec/lutaml/xml/adapter/over_cap_parse_spec.rb +2 -2
  46. data/spec/lutaml/xml/adapter/ox_adapter_spec.rb +2 -2
  47. data/spec/lutaml/xml/adapter/rexml_adapter_spec.rb +2 -2
  48. data/spec/lutaml/xml/adapter/xml_namespace_spec.rb +5 -8
  49. data/spec/lutaml/xml/adapter_loader_spec.rb +15 -3
  50. data/spec/lutaml/xml/adapter_metadata_default_spec.rb +1 -1
  51. data/spec/lutaml/xml/adapter_moxml_engine_spec.rb +1 -2
  52. data/spec/lutaml/xml/builder/builder_spec.rb +28 -20
  53. data/spec/lutaml/xml/default_namespace_spec.rb +4 -5
  54. data/spec/lutaml/xml/entity_fragmentation_spec.rb +2 -2
  55. data/spec/lutaml/xml/mapping_rule_spec.rb +1 -2
  56. data/spec/lutaml/xml/mapping_spec.rb +12 -14
  57. data/spec/lutaml/xml/namespace_missing_prefix_spec.rb +1 -1
  58. data/spec/lutaml/xml/namespace_preservation_spec.rb +11 -11
  59. data/spec/lutaml/xml/namespace_scope_vcard_spec.rb +5 -8
  60. data/spec/lutaml/xml/prefix_control_spec.rb +5 -8
  61. data/spec/lutaml/xml/type_namespace_prefix_spec.rb +2 -2
  62. data/spec/lutaml/xml/xml_adapter_spec.rb +5 -8
  63. data/spec/lutaml/xml/xml_declaration_spec.rb +19 -19
  64. data/spec/spec_helper.rb +2 -2
  65. data/spec/support/test_adapter_config.rb +9 -25
  66. metadata +10 -21
  67. data/lib/lutaml/xml/adapter/leptris_adapter.rb +0 -16
  68. data/lib/lutaml/xml/adapter/nokogiri_adapter.rb +0 -16
  69. data/lib/lutaml/xml/adapter/oga_adapter.rb +0 -20
  70. data/lib/lutaml/xml/adapter/ox_adapter.rb +0 -16
  71. data/lib/lutaml/xml/adapter/rexml_adapter.rb +0 -25
  72. data/lib/lutaml/xml/builder/leptris.rb +0 -13
  73. data/lib/lutaml/xml/builder/nokogiri.rb +0 -13
  74. data/lib/lutaml/xml/builder/oga.rb +0 -13
  75. data/lib/lutaml/xml/builder/ox.rb +0 -13
  76. data/lib/lutaml/xml/builder/rexml.rb +0 -13
  77. data/lib/lutaml/xml/leptris/element.rb +0 -34
  78. data/lib/lutaml/xml/nokogiri/element.rb +0 -34
  79. data/lib/lutaml/xml/nokogiri.rb +0 -10
  80. data/lib/lutaml/xml/oga/element.rb +0 -15
  81. data/lib/lutaml/xml/oga.rb +0 -10
  82. data/lib/lutaml/xml/ox/element.rb +0 -15
  83. data/lib/lutaml/xml/ox.rb +0 -10
  84. data/lib/lutaml/xml/rexml/element.rb +0 -15
  85. data/lib/lutaml/xml/rexml.rb +0 -10
  86. data/lib/tasks/xml_optimization_benchmark.rb +0 -178
@@ -0,0 +1,1547 @@
1
+ ---
2
+ title: XML Namespace Architecture in v0.8.0
3
+ nav_order: 1
4
+ parent: XML namespaces
5
+ ---
6
+
7
+ = XML Namespace Architecture in Lutaml::Model 0.8.0
8
+
9
+ == Overview
10
+
11
+ Version 0.8.0 introduces a complete redesign of XML namespace handling to fully comply with W3C XML Namespace and XSD specifications. This guide explains the new architecture and how to migrate from previous versions.
12
+
13
+ == What Changed in 0.8.0
14
+
15
+ === Major architectural changes
16
+
17
+ **XmlNamespace class introduced**:: Centralized namespace metadata management with full XSD support
18
+
19
+ **Element vs Type distinction**:: Clear separation between XML elements (structure) and XSD types (data)
20
+
21
+ **Namespace-aware parsing**:: URI-based matching (prefix-independent) for W3C compliance
22
+
23
+ **Type-level namespaces**:: Support for namespaced custom Value types
24
+
25
+ **New directives**:: `element()`, `type_name()`, `ordered()`, `mixed_content()` instead of options
26
+
27
+ **Deprecated features**:: `root()` options, `no_root`, mapping-level `namespace:` and `prefix:`
28
+
29
+ === Core principles
30
+
31
+ [cols="1,2,2"]
32
+ |===
33
+ |Principle |Old Approach |New Approach (0.8.0)
34
+
35
+ |Namespace identity
36
+ |Prefix-based matching
37
+ |URI-based matching (prefix-independent)
38
+
39
+ |Element declaration
40
+ |`root "name", ordered: true`
41
+ |`element "name"` + `ordered()` method
42
+
43
+ |Type naming
44
+ |Implicit or via `xsd_type`
45
+ |Explicit via `type_name()`
46
+
47
+ |Namespace config
48
+ |Per-mapping `namespace:` option
49
+ |Centralized `XmlNamespace` class
50
+
51
+ |Custom type namespaces
52
+ |`namespace` directive
53
+ |`xml_namespace` directive
54
+ |===
55
+
56
+ == XmlNamespace Class
57
+
58
+ === Purpose
59
+
60
+ The `XmlNamespace` class encapsulates all namespace metadata following W3C specifications, providing:
61
+
62
+ * Centralized namespace definitions (reusable across models)
63
+ * Full XSD generation support
64
+ * Control over element and attribute qualification
65
+ * Schema versioning and documentation
66
+ * Import/include declarations
67
+
68
+ === Basic syntax
69
+
70
+ [source,ruby]
71
+ ----
72
+ class MyNamespace < Lutaml::Model::XmlNamespace
73
+ uri 'namespace-uri' # Required: namespace URI
74
+ prefix_default 'prefix' # Optional: default prefix
75
+ element_form_default :qualified # Optional: element qualification
76
+ attribute_form_default :unqualified # Optional: attribute qualification
77
+ schema_location 'schema-url' # Optional: XSD location
78
+ version 'version-string' # Optional: schema version
79
+ documentation 'description' # Optional: documentation
80
+ imports OtherNamespace # Optional: imported namespaces
81
+ includes 'schema-file.xsd' # Optional: included schemas
82
+ end
83
+ ----
84
+
85
+ === XmlNamespace directives
86
+
87
+ ==== `uri` (required)
88
+
89
+ Sets the namespace URI that uniquely identifies this namespace.
90
+
91
+ [source,ruby]
92
+ ----
93
+ class CeramicNamespace < Lutaml::Model::XmlNamespace
94
+ uri 'https://example.com/schemas/ceramic/v1'
95
+ end
96
+ ----
97
+
98
+ Per W3C specifications, **the URI is the fundamental identifier** used for namespace matching during parsing.
99
+
100
+ ==== `prefix_default` (optional)
101
+
102
+ Sets the default prefix for this namespace.
103
+
104
+ [source,ruby]
105
+ ----
106
+ class CeramicNamespace < Lutaml::Model::XmlNamespace
107
+ uri 'https://example.com/schemas/ceramic/v1'
108
+ prefix_default 'cer'
109
+ end
110
+ ----
111
+
112
+ The prefix can be overridden at serialization time or in namespace declarations.
113
+
114
+ ==== `element_form_default` (optional)
115
+
116
+ Controls whether locally declared elements must be namespace-qualified by default.
117
+
118
+ [source,ruby]
119
+ ----
120
+ class CeramicNamespace < Lutaml::Model::XmlNamespace
121
+ uri 'https://example.com/schemas/ceramic/v1'
122
+ prefix_default 'cer'
123
+ element_form_default :qualified # or :unqualified (default)
124
+ end
125
+ ----
126
+
127
+ Values:
128
+
129
+ `:qualified`::: Local elements inherit parent namespace and must include prefix
130
+ `:unqualified`::: Local elements have no namespace (W3C default)
131
+
132
+ ==== `attribute_form_default` (optional)
133
+
134
+ Controls whether locally declared attributes must be namespace-qualified by default.
135
+
136
+ [source,ruby]
137
+ ----
138
+ class CeramicNamespace < Lutaml::Model::XmlNamespace
139
+ uri 'https://example.com/schemas/ceramic/v1'
140
+ prefix_default 'cer'
141
+ attribute_form_default :qualified # or :unqualified (default)
142
+ end
143
+ ----
144
+
145
+ Values:
146
+
147
+ `:qualified`::: Local attributes must include namespace prefix
148
+ `:unqualified`::: Local attributes have no namespace prefix (W3C default, recommended)
149
+
150
+ NOTE: Per W3C conventions, attributes are typically unqualified. The `:qualified` setting is required for formats like Office Open XML (OOXML).
151
+
152
+ ==== Other directives
153
+
154
+ `schema_location`::: URL where the XSD schema file can be found
155
+ `version`::: Schema version for documentation
156
+ `documentation`::: Human-readable description for XSD annotation
157
+ `imports`::: Other XmlNamespace classes to import (for cross-namespace references)
158
+ `includes`::: Schema files to include (same namespace)
159
+
160
+ .Complete namespace definition
161
+ [example]
162
+ ====
163
+ [source,ruby]
164
+ ----
165
+ class ContactNamespace < Lutaml::Model::XmlNamespace
166
+ uri 'https://example.com/schemas/contact/v1'
167
+ schema_location 'https://example.com/schemas/contact/v1/contact.xsd'
168
+ prefix_default 'contact'
169
+ element_form_default :qualified
170
+ attribute_form_default :unqualified
171
+ version '1.0'
172
+ documentation "Contact information schema for Example Corp"
173
+ imports AddressNamespace
174
+ includes 'contact-common.xsd', 'contact-types.xsd'
175
+ end
176
+ ----
177
+ ====
178
+
179
+ == Element vs Type Distinction
180
+
181
+ === W3C XML Schema concepts
182
+
183
+ In W3C XSD, **Element** and **Type** are separate, orthogonal concepts:
184
+
185
+ Element declaration:: Defines XML structure - what elements can appear in documents
186
+ Type definition:: Defines data structure - what content elements can contain
187
+
188
+ === Three XSD patterns
189
+
190
+ Lutaml::Model supports three W3C XSD patterns for declaring models:
191
+
192
+ ==== Pattern 1: Anonymous inline complexType
193
+
194
+ **Use when**: Element structure is unique and not reused.
195
+
196
+ [source,ruby]
197
+ ----
198
+ class Product < Lutaml::Model::Serializable
199
+ attribute :name, :string
200
+ attribute :price, :float
201
+
202
+ xml do
203
+ element "product" # <1>
204
+ map_element "name", to: :name
205
+ map_element "price", to: :price
206
+ end
207
+ end
208
+ ----
209
+ <1> Only `element()` declared, no `type_name()`
210
+
211
+ Generated XSD:
212
+
213
+ [source,xml]
214
+ ----
215
+ <xs:element name="product">
216
+ <xs:complexType>
217
+ <xs:sequence>
218
+ <xs:element name="name" type="xs:string"/>
219
+ <xs:element name="price" type="xs:decimal"/>
220
+ </xs:sequence>
221
+ </xs:complexType>
222
+ </xs:element>
223
+ ----
224
+
225
+ ==== Pattern 2: Named reusable complexType
226
+
227
+ **Use when**: ComplexType should be reusable by multiple elements.
228
+
229
+ [source,ruby]
230
+ ----
231
+ class ProductType < Lutaml::Model::Serializable
232
+ attribute :name, :string
233
+ attribute :price, :float
234
+
235
+ xml do
236
+ type_name "ProductType" # <1>
237
+ map_element "name", to: :name
238
+ map_element "price", to: :price
239
+ end
240
+ end
241
+ ----
242
+ <1> Only `type_name()` declared, no `element()`
243
+
244
+ Generated XSD:
245
+
246
+ [source,xml]
247
+ ----
248
+ <xs:complexType name="ProductType">
249
+ <xs:sequence>
250
+ <xs:element name="name" type="xs:string"/>
251
+ <xs:element name="price" type="xs:decimal"/>
252
+ </xs:sequence>
253
+ </xs:complexType>
254
+ ----
255
+
256
+ This type can be referenced by other elements without duplication.
257
+
258
+ ==== Pattern 3: Element with named complexType
259
+
260
+ **Use when**: Want both element declaration AND named type.
261
+
262
+ [source,ruby]
263
+ ----
264
+ class Product < Lutaml::Model::Serializable
265
+ attribute :name, :string
266
+ attribute :price, :float
267
+
268
+ xml do
269
+ element "product" # <1>
270
+ type_name "ProductType" # <2>
271
+ map_element "name", to: :name
272
+ map_element "price", to: :price
273
+ end
274
+ end
275
+ ----
276
+ <1> Element declared
277
+ <2> Type named
278
+
279
+ Generated XSD:
280
+
281
+ [source,xml]
282
+ ----
283
+ <xs:element name="product" type="ProductType"/>
284
+
285
+ <xs:complexType name="ProductType">
286
+ <xs:sequence>
287
+ <xs:element name="name" type="xs:string"/>
288
+ <xs:element name="price" type="xs:decimal"/>
289
+ </xs:sequence>
290
+ </xs:complexType>
291
+ ----
292
+
293
+ **Benefits**: Element declaration for direct use, named type for references and extension.
294
+
295
+ === Choosing a pattern
296
+
297
+ [source]
298
+ ----
299
+ Need reusable type?
300
+ │
301
+ ┌────────────┴────────────┐
302
+ │ │
303
+ YES NO
304
+ │ │
305
+ Want element too? Pattern 1
306
+ │ (Anonymous)
307
+ ┌───────┴────────┐
308
+ │ │
309
+ YES NO
310
+ │ │
311
+ Pattern 3 Pattern 2
312
+ (Element+Type) (Type-only)
313
+ ----
314
+
315
+ == New Model Declaration Syntax
316
+
317
+ === `element()` - Primary API
318
+
319
+ The `element()` method declares the XML element name.
320
+
321
+ **Before (0.7.x):**
322
+
323
+ [source,ruby]
324
+ ----
325
+ xml do
326
+ root "ceramic", ordered: true, mixed: true
327
+ map_element "type", to: :type
328
+ end
329
+ ----
330
+
331
+ **After (0.8.0):**
332
+
333
+ [source,ruby]
334
+ ----
335
+ xml do
336
+ element "ceramic" # <1>
337
+ ordered # <2>
338
+ mixed_content # <3>
339
+ map_element "type", to: :type
340
+ end
341
+ ----
342
+ <1> Element name declaration
343
+ <2> Separate `ordered()` method for strict sequence
344
+ <3> Separate `mixed_content()` method for mixed content
345
+
346
+ NOTE: `root()` still works as a backward-compatible alias but `element()` is recommended for clarity.
347
+
348
+ === `type_name()` - XSD type naming
349
+
350
+ The `type_name()` method sets an explicit type name for XSD generation.
351
+
352
+ [source,ruby]
353
+ ----
354
+ xml do
355
+ element "product"
356
+ type_name "CatalogItemType" # <1>
357
+ map_element "name", to: :name
358
+ end
359
+ ----
360
+ <1> Override default 'ProductType' name
361
+
362
+ NOTE: `xsd_type()` is a permanent alias of `type_name()` for backward compatibility.
363
+
364
+ === `ordered()` - Element sequence
365
+
366
+ The `ordered()` method enforces strict element ordering (replaces `ordered: true` option).
367
+
368
+ [source,ruby]
369
+ ----
370
+ xml do
371
+ element "ceramic"
372
+ ordered # <1>
373
+ map_element "id", to: :id
374
+ map_element "name", to: :name
375
+ end
376
+ ----
377
+ <1> Elements must appear in declared order
378
+
379
+ === `mixed_content()` - Mixed content
380
+
381
+ The `mixed_content()` method enables text and elements together (replaces `mixed: true` option).
382
+
383
+ [source,ruby]
384
+ ----
385
+ xml do
386
+ element "paragraph"
387
+ mixed_content # <1>
388
+ map_element "bold", to: :bold
389
+ end
390
+ ----
391
+ <1> Preserves text alongside elements
392
+
393
+ == Namespace Assignment
394
+
395
+ === Model-level namespace
396
+
397
+ The `namespace` method assigns a namespace to the model's element or type.
398
+
399
+ **Before (0.7.x):**
400
+
401
+ [source,ruby]
402
+ ----
403
+ xml do
404
+ root "ceramic"
405
+ namespace 'http://example.com/ceramic', 'cer' # <1>
406
+ map_element "type", to: :type
407
+ end
408
+ ----
409
+ <1> String-based namespace with URI and prefix
410
+
411
+ **After (0.8.0):**
412
+
413
+ [source,ruby]
414
+ ----
415
+ class CeramicNamespace < Lutaml::Model::XmlNamespace
416
+ uri 'http://example.com/ceramic'
417
+ prefix_default 'cer'
418
+ end
419
+
420
+ xml do
421
+ element "ceramic"
422
+ namespace CeramicNamespace # <1>
423
+ map_element "type", to: :type
424
+ end
425
+ ----
426
+ <1> Reference to XmlNamespace class
427
+
428
+ NOTE: String-based `namespace(uri, prefix)` still works but XmlNamespace classes are recommended.
429
+
430
+ === Type-level namespace (custom Value types)
431
+
432
+ Custom Value types can declare their own namespaces using `xml_namespace`.
433
+
434
+ **Before (0.7.x) - DEPRECATED:**
435
+
436
+ [source,ruby]
437
+ ----
438
+ class ColorType < Lutaml::Model::Type::String
439
+ namespace ColorNamespace # ⚠️ DEPRECATED
440
+ end
441
+ ----
442
+
443
+ **After (0.8.0):**
444
+
445
+ [source,ruby]
446
+ ----
447
+ class ColorType < Lutaml::Model::Type::String
448
+ xml_namespace ColorNamespace # ✅ CURRENT
449
+ xsd_type 'colorType'
450
+ end
451
+ ----
452
+
453
+ IMPORTANT: The `namespace` directive for `Type::Value` classes is deprecated. Use `xml_namespace` instead. This will be removed in version 1.0.0.
454
+
455
+ === Namespace scope consolidation
456
+
457
+ The `namespace_scope` directive consolidates namespace declarations at the root element.
458
+
459
+ [source,ruby]
460
+ ----
461
+ xml do
462
+ element "vCard"
463
+ namespace VcardNamespace
464
+ namespace_scope [DcNamespace, DctermsNamespace] # <1>
465
+
466
+ map_element "title", to: :title
467
+ map_element "created", to: :created
468
+ end
469
+ ----
470
+ <1> All namespaces declared at root instead of each element
471
+
472
+ **Before** (without namespace_scope):
473
+
474
+ [source,xml]
475
+ ----
476
+ <vCard xmlns="urn:ietf:params:xml:ns:vcard-4.0">
477
+ <dc:title xmlns:dc="http://purl.org/dc/elements/1.1/">Dr. John Doe</dc:title>
478
+ <dcterms:created xmlns:dcterms="http://purl.org/dc/terms/">2024-06-01</dcterms:created>
479
+ </vCard>
480
+ ----
481
+
482
+ **After** (with namespace_scope):
483
+
484
+ [source,xml]
485
+ ----
486
+ <vCard xmlns="urn:ietf:params:xml:ns:vcard-4.0"
487
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
488
+ xmlns:dcterms="http://purl.org/dc/terms/">
489
+ <dc:title>Dr. John Doe</dc:title>
490
+ <dcterms:created>2024-06-01</dcterms:created>
491
+ </vCard>
492
+ ----
493
+
494
+ ==== Declaration control
495
+
496
+ Control when namespaces are declared using the `declare:` option:
497
+
498
+ [source,ruby]
499
+ ----
500
+ namespace_scope [
501
+ { namespace: VtNamespace, declare: :always }, # <1>
502
+ { namespace: DcNamespace, declare: :auto } # <2>
503
+ ]
504
+ ----
505
+ <1> Always declare even if unused (for OOXML compatibility)
506
+ <2> Declare only if actually used (W3C default)
507
+
508
+ Declaration modes:
509
+
510
+ `:auto` (default)::: Namespace declared only if actually used
511
+ `:always`::: Namespace always declared (for schema compliance)
512
+ `:never`::: Namespace never declared (reserved for future use)
513
+
514
+ == Removed Mapping-Level Options
515
+
516
+ === No more `namespace:` in mappings
517
+
518
+ **Before (0.7.x):**
519
+
520
+ [source,ruby]
521
+ ----
522
+ xml do
523
+ root "ceramic"
524
+ namespace CeramicNamespace
525
+
526
+ map_element "type", to: :type,
527
+ namespace: 'http://other.com', prefix: 'other' # ❌ REMOVED
528
+ end
529
+ ----
530
+
531
+ **After (0.8.0):**
532
+
533
+ Child elements get their namespace from:
534
+
535
+ 1. **Type-level namespace** (if the attribute type declares one)
536
+ 2. **Parent namespace** + `element_form_default: :qualified`
537
+ 3. **Explicit qualification** via `form: :qualified`
538
+ 4. **No namespace** (if unqualified)
539
+
540
+ [source,ruby]
541
+ ----
542
+ # Option 1: Type-level namespace
543
+ class OtherType < Lutaml::Model::Type::String
544
+ xml_namespace OtherNamespace
545
+ end
546
+
547
+ xml do
548
+ element "ceramic"
549
+ namespace CeramicNamespace
550
+ map_element "type", to: :type # <1>
551
+ end
552
+ ----
553
+ <1> Uses OtherType's namespace automatically
554
+
555
+ [source,ruby]
556
+ ----
557
+ # Option 2: Qualified child (inherits parent namespace)
558
+ class CeramicNamespace < Lutaml::Model::XmlNamespace
559
+ element_form_default :qualified # <1>
560
+ end
561
+
562
+ xml do
563
+ element "ceramic"
564
+ namespace CeramicNamespace
565
+ map_element "type", to: :type # <2>
566
+ end
567
+ ----
568
+ <1> Schema-level default
569
+ <2> Inherits CeramicNamespace
570
+
571
+ [source,ruby]
572
+ ----
573
+ # Option 3: Per-element qualification
574
+ xml do
575
+ element "ceramic"
576
+ namespace CeramicNamespace
577
+ map_element "type", to: :type, form: :qualified # <1>
578
+ end
579
+ ----
580
+ <1> Mapping-level override
581
+
582
+ == Prefix Independence
583
+
584
+ === URI-based namespace matching
585
+
586
+ Lutaml::Model 0.8.0 implements W3C-compliant namespace processing where **namespace URIs, not prefixes, determine element identity**.
587
+
588
+ This means models correctly parse XML documents regardless of which prefix (or no prefix) is used, as long as the namespace URI matches.
589
+
590
+ .Parsing with different prefixes
591
+ [example]
592
+ ====
593
+ [source,ruby]
594
+ ----
595
+ class CeramicNamespace < Lutaml::Model::XmlNamespace
596
+ uri 'http://example.com/ceramic'
597
+ prefix_default 'cer'
598
+ end
599
+
600
+ class Ceramic < Lutaml::Model::Serializable
601
+ attribute :type, :string
602
+
603
+ xml do
604
+ element 'Ceramic'
605
+ namespace CeramicNamespace
606
+ map_element 'Type', to: :type
607
+ end
608
+ end
609
+
610
+ # All three formats parse successfully:
611
+
612
+ # Format 1: Default namespace
613
+ xml1 = '<Ceramic xmlns="http://example.com/ceramic"><Type>Porcelain</Type></Ceramic>'
614
+ Ceramic.from_xml(xml1).type # => "Porcelain"
615
+
616
+ # Format 2: Model's defined prefix
617
+ xml2 = '<cer:Ceramic xmlns:cer="http://example.com/ceramic"><cer:Type>Porcelain</cer:Type></cer:Ceramic>'
618
+ Ceramic.from_xml(xml2).type # => "Porcelain"
619
+
620
+ # Format 3: Arbitrary prefix
621
+ xml3 = '<pottery:Ceramic xmlns:pottery="http://example.com/ceramic"><pottery:Type>Porcelain</pottery:Type></pottery:Ceramic>'
622
+ Ceramic.from_xml(xml3).type # => "Porcelain"
623
+ ----
624
+
625
+ All three examples produce identical `Ceramic` objects because the namespace URI matches.
626
+ ====
627
+
628
+ === Serialization format control
629
+
630
+ The `prefix:` option in `to_xml()` controls output format and supports four modes:
631
+
632
+ ==== Mode 1: Default namespace (no prefix option)
633
+
634
+ By default, serialization uses the default namespace format for cleaner, more readable XML:
635
+
636
+ [source,ruby]
637
+ ----
638
+ ceramic = Ceramic.new(type: "Porcelain")
639
+
640
+ # Default: uses default namespace (xmlns="...")
641
+ ceramic.to_xml
642
+ # => <Ceramic xmlns="http://example.com/ceramic"><Type>Porcelain</Type></Ceramic>
643
+ ----
644
+
645
+ **Automatic prefix switching**: If any **XML attribute** in the model tree uses the root namespace, the system automatically switches to prefixed format. This is required by W3C XML Namespace specifications, which mandate that attributes in a namespace must always be qualified with a prefix.
646
+
647
+ [source,ruby]
648
+ ----
649
+ class Ceramic < Lutaml::Model::Serializable
650
+ attribute :type, :string
651
+ attribute :id, :string # Mapped as XML attribute
652
+
653
+ xml do
654
+ element 'Ceramic'
655
+ namespace CeramicNamespace
656
+ map_element 'Type', to: :type
657
+ map_attribute 'id', to: :id # <1>
658
+ end
659
+ end
660
+
661
+ ceramic = Ceramic.new(type: "Porcelain", id: "C123")
662
+ ceramic.to_xml
663
+ # => <cer:Ceramic xmlns:cer="http://example.com/ceramic" cer:id="C123">
664
+ # <cer:Type>Porcelain</cer:Type>
665
+ # </cer:Ceramic>
666
+ ----
667
+ <1> Presence of XML attribute forces prefix format
668
+
669
+ This automatic switching ensures W3C compliance: since default namespace declarations (`xmlns="..."`) only apply to elements (not attributes), any attribute that needs a namespace must use a prefix.
670
+
671
+ ==== Mode 2: Force prefix with boolean
672
+
673
+ Use `prefix: true` to force prefixed namespace format:
674
+
675
+ [source,ruby]
676
+ ----
677
+ ceramic.to_xml(prefix: true)
678
+ # => <cer:Ceramic xmlns:cer="http://example.com/ceramic">
679
+ # <cer:Type>Porcelain</cer:Type>
680
+ # </cer:Ceramic>
681
+ ----
682
+
683
+ This uses the `prefix_default` from the XmlNamespace class. Useful for compatibility with systems that expect prefixed elements.
684
+
685
+ ==== Mode 3: Custom prefix with string
686
+
687
+ Use `prefix: "string"` to override the default prefix for the root namespace:
688
+
689
+ [source,ruby]
690
+ ----
691
+ ceramic.to_xml(prefix: "custom")
692
+ # => <custom:Ceramic xmlns:custom="http://example.com/ceramic">
693
+ # <custom:Type>Porcelain</custom:Type>
694
+ # </custom:Ceramic>
695
+ ----
696
+
697
+ All elements using the root namespace will use this custom prefix.
698
+
699
+ ==== Mode 4: Per-namespace prefix mapping (Hash)
700
+
701
+ Use `prefix: { hash }` to specify different prefixes for different namespaces:
702
+
703
+ [source,ruby]
704
+ ----
705
+ class DcTitleType < Lutaml::Model::Type::String
706
+ xml_namespace DcNamespace
707
+ end
708
+
709
+ class Vcard < Lutaml::Model::Serializable
710
+ attribute :title, DcTitleType
711
+ attribute :name, :string
712
+
713
+ xml do
714
+ element "vCard"
715
+ namespace VcardNamespace
716
+ map_element "title", to: :title
717
+ map_element "name", to: :name
718
+ end
719
+ end
720
+
721
+ vcard = Vcard.new(title: "Dr. John Doe", name: "John Doe")
722
+
723
+ # Map specific prefixes to namespace classes
724
+ vcard.to_xml(prefix: {
725
+ VcardNamespace => "v",
726
+ DcNamespace => "dc"
727
+ })
728
+ # => <v:vCard xmlns:v="urn:ietf:params:xml:ns:vcard-4.0"
729
+ # xmlns:dc="http://purl.org/dc/elements/1.1/">
730
+ # <dc:title>Dr. John Doe</dc:title>
731
+ # <v:name>John Doe</v:name>
732
+ # </v:vCard>
733
+ ----
734
+
735
+ This mode provides fine-grained control over prefix usage across multiple namespaces.
736
+
737
+ IMPORTANT: The `prefix:` option only affects qualified elements. Unqualified elements (`:unqualified` form) remain without prefix regardless of this setting.
738
+
739
+ == Three-Phase Namespace Architecture
740
+
741
+ === Overview
742
+
743
+ Lutaml::Model 0.8.0 implements a sophisticated three-phase namespace architecture that ensures optimal xmlns declarations while maintaining W3C XML Namespace compliance across all XML adapters (Nokogiri, Ox, Oga).
744
+
745
+ This architecture guarantees consistent namespace behavior regardless of which underlying XML library is used.
746
+
747
+ === Architecture diagram
748
+
749
+ [source]
750
+ ----
751
+ ┌─────────────────────────────────────────────────────────────────┐
752
+ │ XML Serialization Request │
753
+ │ (model.to_xml) │
754
+ └──────────────────────────────┬──────────────────────────────────┘
755
+ │
756
+ ▼
757
+ ┌─────────────────────────────────────────────────────────────────┐
758
+ │ PHASE 1: BOTTOM-UP COLLECTION (NamespaceCollector) │
759
+ │─────────────────────────────────────────────────────────────────│
760
+ │ • Traverse entire model tree depth-first │
761
+ │ • Collect element namespaces from all attributes │
762
+ │ • Collect attribute namespaces from all attributes │
763
+ │ • Collect child model namespaces recursively │
764
+ │ • Collect type-only model namespaces │
765
+ │ • Handle circular references (visited set) │
766
+ │ • Result: Complete namespace requirements for entire tree │
767
+ └──────────────────────────────┬──────────────────────────────────┘
768
+ │
769
+ ▼
770
+ ┌─────────────────────────────────────────────────────────────────┐
771
+ │ PHASE 2: DECLARATION PLANNING (DeclarationPlanner) │
772
+ │─────────────────────────────────────────────────────────────────│
773
+ │ • Analyze collected namespace requirements │
774
+ │ • Decide default vs prefixed format per W3C rules │
775
+ │ • Resolve namespace conflicts intelligently │
776
+ │ • Apply namespace_scope directives │
777
+ │ • Apply prefix: option overrides │
778
+ │ • Implement "never declare twice" principle │
779
+ │ • Result: Optimal xmlns declaration plan for root element │
780
+ └──────────────────────────────┬──────────────────────────────────┘
781
+ │
782
+ ▼
783
+ ┌─────────────────────────────────────────────────────────────────┐
784
+ │ PHASE 3: SERIALIZATION INTEGRATION │
785
+ │─────────────────────────────────────────────────────────────────│
786
+ │ │
787
+ │ ┌────────────────────┐ ┌────────────────┐ ┌──────────────┐ │
788
+ │ │ NokogiriAdapter │ │ OxAdapter │ │ OgaAdapter │ │
789
+ │ └────────────────────┘ └────────────────┘ └──────────────┘ │
790
+ │ │
791
+ │ All adapters use the namespace plan: │
792
+ │ • build_element_with_plan - Apply plan to root element │
793
+ │ • build_ordered_element_with_plan - Ordered content │
794
+ │ • build_unordered_children_with_plan - Unordered children │
795
+ │ • Children reference parent xmlns, never redeclare │
796
+ │ • Result: Consistent XML output across all adapters │
797
+ └──────────────────────────────┬──────────────────────────────────┘
798
+ │
799
+ ▼
800
+ ┌─────────────────┐
801
+ │ XML Document │
802
+ │ with optimal │
803
+ │ xmlns │
804
+ └─────────────────┘
805
+ ----
806
+
807
+ === Phase 1: Bottom-up collection
808
+
809
+ The [`NamespaceCollector`](../../lib/lutaml/model/xml/namespace_collector.rb) traverses the entire model hierarchy bottom-up to collect:
810
+
811
+ * **Element namespaces** from all model attributes
812
+ * **Attribute namespaces** from all model attributes
813
+ * **Child model namespaces** via recursive traversal
814
+ * **Type-only model namespaces** (models without element declarations)
815
+ * **Custom namespace mappings** from type-level declarations
816
+
817
+ **Key features**:
818
+
819
+ * **Circular reference prevention**: Maintains visited set to avoid infinite loops
820
+ * **Inheritance handling**: Correctly processes namespace inheritance scenarios
821
+ * **Complete tree knowledge**: Provides full context before any xmlns decisions
822
+
823
+ === Phase 2: Declaration planning
824
+
825
+ The [`DeclarationPlanner`](../../lib/lutaml/model/xml/declaration_planner.rb) creates an optimal xmlns declaration strategy:
826
+
827
+ **Decision logic**:
828
+
829
+ 1. **Analyze namespace usage**: Count element vs attribute usage
830
+ 2. **Choose format**: Default namespace for most-used, prefixes for others
831
+ 3. **Handle conflicts**: Resolve multiple namespaces intelligently
832
+ 4. **Apply directives**: Respect `namespace_scope` with `declare:` options
833
+ 5. **Apply overrides**: Honor `prefix:` option from `to_xml()`
834
+ 6. **Root declaration**: Declare all required namespaces at root element
835
+
836
+ **Declaration strategies**:
837
+
838
+ * **Default namespace** (`xmlns="uri"`): Used for most common namespace (unless attributes present)
839
+ * **Prefixed namespaces** (`xmlns:prefix="uri"`): Used for additional namespaces
840
+ * **Never declare twice**: xmlns declared once at root, children inherit
841
+
842
+ === Phase 3: Serialization integration
843
+
844
+ All XML adapters use the three-phase architecture via consistent interfaces:
845
+
846
+ **Adapter methods**:
847
+
848
+ * `build_element_with_plan` - Root element with xmlns declarations
849
+ * `build_ordered_element_with_plan` - Sequence-ordered content
850
+ * `build_unordered_children_with_plan` - Flexible child ordering
851
+ * Mixed content handling with namespace awareness
852
+ * Collection handling with namespace awareness
853
+
854
+ **Consistency guarantees**:
855
+
856
+ * **Same model + same options = same XML**: Deterministic output
857
+ * **Adapter-independent**: Nokogiri, Ox, and Oga produce identical xmlns patterns
858
+ * **W3C compliant**: All output follows XML Namespace specifications
859
+
860
+ === Benefits
861
+
862
+ **Single source of truth**:: DeclarationPlanner makes ALL xmlns decisions
863
+
864
+ **Full tree knowledge**:: NamespaceCollector provides complete context before planning
865
+
866
+ **Never declare twice**:: xmlns declared at root, children reference via prefix
867
+
868
+ **Clean separation**:: Three independent, testable phases
869
+
870
+ **Circular reference handling**:: Built-in recursion prevention
871
+
872
+ **Adapter consistency**:: Uniform namespace behavior across all XML libraries
873
+
874
+ === Example flow
875
+
876
+ [source,ruby]
877
+ ----
878
+ class DcNamespace < Lutaml::Model::XmlNamespace
879
+ uri 'http://purl.org/dc/elements/1.1/'
880
+ prefix_default 'dc'
881
+ end
882
+
883
+ class DcTitleType < Lutaml::Model::Type::String
884
+ xml_namespace DcNamespace
885
+ end
886
+
887
+ class Book < Lutaml::Model::Serializable
888
+ attribute :title, DcTitleType
889
+ attribute :isbn, :string
890
+
891
+ xml do
892
+ element "book"
893
+ namespace BookNamespace
894
+ namespace_scope [DcNamespace]
895
+ map_element "title", to: :title
896
+ map_element "isbn", to: :isbn
897
+ end
898
+ end
899
+
900
+ book = Book.new(title: "XML Guide", isbn: "123")
901
+ xml = book.to_xml
902
+ ----
903
+
904
+ **Phase 1 (Collection)**: Discovers BookNamespace (element) and DcNamespace (title attribute type)
905
+
906
+ **Phase 2 (Planning)**: Decides BookNamespace as default, DcNamespace as prefix (because `namespace_scope` consolidates at root)
907
+
908
+ **Phase 3 (Serialization)**: Produces:
909
+
910
+ [source,xml]
911
+ ----
912
+ <book xmlns="http://example.com/book"
913
+ xmlns:dc="http://purl.org/dc/elements/1.1/">
914
+ <dc:title>XML Guide</dc:title>
915
+ <isbn>123</isbn>
916
+ </book>
917
+ ----
918
+
919
+ == Namespace Scope Management
920
+
921
+ === Purpose
922
+
923
+ The `namespace_scope` directive serves two critical purposes:
924
+
925
+ 1. **Consolidate declarations**: Move xmlns declarations from child elements to root
926
+ 2. **Force declarations**: Declare namespaces even when unused (for compatibility)
927
+
928
+ These capabilities are essential for formats like Office Open XML (OOXML) that require specific namespace declarations for compatibility.
929
+
930
+ === Basic consolidation
931
+
932
+ Without `namespace_scope`, each namespace is declared where first used:
933
+
934
+ [source,ruby]
935
+ ----
936
+ class Vcard < Lutaml::Model::Serializable
937
+ attribute :title, DcTitleType
938
+ attribute :created, DctermsCreatedType
939
+
940
+ xml do
941
+ element "vCard"
942
+ namespace VcardNamespace
943
+ # No namespace_scope - local declarations
944
+ map_element "title", to: :title
945
+ map_element "created", to: :created
946
+ end
947
+ end
948
+ ----
949
+
950
+ Output:
951
+ [source,xml]
952
+ ----
953
+ <vCard xmlns="urn:ietf:params:xml:ns:vcard-4.0">
954
+ <dc:title xmlns:dc="http://purl.org/dc/elements/1.1/">Dr. John Doe</dc:title>
955
+ <dcterms:created xmlns:dcterms="http://purl.org/dc/terms/">2024-06-01</dcterms:created>
956
+ </vCard>
957
+ ----
958
+
959
+ With `namespace_scope`, all declarations consolidate at root:
960
+
961
+ [source,ruby]
962
+ ----
963
+ xml do
964
+ element "vCard"
965
+ namespace VcardNamespace
966
+ namespace_scope [DcNamespace, DctermsNamespace] # Consolidate here
967
+ map_element "title", to: :title
968
+ map_element "created", to: :created
969
+ end
970
+ ----
971
+
972
+ Output:
973
+ [source,xml]
974
+ ----
975
+ <vCard xmlns="urn:ietf:params:xml:ns:vcard-4.0"
976
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
977
+ xmlns:dcterms="http://purl.org/dc/terms/">
978
+ <dc:title>Dr. John Doe</dc:title>
979
+ <dcterms:created>2024-06-01</dcterms:created>
980
+ </vCard>
981
+ ----
982
+
983
+ **Benefits**: Cleaner XML, better readability, single location for all xmlns.
984
+
985
+ === Declaration modes
986
+
987
+ Control when namespaces are declared using `declare:` option:
988
+
989
+ [source,ruby]
990
+ ----
991
+ namespace_scope [
992
+ { namespace: RequiredNamespace, declare: :always }, # <1>
993
+ { namespace: OptionalNamespace, declare: :auto }, # <2>
994
+ { namespace: BannedNamespace, declare: :never } # <3>
995
+ ]
996
+ ----
997
+ <1> Always declare even if unused (OOXML compatibility)
998
+ <2> Declare only if actually used (W3C default behavior)
999
+ <3> Never declare (error if used - reserved for future)
1000
+
1001
+ ==== Mode: `:auto` (default)
1002
+
1003
+ Namespace declared **only if actually used** in elements or attributes.
1004
+
1005
+ [source,ruby]
1006
+ ----
1007
+ namespace_scope [DcNamespace] # Implicit declare: :auto
1008
+
1009
+ # If no elements use DcNamespace → not declared
1010
+ # If any element uses DcNamespace → declared at root
1011
+ ----
1012
+
1013
+ **Use for**: Standard W3C behavior, minimize unnecessary declarations
1014
+
1015
+ ==== Mode: `:always`
1016
+
1017
+ Namespace **always declared**, even if unused in elements/attributes.
1018
+
1019
+ [source,ruby]
1020
+ ----
1021
+ namespace_scope [
1022
+ { namespace: VtNamespace, declare: :always }
1023
+ ]
1024
+
1025
+ # VtNamespace declared at root even if no elements use it
1026
+ ----
1027
+
1028
+ **Use for**:
1029
+
1030
+ * Office Open XML compatibility (requires specific xmlns)
1031
+ * Schema validation requirements
1032
+ * External tool requirements
1033
+
1034
+ .OOXML example requiring unused namespace
1035
+ [example]
1036
+ ====
1037
+ [source,ruby]
1038
+ ----
1039
+ class AppNamespace < Lutaml::Model::XmlNamespace
1040
+ uri 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties'
1041
+ prefix_default 'app'
1042
+ end
1043
+
1044
+ class VtNamespace < Lutaml::Model::XmlNamespace
1045
+ uri 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes'
1046
+ prefix_default 'vt'
1047
+ end
1048
+
1049
+ class Properties < Lutaml::Model::Serializable
1050
+ attribute :template, :string
1051
+
1052
+ xml do
1053
+ element "Properties"
1054
+ namespace AppNamespace
1055
+
1056
+ # VtNamespace required by OOXML spec but not used in this model
1057
+ namespace_scope [
1058
+ { namespace: VtNamespace, declare: :always }
1059
+ ]
1060
+
1061
+ map_element "Template", to: :template
1062
+ end
1063
+ end
1064
+
1065
+ props = Properties.new(template: "Normal.dotm")
1066
+ puts props.to_xml
1067
+ ----
1068
+
1069
+ Output:
1070
+ [source,xml]
1071
+ ----
1072
+ <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
1073
+ xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
1074
+ <Template>Normal.dotm</Template>
1075
+ </Properties>
1076
+ ----
1077
+
1078
+ The `vt:` namespace is declared even though no elements use it, satisfying OOXML requirements.
1079
+ ====
1080
+
1081
+ ==== Mode: `:never`
1082
+
1083
+ Namespace **never declared**. If namespace is used in elements, raises error.
1084
+
1085
+ [source,ruby]
1086
+ ----
1087
+ namespace_scope [
1088
+ { namespace: DeprecatedNamespace, declare: :never }
1089
+ ]
1090
+
1091
+ # Raises error if any element uses DeprecatedNamespace
1092
+ ----
1093
+
1094
+ **Use for**: Prevent usage of deprecated namespaces during migration
1095
+
1096
+ === Use cases
1097
+
1098
+ **Consolidation** (`:auto` default):::
1099
+ Clean XML with all xmlns at root
1100
+ +
1101
+ [source,ruby]
1102
+ ----
1103
+ namespace_scope [Namespace1, Namespace2, Namespace3]
1104
+ ----
1105
+
1106
+ **OOXML compatibility** (`:always`):::
1107
+ Force declaration of required but unused namespaces
1108
+ +
1109
+ [source,ruby]
1110
+ ----
1111
+ namespace_scope [
1112
+ { namespace: VtNamespace, declare: :always }
1113
+ ]
1114
+ ----
1115
+
1116
+ **Mixed requirements** (per-namespace control):::
1117
+ Different declaration rules for different namespaces
1118
+ +
1119
+ [source,ruby]
1120
+ ----
1121
+ namespace_scope [
1122
+ CoreNamespace, # Implicit :auto
1123
+ { namespace: RequiredNamespace, declare: :always },
1124
+ { namespace: OptionalNamespace, declare: :auto }
1125
+ ]
1126
+ ----
1127
+
1128
+ == Migration Guide
1129
+
1130
+ === Step 1: Identify affected code
1131
+
1132
+ Find models using deprecated patterns:
1133
+
1134
+ [source,shell]
1135
+ ----
1136
+ # Find no_root usage
1137
+ grep -r "no_root" app/ lib/
1138
+
1139
+ # Find root with options
1140
+ grep -r "root.*ordered:" app/ lib/
1141
+ grep -r "root.*mixed:" app/ lib/
1142
+
1143
+ # Find Type::Value namespace directive
1144
+ grep -r "class.*Type::.*namespace" app/ lib/
1145
+ ----
1146
+
1147
+ === Step 2: Migrate XmlNamespace classes
1148
+
1149
+ **Before:**
1150
+
1151
+ [source,ruby]
1152
+ ----
1153
+ class Contact < Lutaml::Model::Serializable
1154
+ xml do
1155
+ root 'contact'
1156
+ namespace 'https://example.com/contact', 'contact'
1157
+ map_element 'name', to: :name
1158
+ end
1159
+ end
1160
+ ----
1161
+
1162
+ **After:**
1163
+
1164
+ [source,ruby]
1165
+ ----
1166
+ class ContactNamespace < Lutaml::Model::XmlNamespace
1167
+ uri 'https://example.com/contact'
1168
+ prefix_default 'contact'
1169
+ element_form_default :qualified
1170
+ end
1171
+
1172
+ class Contact < Lutaml::Model::Serializable
1173
+ xml do
1174
+ element 'contact' # or keep 'root'
1175
+ namespace ContactNamespace
1176
+ map_element 'name', to: :name
1177
+ end
1178
+ end
1179
+ ----
1180
+
1181
+ === Step 3: Update element declarations
1182
+
1183
+ **Before:**
1184
+
1185
+ [source,ruby]
1186
+ ----
1187
+ xml do
1188
+ root "ceramic", ordered: true, mixed: true
1189
+ end
1190
+ ----
1191
+
1192
+ **After:**
1193
+
1194
+ [source,ruby]
1195
+ ----
1196
+ xml do
1197
+ element "ceramic" # or keep 'root'
1198
+ ordered # separate method
1199
+ mixed_content # separate method
1200
+ end
1201
+ ----
1202
+
1203
+ === Step 4: Migrate type-only models
1204
+
1205
+ **Before:**
1206
+
1207
+ [source,ruby]
1208
+ ----
1209
+ class Address < Lutaml::Model::Serializable
1210
+ xml do
1211
+ no_root # ⚠️ DEPRECATED
1212
+ map_element 'street', to: :street
1213
+ end
1214
+ end
1215
+ ----
1216
+
1217
+ **After:**
1218
+
1219
+ [source,ruby]
1220
+ ----
1221
+ class Address < Lutaml::Model::Serializable
1222
+ xml do
1223
+ # Simply omit element() or root() call
1224
+ sequence do # optional but recommended
1225
+ map_element 'street', to: :street
1226
+ end
1227
+ end
1228
+ end
1229
+ ----
1230
+
1231
+ === Step 5: Update Type::Value namespaces
1232
+
1233
+ **Before:**
1234
+
1235
+ [source,ruby]
1236
+ ----
1237
+ class ColorType < Lutaml::Model::Type::String
1238
+ namespace ColorNamespace # ⚠️ DEPRECATED
1239
+ end
1240
+ ----
1241
+
1242
+ **After:**
1243
+
1244
+ [source,ruby]
1245
+ ----
1246
+ class ColorType < Lutaml::Model::Type::String
1247
+ xml_namespace ColorNamespace # ✅ CURRENT
1248
+ end
1249
+ ----
1250
+
1251
+ === Step 6: Remove mapping-level namespace options
1252
+
1253
+ **Before:**
1254
+
1255
+ [source,ruby]
1256
+ ----
1257
+ map_element "child", to: :child,
1258
+ namespace: 'http://other.com', prefix: 'other'
1259
+ ----
1260
+
1261
+ **After** (choose appropriate approach):
1262
+
1263
+ [source,ruby]
1264
+ ----
1265
+ # Option 1: Use type-level namespace
1266
+ class OtherType < Lutaml::Model::Type::String
1267
+ xml_namespace OtherNamespace
1268
+ end
1269
+ attribute :child, OtherType
1270
+
1271
+ # Option 2: Use qualification
1272
+ class ParentNamespace < Lutaml::Model::XmlNamespace
1273
+ element_form_default :qualified
1274
+ end
1275
+
1276
+ # Option 3: Per-element form
1277
+ map_element "child", to: :child, form: :qualified
1278
+ ----
1279
+
1280
+ === Step 7: Test thoroughly
1281
+
1282
+ [source,ruby]
1283
+ ----
1284
+ # Round-trip test
1285
+ original = Model.new(attr: "value")
1286
+ xml = original.to_xml
1287
+ parsed = Model.from_xml(xml)
1288
+ assert_equal original, parsed
1289
+
1290
+ # Namespace format test
1291
+ assert_includes xml, 'xmlns'
1292
+
1293
+ # Prefix-independent parsing test
1294
+ xml_with_prefix = original.to_xml(prefix: true)
1295
+ parsed_from_prefix = Model.from_xml(xml_with_prefix)
1296
+ assert_equal original, parsed_from_prefix
1297
+ ----
1298
+
1299
+ == Complete Migration Example
1300
+
1301
+ .Before (0.7.x)
1302
+ [source,ruby]
1303
+ ----
1304
+ class Metadata < Lutaml::Model::Serializable
1305
+ attribute :version, :string
1306
+
1307
+ xml do
1308
+ no_root
1309
+ map_attribute 'version', to: :version
1310
+ end
1311
+ end
1312
+
1313
+ class Document < Lutaml::Model::Serializable
1314
+ attribute :title, :string
1315
+ attribute :metadata, Metadata
1316
+
1317
+ xml do
1318
+ root 'document', ordered: true
1319
+ namespace 'https://example.com/doc', 'doc'
1320
+ map_element 'title', to: :title
1321
+ map_element 'metadata', to: :metadata
1322
+ end
1323
+ end
1324
+ ----
1325
+
1326
+ .After (0.8.0)
1327
+ [source,ruby]
1328
+ ----
1329
+ class DocNamespace < Lutaml::Model::XmlNamespace
1330
+ uri 'https://example.com/doc'
1331
+ prefix_default 'doc'
1332
+ element_form_default :qualified
1333
+ version '2.0'
1334
+ documentation "Document schema v2"
1335
+ end
1336
+
1337
+ # Type-only model (no element declaration)
1338
+ class Metadata < Lutaml::Model::Serializable
1339
+ attribute :version, :string
1340
+
1341
+ xml do
1342
+ map_attribute 'version', to: :version
1343
+ end
1344
+ end
1345
+
1346
+ class Document < Lutaml::Model::Serializable
1347
+ attribute :title, :string
1348
+ attribute :metadata, Metadata
1349
+
1350
+ xml do
1351
+ element 'document'
1352
+ namespace DocNamespace
1353
+ ordered
1354
+ documentation "A document with metadata"
1355
+
1356
+ sequence do
1357
+ map_element 'title', to: :title
1358
+ map_element 'metadata', to: :metadata
1359
+ end
1360
+ end
1361
+ end
1362
+ ----
1363
+
1364
+ == Deprecation Timeline
1365
+
1366
+ [cols="1,2,2"]
1367
+ |===
1368
+ |Feature |Status |Action Required
1369
+
1370
+ |`root()` method
1371
+ |✅ Maintained
1372
+ |No action, but `element()` preferred
1373
+
1374
+ |`root` with options
1375
+ |⚠️ Deprecated pattern
1376
+ |Move options to separate methods
1377
+
1378
+ |`no_root`
1379
+ |⚠️ Deprecated
1380
+ |Omit element() declaration
1381
+
1382
+ |`Type::Value.namespace`
1383
+ |⚠️ Deprecated
1384
+ |Use `xml_namespace` instead
1385
+
1386
+ |Mapping-level `namespace:`
1387
+ |❌ Removed
1388
+ |Use type-level or qualification
1389
+
1390
+ |Mapping-level `prefix:`
1391
+ |❌ Removed
1392
+ |Use XmlNamespace classes
1393
+
1394
+ |String-based `namespace()`
1395
+ |✅ Maintained
1396
+ |Works but XmlNamespace preferred
1397
+ |===
1398
+
1399
+ == Best Practices
1400
+
1401
+ === 1. Use XmlNamespace classes
1402
+
1403
+ [source,ruby]
1404
+ ----
1405
+ # GOOD: Centralized, reusable, full XSD support
1406
+ class ContactNamespace < Lutaml::Model::XmlNamespace
1407
+ uri 'https://example.com/contact'
1408
+ prefix_default 'contact'
1409
+ element_form_default :qualified
1410
+ end
1411
+
1412
+ # AVOID: String-based (limited features)
1413
+ namespace 'https://example.com/contact', 'contact'
1414
+ ----
1415
+
1416
+ === 2. Be explicit about qualification
1417
+
1418
+ [source,ruby]
1419
+ ----
1420
+ # GOOD: Clear intent
1421
+ class MyNamespace < Lutaml::Model::XmlNamespace
1422
+ element_form_default :qualified # or :unqualified
1423
+ end
1424
+
1425
+ # AVOID: Relying on default
1426
+ class MyNamespace < Lutaml::Model::XmlNamespace
1427
+ # Defaults to :unqualified, but intent unclear
1428
+ end
1429
+ ----
1430
+
1431
+ === 3. Use element() for clarity
1432
+
1433
+ [source,ruby]
1434
+ ----
1435
+ # GOOD: Modern, clear
1436
+ xml do
1437
+ element "product"
1438
+ type_name "ProductType"
1439
+ end
1440
+
1441
+ # ACCEPTABLE: Backward compatible
1442
+ xml do
1443
+ root "product"
1444
+ end
1445
+ ----
1446
+
1447
+ === 4. Document your schemas
1448
+
1449
+ [source,ruby]
1450
+ ----
1451
+ class ProductNamespace < Lutaml::Model::XmlNamespace
1452
+ uri 'https://example.com/product/v1'
1453
+ version '1.0'
1454
+ documentation "Product catalog schema for Example Corp"
1455
+ end
1456
+
1457
+ class Product < Lutaml::Model::Serializable
1458
+ xml do
1459
+ element "product"
1460
+ namespace ProductNamespace
1461
+ documentation "Represents a product in the catalog"
1462
+ end
1463
+ end
1464
+ ----
1465
+
1466
+ == Troubleshooting
1467
+
1468
+ === Element not getting prefix
1469
+
1470
+ *Problem*: Using `to_xml(prefix: true)` but child elements don't have prefix
1471
+
1472
+ *Cause*: Child elements are unqualified (`:unqualified` form default)
1473
+
1474
+ *Solution*: Make children qualified via:
1475
+
1476
+ [source,ruby]
1477
+ ----
1478
+ # Option 1: Schema-level
1479
+ class MyNamespace < Lutaml::Model::XmlNamespace
1480
+ element_form_default :qualified
1481
+ end
1482
+
1483
+ # Option 2: Per-element
1484
+ map_element "child", to: :value, form: :qualified
1485
+ ----
1486
+
1487
+ === Parsing fails with different prefix
1488
+
1489
+ *Problem*: Model parses with one prefix but not another
1490
+
1491
+ *Cause*: Likely not an issue in 0.8.0 (URI-based matching)
1492
+
1493
+ *Solution*: Verify namespace URI is correct in XML:
1494
+
1495
+ [source,ruby]
1496
+ ----
1497
+ # Check if URIs match
1498
+ puts MyNamespace.uri
1499
+ # Compare with xmlns declaration in XML
1500
+ ----
1501
+
1502
+ === Attributes not qualified
1503
+
1504
+ *Problem*: `attribute_form_default :qualified` not working
1505
+
1506
+ *Cause*: Fixed in 0.8.0 - was a bug in 0.7.x
1507
+
1508
+ *Solution*: Ensure you're on version 0.8.0 or later:
1509
+
1510
+ [source,ruby]
1511
+ ----
1512
+ # This now works correctly:
1513
+ class MyNamespace < Lutaml::Model::XmlNamespace
1514
+ attribute_form_default :qualified
1515
+ end
1516
+ ----
1517
+
1518
+ === Deprecation warning for namespace
1519
+
1520
+ *Problem*: `[DEPRECATION] Type::Value.namespace is deprecated`
1521
+
1522
+ *Solution*: Replace with `xml_namespace`:
1523
+
1524
+ [source,ruby]
1525
+ ----
1526
+ # Before
1527
+ class CustomType < Lutaml::Model::Type::String
1528
+ namespace MyNamespace # ⚠️
1529
+
1530
+ # After
1531
+ class CustomType < Lutaml::Model::Type::String
1532
+ xml_namespace MyNamespace # ✅
1533
+ ----
1534
+
1535
+ == See Also
1536
+
1537
+ * link:xml-namespaces[XML Namespaces Guide]
1538
+ * link:xml-namespace-qualification[XML Namespace Qualification]
1539
+ * link:../xml_mappings/06_migration_guide[XML Mappings Migration Guide]
1540
+ * link:../../_migrations/xsd-type-migration[XSD Type Architecture]
1541
+ * link:../../_pages/breaking-changes[Breaking Changes]
1542
+
1543
+ == References
1544
+
1545
+ * https://www.w3.org/TR/REC-xml-names/[W3C Namespaces in XML]
1546
+ * https://www.w3.org/TR/xmlschema-1/[W3C XML Schema Part 1: Structures]
1547
+ * https://www.w3.org/TR/xmlschema-2/[W3C XML Schema Part 2: Datatypes]