fastxml 0.8.1

A fast, memory-efficient XML library with XPath and XSD validation support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! Tests for duplicate schema handling and default namespace.

/// Test that duplicate schemas (same schema resolved multiple times) don't break compilation.
/// This reproduces the issue in compile_schema_for_streaming where resolve_all is called
/// for each schema location, potentially adding the same dependency schemas multiple times.
#[test]
fn test_duplicate_schemas_in_compile_schemas() {
    use fastxml::Namespace;
    use fastxml::event::{XmlEvent, XmlEventHandler};
    use fastxml::schema::validator::OnePassSchemaValidator;
    use fastxml::schema::xsd::{compile_schemas, parser::parse_xsd_ast};
    use std::sync::Arc;

    // GML base schema
    let gml_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/gml"
               elementFormDefault="qualified">

        <xs:complexType name="AbstractGMLType" abstract="true">
            <xs:sequence>
                <xs:element name="description" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>

        <xs:complexType name="AbstractFeatureType" abstract="true">
            <xs:complexContent>
                <xs:extension base="gml:AbstractGMLType">
                    <xs:sequence>
                        <xs:element name="boundedBy" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:schema>"#;

    // Core CityGML schema
    let core_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:core="http://www.opengis.net/citygml/2.0"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/citygml/2.0"
               elementFormDefault="qualified">

        <xs:complexType name="AbstractCityObjectType" abstract="true">
            <xs:complexContent>
                <xs:extension base="gml:AbstractFeatureType">
                    <xs:sequence>
                        <xs:element name="creationDate" type="xs:date" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:schema>"#;

    // Transportation schema
    let tran_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:tran="http://www.opengis.net/citygml/transportation/2.0"
               xmlns:core="http://www.opengis.net/citygml/2.0"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/citygml/transportation/2.0"
               elementFormDefault="qualified">

        <xs:complexType name="TransportationComplexType">
            <xs:complexContent>
                <xs:extension base="core:AbstractCityObjectType">
                    <xs:sequence>
                        <xs:element name="class" type="xs:string" minOccurs="0"/>
                        <xs:element name="function" type="xs:string" minOccurs="0"/>
                        <xs:element name="lod1MultiSurface" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <xs:complexType name="RoadType">
            <xs:complexContent>
                <xs:extension base="tran:TransportationComplexType">
                    <xs:sequence>
                        <xs:element name="trafficArea" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <xs:element name="Road" type="tran:RoadType"/>
    </xs:schema>"#;

    // Parse each schema
    let gml_ast = parse_xsd_ast(gml_xsd.as_bytes()).unwrap();
    let core_ast = parse_xsd_ast(core_xsd.as_bytes()).unwrap();
    let tran_ast = parse_xsd_ast(tran_xsd.as_bytes()).unwrap();

    // Simulate what compile_schema_for_streaming does:
    // Each resolve_all call returns the schema + its dependencies
    // So we get duplicates when we extend all_schemas
    //
    // IMPORTANT: In compile_schema_for_streaming, the order depends on xsi:schemaLocation
    // which might process tran BEFORE core is resolved, leading to:
    // First "resolve_all" for tran returns: [gml, core, tran] (resolves dependencies)
    // Second "resolve_all" for core returns: [gml, core] (resolved again!)
    // Third "resolve_all" for gml returns: [gml] (resolved again!)
    // Result: [gml, core, tran, gml, core, gml] - tran comes BEFORE core is "properly" resolved

    let all_schemas = vec![
        gml_ast.clone(),  // from tran resolve (dependency)
        core_ast.clone(), // from tran resolve (dependency)
        tran_ast,         // from tran resolve (entry)
        gml_ast.clone(),  // from core resolve (dependency)
        core_ast.clone(), // from core resolve (entry)
        gml_ast.clone(),  // from gml resolve (entry)
    ];

    eprintln!("=== Duplicate schemas test ===");
    eprintln!("Number of schemas: {}", all_schemas.len());

    let mut compiled = compile_schemas(all_schemas).expect("Failed to compile schemas");
    fastxml::schema::xsd::register_builtin_types(&mut compiled);

    // Debug: Check what's in the type_children_cache
    eprintln!(
        "Types in schema: {:?}",
        compiled.types.keys().collect::<Vec<_>>()
    );
    if let Some(flattened) = compiled.type_children_cache.get("RoadType") {
        eprintln!(
            "RoadType children: {:?}",
            flattened.constraints.keys().collect::<Vec<_>>()
        );
    }
    if let Some(flattened) = compiled.type_children_cache.get("tran:RoadType") {
        eprintln!(
            "tran:RoadType children: {:?}",
            flattened.constraints.keys().collect::<Vec<_>>()
        );
    }

    let mut validator = OnePassSchemaValidator::new(Arc::new(compiled));

    // Test validation
    validator
        .handle(&XmlEvent::StartElement {
            name: "Road".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![
                Namespace::new("tran", "http://www.opengis.net/citygml/transportation/2.0"),
                Namespace::new("core", "http://www.opengis.net/citygml/2.0"),
                Namespace::new("gml", "http://www.opengis.net/gml"),
            ],
            line: Some(1),
            column: Some(1),
        })
        .unwrap();

    // Test core:creationDate (inherited from core:AbstractCityObjectType)
    validator
        .handle(&XmlEvent::StartElement {
            name: "creationDate".into(),
            prefix: Some("core".into()),
            namespace: Some("http://www.opengis.net/citygml/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(2),
            column: Some(1),
        })
        .unwrap();
    validator
        .handle(&XmlEvent::Text("2024-01-01".into()))
        .unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "creationDate".into(),
            prefix: Some("core".into()),
        })
        .unwrap();

    // Test tran:class (defined in TransportationComplexType)
    validator
        .handle(&XmlEvent::StartElement {
            name: "class".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(3),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("9999".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "class".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    // Test tran:function
    validator
        .handle(&XmlEvent::StartElement {
            name: "function".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(4),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("9020".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "function".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    // Test tran:lod1MultiSurface
    validator
        .handle(&XmlEvent::StartElement {
            name: "lod1MultiSurface".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(5),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("surface".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "lod1MultiSurface".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    validator
        .handle(&XmlEvent::EndElement {
            name: "Road".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    let errors: Vec<_> = validator
        .errors()
        .iter()
        .filter(|e| e.message.contains("not declared"))
        .collect();

    eprintln!("Validation errors: {:?}", errors);

    assert!(
        errors.is_empty(),
        "Duplicate schemas should not break inheritance. Errors: {:?}",
        errors
    );
}

/// Test with schema using default namespace (xmlns="...") instead of prefixed namespace.
/// This reproduces the actual OGC CityGML schema pattern where:
/// - transportation.xsd uses xmlns="http://www.opengis.net/citygml/transportation/2.0" (default)
/// - Types are stored without prefix (e.g., "RoadType" not "tran:RoadType")
/// - But XML uses prefixed names (tran:Road)
#[test]
fn test_schema_with_default_namespace() {
    use fastxml::Namespace;
    use fastxml::event::{XmlEvent, XmlEventHandler};
    use fastxml::schema::validator::OnePassSchemaValidator;
    use fastxml::schema::xsd::{compile_schemas, parser::parse_xsd_ast, register_builtin_types};
    use std::sync::Arc;

    // GML base schema (uses gml: prefix for its own namespace)
    let gml_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/gml"
               elementFormDefault="qualified">

        <xs:complexType name="AbstractGMLType" abstract="true">
            <xs:sequence>
                <xs:element name="description" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>

        <xs:complexType name="AbstractFeatureType" abstract="true">
            <xs:complexContent>
                <xs:extension base="gml:AbstractGMLType">
                    <xs:sequence>
                        <xs:element name="boundedBy" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:schema>"#;

    // Core CityGML schema - uses DEFAULT namespace for its own types!
    // This matches the actual OGC schema pattern
    let core_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="http://www.opengis.net/citygml/2.0"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/citygml/2.0"
               elementFormDefault="qualified">

        <xs:import namespace="http://www.opengis.net/gml"
                   schemaLocation="http://schemas.opengis.net/gml/3.1.1/base/gml.xsd"/>

        <xs:complexType name="AbstractCityObjectType" abstract="true">
            <xs:complexContent>
                <xs:extension base="gml:AbstractFeatureType">
                    <xs:sequence>
                        <xs:element name="creationDate" type="xs:date" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <xs:element name="_CityObject" type="AbstractCityObjectType" abstract="true"
                    substitutionGroup="gml:_Feature"/>
    </xs:schema>"#;

    // Transportation schema - also uses DEFAULT namespace for its own types!
    let tran_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="http://www.opengis.net/citygml/transportation/2.0"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:core="http://www.opengis.net/citygml/2.0"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/citygml/transportation/2.0"
               elementFormDefault="qualified">

        <xs:import namespace="http://www.opengis.net/citygml/2.0"
                   schemaLocation="http://schemas.opengis.net/citygml/2.0/cityGMLBase.xsd"/>
        <xs:import namespace="http://www.opengis.net/gml"
                   schemaLocation="http://schemas.opengis.net/gml/3.1.1/base/gml.xsd"/>

        <xs:complexType name="AbstractTransportationObjectType" abstract="true">
            <xs:complexContent>
                <xs:extension base="core:AbstractCityObjectType">
                    <xs:sequence/>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <xs:complexType name="TransportationComplexType">
            <xs:complexContent>
                <xs:extension base="AbstractTransportationObjectType">
                    <xs:sequence>
                        <xs:element name="class" type="xs:string" minOccurs="0"/>
                        <xs:element name="function" type="xs:string" minOccurs="0"/>
                        <xs:element name="lod1MultiSurface" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <xs:complexType name="RoadType">
            <xs:complexContent>
                <xs:extension base="TransportationComplexType">
                    <xs:sequence>
                        <xs:element name="trafficArea" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <xs:element name="Road" type="RoadType" substitutionGroup="core:_CityObject"/>
    </xs:schema>"#;

    // Parse schemas
    let gml_ast = parse_xsd_ast(gml_xsd.as_bytes()).unwrap();
    let core_ast = parse_xsd_ast(core_xsd.as_bytes()).unwrap();
    let tran_ast = parse_xsd_ast(tran_xsd.as_bytes()).unwrap();

    eprintln!("=== Default namespace test ===");
    eprintln!("gml namespace_bindings: {:?}", gml_ast.namespace_bindings);
    eprintln!("core namespace_bindings: {:?}", core_ast.namespace_bindings);
    eprintln!("tran namespace_bindings: {:?}", tran_ast.namespace_bindings);

    let all_schemas = vec![gml_ast, core_ast, tran_ast];

    let mut compiled = compile_schemas(all_schemas).expect("Failed to compile schemas");
    register_builtin_types(&mut compiled);

    // Debug: Check type storage
    eprintln!("Types in schema:");
    for type_name in compiled.types.keys() {
        if type_name.contains("Road")
            || type_name.contains("Transportation")
            || type_name.contains("CityObject")
        {
            eprintln!("  {}", type_name);
        }
    }

    // Debug: Check type_children_cache
    eprintln!("Type children cache:");
    for (type_name, flattened) in &compiled.type_children_cache {
        if type_name.contains("Road") {
            eprintln!(
                "  {}: {:?}",
                type_name,
                flattened.constraints.keys().collect::<Vec<_>>()
            );
        }
    }

    // Debug: Check elements
    eprintln!("Elements:");
    for (elem_name, elem_def) in &compiled.elements {
        if elem_name.contains("Road") {
            eprintln!("  {} -> type_ref: {:?}", elem_name, elem_def.type_ref);
        }
    }

    let mut validator = OnePassSchemaValidator::new(Arc::new(compiled));

    // Test validation with prefixed element names (as used in actual XML)
    validator
        .handle(&XmlEvent::StartElement {
            name: "Road".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![
                Namespace::new("tran", "http://www.opengis.net/citygml/transportation/2.0"),
                Namespace::new("core", "http://www.opengis.net/citygml/2.0"),
                Namespace::new("gml", "http://www.opengis.net/gml"),
            ],
            line: Some(1),
            column: Some(1),
        })
        .unwrap();

    // Test core:creationDate (inherited from core:AbstractCityObjectType)
    validator
        .handle(&XmlEvent::StartElement {
            name: "creationDate".into(),
            prefix: Some("core".into()),
            namespace: Some("http://www.opengis.net/citygml/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(2),
            column: Some(1),
        })
        .unwrap();
    validator
        .handle(&XmlEvent::Text("2024-01-01".into()))
        .unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "creationDate".into(),
            prefix: Some("core".into()),
        })
        .unwrap();

    // Test tran:class
    validator
        .handle(&XmlEvent::StartElement {
            name: "class".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(3),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("9999".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "class".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    // Test tran:function
    validator
        .handle(&XmlEvent::StartElement {
            name: "function".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(4),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("9020".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "function".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    // Test tran:lod1MultiSurface
    validator
        .handle(&XmlEvent::StartElement {
            name: "lod1MultiSurface".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(5),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("surface".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "lod1MultiSurface".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    validator
        .handle(&XmlEvent::EndElement {
            name: "Road".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    let errors: Vec<_> = validator
        .errors()
        .iter()
        .filter(|e| e.message.contains("not declared"))
        .collect();

    eprintln!("Validation errors: {:?}", errors);

    assert!(
        errors.is_empty(),
        "Schema with default namespace should work. Errors: {:?}",
        errors
    );
}

/// Test that duplicate schemas (same targetNamespace appearing multiple times) are properly
/// deduplicated and don't break the inheritance chain.
/// This reproduces the issue found in compile_schema_for_streaming where resolve_all is called
/// for each schema location, causing the same schema to be added to all_schemas multiple times.
/// Without deduplication, the inheritance chain breaks because namespace bindings get overwritten.
#[test]
fn test_duplicate_schemas_deduplication() {
    use fastxml::schema::xsd::{compile_schemas, parser::parse_xsd_ast, register_builtin_types};

    // GML schema
    let gml_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/gml"
               elementFormDefault="qualified">
        <xs:complexType name="AbstractFeatureType" abstract="true">
            <xs:sequence>
                <xs:element name="boundedBy" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:schema>"#;

    // Core schema
    let core_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="http://www.opengis.net/citygml/2.0"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/citygml/2.0"
               elementFormDefault="qualified">
        <xs:complexType name="AbstractCityObjectType" abstract="true">
            <xs:complexContent>
                <xs:extension base="gml:AbstractFeatureType">
                    <xs:sequence>
                        <xs:element name="creationDate" type="xs:date" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:schema>"#;

    // Transportation schema
    let tran_xsd = r#"<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="http://www.opengis.net/citygml/transportation/2.0"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:core="http://www.opengis.net/citygml/2.0"
               xmlns:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/citygml/transportation/2.0"
               elementFormDefault="qualified">
        <xs:complexType name="TransportationComplexType">
            <xs:complexContent>
                <xs:extension base="core:AbstractCityObjectType">
                    <xs:sequence>
                        <xs:element name="class" type="xs:string" minOccurs="0"/>
                        <xs:element name="function" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
        <xs:complexType name="RoadType">
            <xs:complexContent>
                <xs:extension base="TransportationComplexType">
                    <xs:sequence>
                        <xs:element name="trafficArea" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
        <xs:element name="Road" type="RoadType"/>
    </xs:schema>"#;

    let gml_ast = parse_xsd_ast(gml_xsd.as_bytes()).unwrap();
    let core_ast = parse_xsd_ast(core_xsd.as_bytes()).unwrap();
    let tran_ast = parse_xsd_ast(tran_xsd.as_bytes()).unwrap();

    // Simulate what happens in compile_schema_for_streaming:
    // Each resolve_all returns the entry schema + its dependencies
    // This causes massive duplication!
    let all_schemas = vec![
        // First resolve_all for tran: returns [gml, core, tran]
        gml_ast.clone(),
        core_ast.clone(),
        tran_ast.clone(),
        // Second resolve_all for core: returns [gml, core]
        gml_ast.clone(),
        core_ast.clone(),
        // Third resolve_all for gml: returns [gml]
        gml_ast.clone(),
        // Fourth resolve_all for some other schema that depends on core
        gml_ast.clone(),
        core_ast.clone(),
        // More duplicates...
        gml_ast,
        core_ast,
        tran_ast,
    ];

    eprintln!("=== Duplicate schemas deduplication test ===");
    eprintln!("Input schemas count: {}", all_schemas.len());

    let mut compiled = compile_schemas(all_schemas).expect("Failed to compile schemas");
    register_builtin_types(&mut compiled);

    // Check that RoadType has inherited elements from the full chain
    let road_type_cache = compiled.type_children_cache.get("RoadType");
    eprintln!(
        "RoadType cache: {:?}",
        road_type_cache.map(|f| f.constraints.keys().collect::<Vec<_>>())
    );

    // RoadType should have:
    // - trafficArea (own)
    // - class, function (from TransportationComplexType)
    // - creationDate (from AbstractCityObjectType)
    // - boundedBy (from AbstractFeatureType)
    let road_type_cache = road_type_cache.expect("RoadType should be in cache");
    assert!(
        road_type_cache.constraints.contains_key("trafficArea"),
        "RoadType should have trafficArea"
    );
    assert!(
        road_type_cache.constraints.contains_key("class"),
        "RoadType should have class (inherited from TransportationComplexType)"
    );
    assert!(
        road_type_cache.constraints.contains_key("function"),
        "RoadType should have function (inherited from TransportationComplexType)"
    );
    assert!(
        road_type_cache.constraints.contains_key("creationDate"),
        "RoadType should have creationDate (inherited from AbstractCityObjectType)"
    );
    assert!(
        road_type_cache.constraints.contains_key("boundedBy"),
        "RoadType should have boundedBy (inherited from AbstractFeatureType)"
    );

    // Verify no double-prefix keys like "gml:tran:RoadType" or "core:tran:RoadType"
    // This was a bug where prefixes were incorrectly concatenated
    let double_prefix_keys: Vec<_> = compiled
        .type_children_cache
        .keys()
        .filter(|k| k.matches(':').count() >= 2)
        .collect();
    assert!(
        double_prefix_keys.is_empty(),
        "Should not have double-prefix keys in type_children_cache, found: {:?}",
        double_prefix_keys
    );

    // Also verify that types are correctly registered with proper prefixes
    // The tran namespace types should be accessible
    assert!(
        compiled
            .type_children_cache
            .contains_key("TransportationComplexType"),
        "Should have TransportationComplexType in cache"
    );
    assert!(
        compiled
            .type_children_cache
            .contains_key("AbstractCityObjectType"),
        "Should have AbstractCityObjectType in cache"
    );
    assert!(
        compiled
            .type_children_cache
            .contains_key("AbstractFeatureType"),
        "Should have AbstractFeatureType in cache"
    );
}