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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! Tests for type inheritance and extension chains.

use fastxml::schema::types::TypeDef;
use fastxml::schema::{CompiledSchema, ComplexType, ContentModel, ElementDef};

/// Tests that element type resolution uses namespace-qualified type references.
///
/// When tran:Track element has type_ref="tran:TrackType", it should resolve to
/// tran:TrackType, not gml:TrackType.
#[test]
fn test_element_type_ref_with_namespace() {
    let mut schema = CompiledSchema::new();

    // Create both TrackTypes with different structures
    let gml_track_type = ComplexType::sequence(
        "gml:TrackType",
        vec![ElementDef::new("MovingObjectStatus").with_type("gml:MovingObjectStatusType")],
    );

    let tran_track_type = ComplexType::sequence(
        "tran:TrackType",
        vec![
            ElementDef::new("class")
                .with_type("gml:CodeType")
                .optional(),
        ],
    );

    schema.types.insert(
        "gml:TrackType".to_string(),
        TypeDef::Complex(gml_track_type),
    );
    schema.types.insert(
        "tran:TrackType".to_string(),
        TypeDef::Complex(tran_track_type),
    );

    // Create tran:Track element with namespace-qualified type reference
    let track_element = ElementDef::new("Track").with_type("tran:TrackType");

    schema
        .elements
        .insert("tran:Track".to_string(), track_element);

    // Lookup the element
    let elem = schema.get_element("tran:Track");
    assert!(elem.is_some(), "tran:Track element should be found");

    // Get the type reference and resolve it
    let type_ref = elem.unwrap().type_ref.as_ref().unwrap();
    assert_eq!(type_ref, "tran:TrackType");

    // Resolve the type - should get tran:TrackType, not gml:TrackType
    let resolved_type = schema.get_type(type_ref);
    assert!(resolved_type.is_some(), "tran:TrackType should be resolved");

    if let Some(TypeDef::Complex(complex)) = resolved_type {
        if let ContentModel::Sequence(elements) = &complex.content {
            // Should have "class" element, not "MovingObjectStatus"
            assert_eq!(
                elements[0].name, "class",
                "tran:Track should use tran:TrackType (with 'class'), not gml:TrackType (with 'MovingObjectStatus')"
            );
        } else {
            panic!("expected sequence content");
        }
    }
}

/// Tests that elements defined in a base type via xs:extension are visible in derived types.
///
/// This reproduces a bug where:
/// - TransportationComplexType defines `class`, `function`, `lod1MultiSurface`
/// - RoadType extends TransportationComplexType via xs:extension
/// - Road element uses RoadType
/// - When validating <tran:Road><tran:class>...</tran:class></tran:Road>,
///   the validator fails to find `class` because it doesn't traverse the inheritance chain
#[test]
fn test_inherited_elements_from_base_type_extension() {
    use fastxml::Namespace;
    use fastxml::event::{XmlEvent, XmlEventHandler};
    use fastxml::schema::validator::OnePassSchemaValidator;
    use fastxml::schema::xsd::parse_xsd_multiple;
    use std::sync::Arc;

    // Schema mimicking CityGML Transportation module inheritance chain:
    // AbstractCityObjectType -> AbstractTransportationObjectType -> TransportationComplexType -> RoadType
    let schema_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:gml="http://www.opengis.net/gml"
               targetNamespace="http://www.opengis.net/citygml/transportation/2.0"
               elementFormDefault="qualified">

        <!-- Base type with some elements -->
        <xs:complexType name="AbstractTransportationObjectType" abstract="true">
            <xs:sequence>
                <xs:element name="description" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>

        <!-- TransportationComplexType extends base and adds class, function, lod1MultiSurface -->
        <xs:complexType name="TransportationComplexType">
            <xs:complexContent>
                <xs:extension base="tran:AbstractTransportationObjectType">
                    <xs:sequence>
                        <xs:element name="class" type="xs:string" minOccurs="0"/>
                        <xs:element name="function" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                        <xs:element name="lod1MultiSurface" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

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

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

    let schema = parse_xsd_multiple(&[(
        "http://www.opengis.net/citygml/transportation/2.0/transportation.xsd",
        schema_xsd.as_bytes(),
    )])
    .expect("Failed to compile schema");

    // Debug: Print the type structure
    if let Some(TypeDef::Complex(road_type)) = schema.get_type("tran:RoadType") {
        eprintln!("RoadType content: {:?}", road_type.content);
    }

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

    // Validate XML: <tran:Road><tran:class>道路</tran:class></tran:Road>
    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",
            )],
            line: Some(1),
            column: Some(1),
        })
        .unwrap();

    // This is the problematic element: class is defined in TransportationComplexType,
    // which is the base of RoadType. The validator should find it via inheritance.
    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(2),
            column: Some(1),
        })
        .unwrap();

    validator.handle(&XmlEvent::Text("道路".into())).unwrap();

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

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

    validator.handle(&XmlEvent::Eof).unwrap();
    validator.finish().unwrap();

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

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

    // After the fix, this should pass - class element should be found via inheritance
    assert!(
        errors.is_empty(),
        "Elements from base type (class) should be visible in derived type (RoadType). Errors: {:?}",
        errors
    );
}

/// Test that elements inherited from a base type in a DIFFERENT namespace (via xs:import)
/// are visible in derived types. This reproduces the CityGML issue where:
/// - core:AbstractCityObjectType (defines creationDate) in core namespace
/// - tran:TransportationComplexType extends core:AbstractTransportationObjectType
/// - tran:Road uses tran:RoadType which extends tran:TransportationComplexType
#[test]
fn test_inherited_elements_across_namespaces_via_import() {
    use fastxml::Namespace;
    use fastxml::event::{XmlEvent, XmlEventHandler};
    use fastxml::schema::validator::OnePassSchemaValidator;
    use fastxml::schema::xsd::parse_xsd_multiple;
    use std::sync::Arc;

    // Core schema with AbstractCityObjectType defining creationDate
    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"
               targetNamespace="http://www.opengis.net/citygml/2.0"
               elementFormDefault="qualified">

        <xs:complexType name="AbstractCityObjectType" abstract="true">
            <xs:sequence>
                <xs:element name="creationDate" type="xs:date" minOccurs="0"/>
                <xs:element name="terminationDate" type="xs:date" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:schema>"#;

    // Transportation schema that imports core and extends AbstractCityObjectType
    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"
               targetNamespace="http://www.opengis.net/citygml/transportation/2.0"
               elementFormDefault="qualified">

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

        <!-- Extends core:AbstractCityObjectType to inherit creationDate -->
        <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" maxOccurs="unbounded"/>
                        <xs:element name="lod1MultiSurface" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

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

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

    let schema = parse_xsd_multiple(&[
        (
            "http://www.opengis.net/citygml/2.0/cityGMLBase.xsd",
            core_xsd.as_bytes(),
        ),
        (
            "http://www.opengis.net/citygml/transportation/2.0/transportation.xsd",
            tran_xsd.as_bytes(),
        ),
    ])
    .expect("Failed to compile schema");

    // Debug: Check what's in the type_children_cache for RoadType
    eprintln!("=== Type children cache contents ===");
    for (type_name, flattened) in &schema.type_children_cache {
        if type_name.contains("Road") || type_name.contains("Transportation") {
            eprintln!(
                "{}: {:?}",
                type_name,
                flattened.constraints.keys().collect::<Vec<_>>()
            );
        }
    }

    // Debug: Check the type structure
    if let Some(fastxml::schema::types::TypeDef::Complex(road_type)) = schema.get_type("RoadType") {
        eprintln!("RoadType content: {:?}", road_type.content);
    }
    if let Some(fastxml::schema::types::TypeDef::Complex(trans_type)) =
        schema.get_type("TransportationComplexType")
    {
        eprintln!(
            "TransportationComplexType content: {:?}",
            trans_type.content
        );
    }

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

    // Validate: <tran:Road><core:creationDate>2024-01-01</core:creationDate></tran:Road>
    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"),
            ],
            line: Some(1),
            column: Some(1),
        })
        .unwrap();

    // creationDate is defined in core:AbstractCityObjectType but should be visible via inheritance
    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();

    // Also test tran:class which is defined directly 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();

    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);

    // The test should pass - both creationDate (from core namespace) and class should be found
    assert!(
        errors.is_empty(),
        "Elements inherited across namespaces should be visible. Errors: {:?}",
        errors
    );
}

/// Test deep inheritance chain across multiple namespaces (mimics actual CityGML structure)
/// gml:AbstractGMLType -> gml:AbstractFeatureType -> core:AbstractCityObjectType
///   -> tran:AbstractTransportationObjectType -> tran:TransportationComplexType -> tran:RoadType
#[test]
fn test_deep_inheritance_chain_across_namespaces() {
    use fastxml::Namespace;
    use fastxml::event::{XmlEvent, XmlEventHandler};
    use fastxml::schema::validator::OnePassSchemaValidator;
    use fastxml::schema::xsd::parse_xsd_multiple;
    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:element name="name" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute ref="gml:id"/>
        </xs:complexType>

        <xs:attribute name="id" type="xs:ID"/>

        <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: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:element name="terminationDate" type="xs:date" minOccurs="0"/>
                        <xs:element name="externalReference" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <xs:element name="cityObjectMember" type="xs:string"/>
    </xs:schema>"#;

    // Transportation schema with deep inheritance
    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:import namespace="http://www.opengis.net/citygml/2.0"
                   schemaLocation="http://www.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"/>

        <!-- Intermediate abstract type -->
        <xs:complexType name="AbstractTransportationObjectType" abstract="true">
            <xs:complexContent>
                <xs:extension base="core:AbstractCityObjectType">
                    <xs:sequence>
                        <!-- No additional elements here -->
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <!-- TransportationComplexType adds class, function, etc. -->
        <xs:complexType name="TransportationComplexType">
            <xs:complexContent>
                <xs:extension base="tran:AbstractTransportationObjectType">
                    <xs:sequence>
                        <xs:element name="class" type="xs:string" minOccurs="0"/>
                        <xs:element name="function" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                        <xs:element name="usage" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                        <xs:element name="lod1MultiSurface" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <!-- RoadType extends TransportationComplexType -->
        <xs:complexType name="RoadType">
            <xs:complexContent>
                <xs:extension base="tran:TransportationComplexType">
                    <xs:sequence>
                        <xs:element name="trafficArea" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                        <xs:element name="auxiliaryTrafficArea" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

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

    let schema = parse_xsd_multiple(&[
        (
            "http://schemas.opengis.net/gml/3.1.1/base/gml.xsd",
            gml_xsd.as_bytes(),
        ),
        (
            "http://www.opengis.net/citygml/2.0/cityGMLBase.xsd",
            core_xsd.as_bytes(),
        ),
        (
            "http://www.opengis.net/citygml/transportation/2.0/transportation.xsd",
            tran_xsd.as_bytes(),
        ),
    ])
    .expect("Failed to compile schema");

    // Debug: Check type_children_cache contents for RoadType
    eprintln!("=== Deep inheritance test: type_children_cache ===");
    if let Some(flattened) = schema.type_children_cache.get("RoadType") {
        eprintln!(
            "RoadType children: {:?}",
            flattened.constraints.keys().collect::<Vec<_>>()
        );
    } else {
        eprintln!("RoadType not found in cache!");
    }
    if let Some(flattened) = schema.type_children_cache.get("tran:RoadType") {
        eprintln!(
            "tran:RoadType children: {:?}",
            flattened.constraints.keys().collect::<Vec<_>>()
        );
    }

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

    // Start Road element
    validator
        .handle(&XmlEvent::StartElement {
            name: "Road".into(),
            prefix: Some("tran".into()),
            namespace: Some("http://www.opengis.net/citygml/transportation/2.0".into()),
            attributes: vec![("gml:id".into(), "road_1".into())],
            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 element from gml:AbstractFeatureType (3 levels up)
    validator
        .handle(&XmlEvent::StartElement {
            name: "boundedBy".into(),
            prefix: Some("gml".into()),
            namespace: Some("http://www.opengis.net/gml".into()),
            attributes: vec![],
            namespace_decls: vec![],
            line: Some(2),
            column: Some(1),
        })
        .unwrap();
    validator
        .handle(&XmlEvent::Text("envelope".into()))
        .unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "boundedBy".into(),
            prefix: Some("gml".into()),
        })
        .unwrap();

    // Test element from core:AbstractCityObjectType (2 levels up)
    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(3),
            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 element from tran:TransportationComplexType (1 level up)
    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(4),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("9999".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "class".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    // Test element from tran:TransportationComplexType
    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(5),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("9020".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "function".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    // Test element from tran:TransportationComplexType
    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(6),
            column: Some(1),
        })
        .unwrap();
    validator.handle(&XmlEvent::Text("surface".into())).unwrap();
    validator
        .handle(&XmlEvent::EndElement {
            name: "lod1MultiSurface".into(),
            prefix: Some("tran".into()),
        })
        .unwrap();

    // End Road element
    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);

    // All elements from the deep inheritance chain should be visible
    assert!(
        errors.is_empty(),
        "Elements from deep inheritance chain should be visible. Errors: {:?}",
        errors
    );
}

/// Test that same-namespace inheritance works when base type is referenced without prefix.
///
/// In CityGML schemas, types in the same namespace extend each other without prefix:
/// - RoadType extends TransportationComplexType (not tran:TransportationComplexType)
///
/// This test verifies that the inheritance chain is resolved correctly even when
/// the base type reference has no namespace prefix.
#[test]
fn test_same_namespace_inheritance_without_prefix() {
    use fastxml::schema::xsd::{compile_schemas, parser::parse_xsd_ast, register_builtin_types};

    // Schema where types extend other types in the same namespace WITHOUT prefix
    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"
               targetNamespace="http://www.opengis.net/citygml/transportation/2.0"
               elementFormDefault="qualified">

        <!-- Base type with elements -->
        <xs:complexType name="TransportationComplexType">
            <xs:sequence>
                <xs:element name="class" type="xs:string" minOccurs="0"/>
                <xs:element name="function" type="xs:string" minOccurs="0"/>
                <xs:element name="usage" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>

        <!-- Derived type extends base WITHOUT prefix (same namespace) -->
        <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="tran:RoadType"/>
    </xs:schema>"#;

    let tran_ast = parse_xsd_ast(tran_xsd.as_bytes()).unwrap();
    let mut compiled = compile_schemas(vec![tran_ast]).expect("Failed to compile schemas");
    register_builtin_types(&mut compiled);

    eprintln!("=== Same-namespace inheritance test ===");

    // Check that types are stored
    eprintln!(
        "Types in schema: {:?}",
        compiled.types.keys().collect::<Vec<_>>()
    );

    // RoadType should inherit elements from TransportationComplexType
    let road_type_cache = compiled.type_children_cache.get("RoadType");
    eprintln!(
        "RoadType cache: {:?}",
        road_type_cache.map(|f| f.constraints.keys().collect::<Vec<_>>())
    );

    let road_type_cache = road_type_cache.expect("RoadType should be in cache");

    // Own element
    assert!(
        road_type_cache.constraints.contains_key("trafficArea"),
        "RoadType should have trafficArea (own element)"
    );

    // Inherited elements from TransportationComplexType (extended WITHOUT prefix)
    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("usage"),
        "RoadType should have usage (inherited from TransportationComplexType)"
    );
}