xsd-schema 0.1.0

XML Schema (XSD 1.0/1.1) validator with PSVI and a built-in XPath 2.0 engine
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
//! Simple type value validation
//!
//! Validates text content against XSD simple types (atomic, list, union).
//! Reuses `VALIDATOR_REGISTRY` for lexical validation and `FacetSet` for
//! facet constraint checking.

use crate::ids::{SimpleTypeKey, TypeKey};
use crate::parser::frames::{ComplexContentResult, DerivationMethod, SimpleTypeVariety};
use crate::schema::SchemaSet;
use crate::types::facets::{normalize_whitespace, FacetSet, WhitespaceMode};
use crate::types::validators::VALIDATOR_REGISTRY;
use crate::types::value::{XmlValue, XmlValueKind};
use crate::types::XmlTypeCode;

use super::errors::{self, ValidationError};

/// Result of simple type validation
#[derive(Debug)]
pub struct SimpleTypeResult {
    /// The typed value produced by validation
    pub typed_value: XmlValue,
    /// For union types: the member type that matched
    pub member_type: Option<TypeKey>,
    /// The whitespace-normalized value (PSVI `[schema normalized value]`)
    pub normalized_value: Option<String>,
}

/// Validate a string value against a simple type.
///
/// Dispatches to atomic, list, or union validation depending on the type's variety.
/// For complex types with simpleContent, walks the base type chain to find the
/// underlying simple type.
pub fn validate_simple_type(
    value: &str,
    type_key: TypeKey,
    schema_set: &SchemaSet,
) -> Result<SimpleTypeResult, ValidationError> {
    validate_simple_type_inner(value, type_key, schema_set)
}

/// Value-space equality for fixed/default values (§3.3.4.3 clause 5.2.2.2.2, §3.2.4.4).
///
/// Fast-path lexical match; otherwise parses both strings against `type_key`
/// and compares their typed values. Returns `false` when `type_key` is `None`
/// or when either parse fails.
pub fn fixed_values_equal(
    a: &str,
    b: &str,
    type_key: Option<TypeKey>,
    schema_set: &SchemaSet,
) -> bool {
    if a == b {
        return true;
    }
    let Some(tk) = type_key else {
        return false;
    };
    let Ok(a_result) = validate_simple_type(a, tk, schema_set) else {
        return false;
    };
    let Ok(b_result) = validate_simple_type(b, tk, schema_set) else {
        return false;
    };
    a_result.typed_value == b_result.typed_value
}

/// Value-space equality when the instance's typed value is already computed.
///
/// Avoids re-parsing `instance_raw`; only parses `fixed` on lexical mismatch.
/// Returns `false` when `type_key` is `None` or when parsing `fixed` fails.
pub fn fixed_matches_typed(
    instance_raw: &str,
    instance_typed: &XmlValue,
    fixed: &str,
    type_key: Option<TypeKey>,
    schema_set: &SchemaSet,
) -> bool {
    if instance_raw == fixed {
        return true;
    }
    let Some(tk) = type_key else {
        return false;
    };
    let Ok(fixed_result) = validate_simple_type(fixed, tk, schema_set) else {
        return false;
    };
    *instance_typed == fixed_result.typed_value
}

fn validate_simple_type_inner(
    value: &str,
    type_key: TypeKey,
    schema_set: &SchemaSet,
) -> Result<SimpleTypeResult, ValidationError> {
    match type_key {
        TypeKey::Simple(sk) => {
            let st_data = match schema_set.arenas.simple_types.get(sk) {
                Some(d) => d,
                None => {
                    return Err(errors::error(
                        "cvc-simple-type",
                        "Simple type definition not found",
                        None,
                    ));
                }
            };
            match st_data.variety {
                SimpleTypeVariety::Atomic => validate_atomic_type(value, sk, schema_set),
                SimpleTypeVariety::List => validate_list_type(value, sk, schema_set),
                SimpleTypeVariety::Union => validate_union_type(value, sk, schema_set),
            }
        }
        TypeKey::Complex(ck) => {
            // §3.4.2.2 clause 1.1: the inline <xs:simpleType> inside
            // simpleContent/restriction is the content type when present;
            // otherwise inherit by walking resolved_base_type.
            let ct_data = match schema_set.arenas.complex_types.get(ck) {
                Some(d) => d,
                None => {
                    return Err(errors::error(
                        "cvc-simple-type",
                        "Complex type definition not found",
                        None,
                    ));
                }
            };
            let result = if let Some(inline_st) = ct_data.resolved_simple_content_type {
                validate_simple_type_inner(value, inline_st, schema_set)?
            } else if let Some(base_key) = ct_data.resolved_base_type {
                validate_simple_type_inner(value, base_key, schema_set)?
            } else {
                // No base type — treat as anySimpleType (accept any value)
                SimpleTypeResult {
                    typed_value: XmlValue::untyped(value),
                    member_type: None,
                    normalized_value: None,
                }
            };
            // §3.4.2.2 clause 1.2: a simpleContent restriction may declare
            // local string-applicable facets (length / pattern / enumeration)
            // that further constrain the recursively validated value. The
            // base/inline call already applied the base type's facets; here we
            // apply only the restriction's local set. Extension cannot add
            // facets, so it is excluded.
            if let ComplexContentResult::Simple(sc) = &ct_data.content {
                if sc.derivation == DerivationMethod::Restriction && !sc.facets.is_empty() {
                    let lex = result.normalized_value.as_deref().unwrap_or(value);
                    sc.facets.validate_string(lex).map_err(|e| {
                        let code = errors::facet_constraint_code(&e);
                        errors::from_facet_error(code, e, None)
                    })?;
                }
            }
            Ok(result)
        }
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Walk the resolved_base_type chain from `sk` until we find a built-in type code.
/// Returns `None` if no built-in ancestor is found (cycle guard at depth 100).
fn resolve_type_code(sk: SimpleTypeKey, schema_set: &SchemaSet) -> Option<XmlTypeCode> {
    let mut current = sk;
    for _ in 0..100 {
        if let Some(code) = schema_set.get_type_code(current) {
            return Some(code);
        }
        let st_data = schema_set.arenas.simple_types.get(current)?;
        match st_data.resolved_base_type {
            Some(TypeKey::Simple(base)) => current = base,
            _ => return None,
        }
    }
    None
}

/// Walk the simple-type derivation chain and return `true` if any
/// ancestor (including `sk` itself) declares an `enumeration` facet.
/// `FacetSet::inherit_from` does not propagate enumeration through base
/// types, so [`collect_facets`] alone cannot answer this question.
fn has_enumeration_in_chain(sk: SimpleTypeKey, schema_set: &SchemaSet) -> bool {
    let mut current = sk;
    for _ in 0..100 {
        let Some(st_data) = schema_set.arenas.simple_types.get(current) else {
            return false;
        };
        if st_data.facets.enumeration.is_some() {
            return true;
        }
        match st_data.resolved_base_type {
            Some(TypeKey::Simple(base)) => current = base,
            _ => return false,
        }
    }
    false
}

/// Collect the effective facet set for a simple type by walking the derivation chain.
///
/// Starts from the most-derived type's facets, then inherits from each base type.
/// `inherit_from` only fills missing values, so derived facets take priority.
fn collect_facets(sk: SimpleTypeKey, schema_set: &SchemaSet) -> FacetSet {
    let st_data = match schema_set.arenas.simple_types.get(sk) {
        Some(d) => d,
        None => return FacetSet::new(),
    };
    let mut facets = st_data.facets.clone();

    // Walk the base type chain
    let mut current_base = st_data.resolved_base_type;
    for _ in 0..100 {
        match current_base {
            Some(TypeKey::Simple(base_sk)) => {
                if let Some(base_data) = schema_set.arenas.simple_types.get(base_sk) {
                    facets.inherit_from(&base_data.facets);
                    current_base = base_data.resolved_base_type;
                } else {
                    break;
                }
            }
            _ => break,
        }
    }
    facets
}

// ---------------------------------------------------------------------------
// Atomic
// ---------------------------------------------------------------------------

fn validate_atomic_type(
    value: &str,
    sk: SimpleTypeKey,
    schema_set: &SchemaSet,
) -> Result<SimpleTypeResult, ValidationError> {
    let type_code = resolve_type_code(sk, schema_set);

    // Short-circuit for abstract base types — accept as untyped
    match type_code {
        Some(XmlTypeCode::AnySimpleType) | Some(XmlTypeCode::AnyAtomicType) | None => {
            return Ok(SimpleTypeResult {
                typed_value: XmlValue::untyped(value),
                member_type: None,
                normalized_value: None,
            });
        }
        _ => {}
    }
    let type_code = type_code.unwrap();

    let validator = match VALIDATOR_REGISTRY.get_by_code(type_code) {
        Some(v) => v,
        None => {
            return Err(errors::error(
                "cvc-datatype-valid",
                format!("No validator for type code {:?}", type_code),
                None,
            ));
        }
    };

    let facets = collect_facets(sk, schema_set);

    // XSD Part 2 §3.2 / W3C bug 14388: `xs:NOTATION` is only usable via
    // restriction with an `enumeration` facet binding the value to a
    // declared notation. The schema may legally reference `xs:NOTATION`
    // directly, but any instance value must fail because there is no
    // enumeration to match against.
    if type_code == XmlTypeCode::Notation && !has_enumeration_in_chain(sk, schema_set) {
        return Err(errors::error(
            "cvc-datatype-valid",
            "xs:NOTATION cannot be used directly for instance validation; \
             only datatypes derived from NOTATION by restriction with an \
             enumeration facet are permitted (§3.2)",
            None,
        ));
    }

    // Compute normalized value (PSVI [schema normalized value])
    let effective_ws = facets
        .whitespace
        .as_ref()
        .map(|w| w.value)
        .unwrap_or_else(|| validator.whitespace());
    let normalized = normalize_whitespace(value, effective_ws);

    let typed_value = if facets.is_empty() {
        validator.validate(value)
    } else {
        validator.validate_with_facets(value, &facets)
    };

    match typed_value {
        Ok(mut val) => {
            // Propagate schema type so XPath2 sequence type matching works
            if val.schema_type.is_none() {
                val.schema_type = Some(sk);
            }
            // XSD 1.0 §3.2.7.2: year zero ("0000") is not allowed in dateTime,
            // date, gYear, or gYearMonth lexical forms. XSD 1.1 explicitly
            // permits 0000 as the representation of 1 BCE.
            // XSD 1.0 §3.2.4 / §3.2.5 also omit `+INF` from the special
            // float / double lexicals enumerated by XSD 1.1 §3.3.16/17.
            if schema_set.is_xsd10() {
                check_xsd10_year_zero(&val, value)?;
                check_xsd10_plus_inf(&val, value)?;
            }
            // XSD 1.1: evaluate assertion facets
            #[cfg(feature = "xsd11")]
            evaluate_assertion_facets(&val, &facets, schema_set, Some(sk))?;
            Ok(SimpleTypeResult {
                typed_value: val,
                member_type: None,
                normalized_value: Some(normalized),
            })
        }
        Err(type_err) => {
            let code = errors::value_error_constraint_code(&type_err);
            Err(errors::from_value_error(code, type_err, None))
        }
    }
}

/// XSD 1.0 §3.2.4 / §3.2.5: `+INF` is not in the lexical space (added in
/// Datatypes 1.1). The collapsed value is checked against the original
/// lexical form (after whitespace collapse, since `xs:float` / `xs:double`
/// declare `whiteSpace = collapse`).
fn check_xsd10_plus_inf(val: &XmlValue, raw: &str) -> Result<(), ValidationError> {
    use crate::types::value::XmlAtomicValue;
    let is_float_or_double = matches!(
        val.value,
        XmlValueKind::Atomic(XmlAtomicValue::Float(_)) | XmlValueKind::Atomic(XmlAtomicValue::Double(_))
    );
    if !is_float_or_double {
        return Ok(());
    }
    let collapsed =
        crate::types::facets::normalize_whitespace(raw, crate::types::facets::WhitespaceMode::Collapse);
    if collapsed == "+INF" {
        Err(errors::error(
            "cvc-datatype-valid",
            format!("'+INF' is not a valid XSD 1.0 float/double lexical form (value '{}')", raw),
            None,
        ))
    } else {
        Ok(())
    }
}

/// XSD 1.0 §3.2.7.2: reject year 0000 in dateTime / date / gYear / gYearMonth.
fn check_xsd10_year_zero(val: &XmlValue, raw: &str) -> Result<(), ValidationError> {
    use crate::types::value::XmlAtomicValue;
    let year_zero = match &val.value {
        XmlValueKind::Atomic(XmlAtomicValue::DateTime(v)) => v.year == 0,
        XmlValueKind::Atomic(XmlAtomicValue::Date(v)) => v.year == 0,
        XmlValueKind::Atomic(XmlAtomicValue::GYear(v)) => v.year == 0,
        XmlValueKind::Atomic(XmlAtomicValue::GYearMonth(v)) => v.year == 0,
        _ => false,
    };
    if year_zero {
        Err(errors::error(
            "cvc-datatype-valid",
            format!(
                "Year 0000 is not a valid XSD 1.0 lexical form (value '{}'); use '-0001' for 1 BCE",
                raw
            ),
            None,
        ))
    } else {
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// List
// ---------------------------------------------------------------------------

fn validate_list_type(
    value: &str,
    sk: SimpleTypeKey,
    schema_set: &SchemaSet,
) -> Result<SimpleTypeResult, ValidationError> {
    let st_data = match schema_set.arenas.simple_types.get(sk) {
        Some(d) => d,
        None => {
            return Err(errors::error(
                "cvc-simple-type",
                "List type definition not found",
                None,
            ));
        }
    };

    let item_type_key = match st_data.resolved_item_type {
        Some(tk) => tk,
        None => {
            if let Some(deferred) = &st_data.deferred_item_type_error {
                return Err(errors::error(
                    "src-resolve",
                    deferred.message.clone(),
                    None,
                ));
            }
            // No item type resolved — cannot validate list items, accept as untyped
            return Ok(SimpleTypeResult {
                typed_value: XmlValue::untyped(value),
                member_type: None,
                normalized_value: None,
            });
        }
    };

    // Normalize whitespace (collapse) and split
    let normalized = normalize_whitespace(value, WhitespaceMode::Collapse);
    let items_str: Vec<&str> = if normalized.is_empty() {
        Vec::new()
    } else {
        normalized.split(' ').collect()
    };

    // Validate each item
    let mut items = Vec::with_capacity(items_str.len());
    let mut item_type_code = XmlTypeCode::UntypedAtomic;
    for item_str in &items_str {
        let result = validate_simple_type_inner(item_str, item_type_key, schema_set)?;
        item_type_code = result.typed_value.type_code;
        match result.typed_value.value {
            XmlValueKind::Atomic(atom) => items.push(atom),
            XmlValueKind::UntypedAtomic(s) => {
                items.push(crate::types::value::XmlAtomicValue::String(s));
            }
            _ => {
                items.push(crate::types::value::XmlAtomicValue::String(
                    result.typed_value.to_string_value(),
                ));
            }
        }
    }

    // Check list-level facets (length, minLength, maxLength, pattern, enumeration)
    let facets = collect_facets(sk, schema_set);
    if !facets.is_empty() {
        // Length constraints on list are item count
        facets
            .validate_list_length(items_str.len() as u64)
            .map_err(|e| {
                let code = errors::facet_constraint_code(&e);
                errors::from_facet_error(code, e, None)
            })?;

        // Pattern/enumeration on the normalized string representation
        facets
            .validate_string_patterns_enums(&normalized)
            .map_err(|e| {
                let code = errors::facet_constraint_code(&e);
                errors::from_facet_error(code, e, None)
            })?;
    }

    // Use the list type's own code (e.g. IdRefs, NmTokens, Entities) when it
    // has a built-in one, so that downstream consumers like collect_id_idref
    // can distinguish list types from their item types.
    let list_type_code = resolve_type_code(sk, schema_set)
        .filter(|code| code.is_list())
        .unwrap_or(item_type_code);

    let typed_value = XmlValue::with_schema_type(
        list_type_code,
        sk,
        XmlValueKind::List {
            item_type: item_type_code,
            items,
        },
    );

    // XSD 1.1: evaluate assertion facets ($value is the sequence of list items)
    #[cfg(feature = "xsd11")]
    {
        let item_sk = item_type_key.as_simple();
        evaluate_assertion_facets(&typed_value, &facets, schema_set, item_sk)?;
    }

    Ok(SimpleTypeResult {
        typed_value,
        member_type: None,
        normalized_value: Some(normalized),
    })
}

// ---------------------------------------------------------------------------
// Union
// ---------------------------------------------------------------------------

fn validate_union_type(
    value: &str,
    sk: SimpleTypeKey,
    schema_set: &SchemaSet,
) -> Result<SimpleTypeResult, ValidationError> {
    let st_data = match schema_set.arenas.simple_types.get(sk) {
        Some(d) => d,
        None => {
            return Err(errors::error(
                "cvc-simple-type",
                "Union type definition not found",
                None,
            ));
        }
    };

    // Try each member type in order
    for &member_key in &st_data.resolved_member_types {
        if let Ok(result) = validate_simple_type_inner(value, member_key, schema_set) {
            // Match found — check union-level facets (pattern, enumeration)
            let facets = collect_facets(sk, schema_set);
            if !facets.is_empty() {
                let check_value = result.typed_value.to_string_value();
                // §cvc-pattern-valid: patterns apply to the LEXICAL form (original value),
                // not the canonical/parsed form (e.g. "-.5" must match "\-\.\d+" not "-0.5")
                let lex_value = normalize_whitespace(value, WhitespaceMode::Collapse);
                facets.validate_patterns_only(&lex_value).map_err(|e| {
                    let code = errors::facet_constraint_code(&e);
                    errors::from_facet_error(code, e, None)
                })?;
                // Enumeration checked in value space: re-parse each enum value as the
                // matched primitive type and compare canonical forms (handles float/
                // double rounding and duration leading-zero differences)
                let type_code = result.typed_value.type_code;
                facets
                    .validate_enum_value_space(
                        |s| {
                            VALIDATOR_REGISTRY
                                .validate(type_code, s)
                                .map(|v| v.to_string_value() == check_value)
                                .unwrap_or(false)
                        },
                        &check_value,
                    )
                    .map_err(|e| {
                        let code = errors::facet_constraint_code(&e);
                        errors::from_facet_error(code, e, None)
                    })?;
            }

            // Propagate schema type on inner value from the matched member type
            let mut inner = result.typed_value;
            if inner.schema_type.is_none() {
                inner.schema_type = member_key.as_simple();
            }

            // XSD 1.1: evaluate assertion facets ($value is the member-validated value)
            #[cfg(feature = "xsd11")]
            {
                let item_sk = member_key.as_simple().and_then(|sk| {
                    crate::types::sequence::resolve_list_item_schema_type(sk, schema_set)
                });
                evaluate_assertion_facets(&inner, &facets, schema_set, item_sk)?;
            }

            return Ok(SimpleTypeResult {
                normalized_value: result.normalized_value,
                typed_value: XmlValue::with_schema_type(
                    inner.type_code,
                    sk,
                    XmlValueKind::Union(Box::new(inner)),
                ),
                member_type: Some(member_key),
            });
        }
    }

    // No member matched
    Err(errors::error(
        "cvc-simple-type",
        format!(
            "Value '{}' does not match any member type of the union",
            value
        ),
        None,
    ))
}

// ---------------------------------------------------------------------------
// XSD 1.1: Assertion facet evaluation
// ---------------------------------------------------------------------------

#[cfg(feature = "xsd11")]
fn resolve_assertion_default_ns(
    raw: Option<&str>,
    source: Option<&crate::parser::location::SourceRef>,
    ns_snapshot: &crate::namespace::context::NamespaceContextSnapshot,
    schema_set: &SchemaSet,
) -> Option<crate::ids::NameId> {
    // Look up the schema document that defines this assertion
    let doc = source.and_then(|s| schema_set.documents.get(s.doc_id as usize));

    // Cascade: facet-level > schema-level (of the defining document)
    let effective = if let Some(raw) = raw {
        Some(raw.to_string())
    } else {
        doc.and_then(|d| d.xpath_default_namespace)
            .map(|id| schema_set.name_table.resolve(id))
    };

    match effective.as_deref() {
        Some("##defaultNamespace") => ns_snapshot.default_ns,
        Some("##targetNamespace") => doc.and_then(|d| d.target_namespace),
        Some("##local") | None => None,
        Some(uri) => Some(schema_set.name_table.add(uri)),
    }
}

/// Evaluate assertion facets against a typed value (XSD 1.1).
///
/// Assertion facets on simpleType restrictions receive only the `$value`
/// variable (the typed value being validated). No context node or DOM
/// access is needed, so this evaluates inline during streaming validation.
#[cfg(feature = "xsd11")]
fn evaluate_assertion_facets(
    typed_value: &XmlValue,
    facets: &FacetSet,
    schema_set: &SchemaSet,
    item_schema_type: Option<SimpleTypeKey>,
) -> Result<(), ValidationError> {
    use crate::xpath::api::XPathExpr;
    use crate::xpath::functions::effective_boolean_value;
    use crate::xpath::{RoXmlNavigator, XPathContext};

    if facets.assertions.is_empty() {
        return Ok(());
    }

    for assertion in &facets.assertions {
        if assertion.test.is_empty() {
            continue;
        }

        // Resolve assertion source location for error reporting
        let location = assertion
            .source
            .as_ref()
            .and_then(|s| schema_set.source_maps.locate(s));

        // Build XPath static context
        let ctx = XPathContext::new(&schema_set.name_table)
            .with_namespaces(assertion.ns_snapshot.clone())
            .with_schema_set(schema_set);

        // Set default element namespace from xpathDefaultNamespace cascade
        let ctx = if let Some(default_ns) = resolve_assertion_default_ns(
            assertion.xpath_default_namespace.as_deref(),
            assertion.source.as_ref(),
            &assertion.ns_snapshot,
            schema_set,
        ) {
            ctx.with_default_element_ns(default_ns)
        } else {
            ctx
        };

        // Compile the XPath expression with $value declared
        let expr =
            XPathExpr::compile_with_vars(&assertion.test, &ctx, &["value"]).map_err(|e| {
                errors::error(
                    "cvc-assertion",
                    format!(
                        "Failed to compile assertion test '{}': {}",
                        assertion.test, e
                    ),
                    location.clone(),
                )
            })?;

        // Convert typed value to XPathValue
        let xpath_value = typed_value.to_xpath_value::<RoXmlNavigator<'static>>(item_schema_type);

        // Evaluate with $value bound
        let result = expr
            .evaluator(&ctx)
            .run_with::<RoXmlNavigator<'static>, _>(|eval| {
                eval.set_variable_by_name("value", xpath_value).unwrap();
            })
            .map_err(|e| {
                errors::error(
                    "cvc-assertion",
                    format!(
                        "Failed to evaluate assertion test '{}': {}",
                        assertion.test, e
                    ),
                    location.clone(),
                )
            })?;

        // Effective boolean value must be true
        let ebv = effective_boolean_value(&result).map_err(|e| {
            errors::error(
                "cvc-assertion",
                format!(
                    "Failed to compute boolean value for assertion '{}': {}",
                    assertion.test, e
                ),
                location.clone(),
            )
        })?;

        if !ebv {
            return Err(errors::error(
                "cvc-assertion",
                format!(
                    "Assertion '{}' failed for value '{}'",
                    assertion.test,
                    typed_value.to_string_value()
                ),
                location,
            ));
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline::load_and_process_schema;

    fn load_schema(xsd: &str) -> SchemaSet {
        let mut schema_set = SchemaSet::new();
        load_and_process_schema(xsd.as_bytes(), "test.xsd", &mut schema_set, None)
            .expect("failed to load schema");
        schema_set
    }

    /// Helper: get the TypeKey for a global element's type
    fn element_type(schema_set: &SchemaSet, local_name: &str) -> TypeKey {
        let name_id = schema_set.name_table.add(local_name);
        let elem_key = schema_set
            .lookup_element(None, name_id)
            .expect("element not found");
        schema_set.arenas.elements[elem_key]
            .resolved_type
            .expect("element has no resolved type")
    }

    #[test]
    fn test_builtin_string_accepts_anything() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="e" type="xs:string"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let result = validate_simple_type("hello world", tk, &schema).unwrap();
        assert!(result.member_type.is_none());
        assert_eq!(result.typed_value.type_code, XmlTypeCode::String);
    }

    #[test]
    fn test_builtin_integer_valid() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="e" type="xs:integer"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let result = validate_simple_type("42", tk, &schema).unwrap();
        assert_eq!(result.typed_value.type_code, XmlTypeCode::Integer);
    }

    #[test]
    fn test_builtin_integer_invalid() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="e" type="xs:integer"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let err = validate_simple_type("abc", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-datatype-valid");
    }

    #[test]
    fn test_builtin_boolean_valid() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="e" type="xs:boolean"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        for v in &["true", "false", "1", "0"] {
            assert!(
                validate_simple_type(v, tk, &schema).is_ok(),
                "failed for '{}'",
                v
            );
        }
    }

    #[test]
    fn test_builtin_boolean_invalid() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="e" type="xs:boolean"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("yes", tk, &schema).is_err());
    }

    #[test]
    fn test_user_defined_restriction_min_max() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="score">
                    <xs:restriction base="xs:integer">
                        <xs:minInclusive value="0"/>
                        <xs:maxInclusive value="100"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="score"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");

        assert!(validate_simple_type("50", tk, &schema).is_ok());
        assert!(validate_simple_type("0", tk, &schema).is_ok());
        assert!(validate_simple_type("100", tk, &schema).is_ok());

        let err = validate_simple_type("101", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-maxInclusive-valid");

        let err = validate_simple_type("-1", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-minInclusive-valid");
    }

    #[test]
    fn test_enumeration_facet() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="color">
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="red"/>
                        <xs:enumeration value="green"/>
                        <xs:enumeration value="blue"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="color"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");

        assert!(validate_simple_type("red", tk, &schema).is_ok());
        assert!(validate_simple_type("green", tk, &schema).is_ok());

        let err = validate_simple_type("yellow", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-enumeration-valid");
    }

    #[test]
    fn test_pattern_facet() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="zipCode">
                    <xs:restriction base="xs:string">
                        <xs:pattern value="[0-9]{5}"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="zipCode"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");

        assert!(validate_simple_type("12345", tk, &schema).is_ok());

        let err = validate_simple_type("1234", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-pattern-valid");
    }

    #[test]
    fn test_empty_string_against_integer() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="e" type="xs:integer"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("", tk, &schema).is_err());
    }

    #[test]
    fn test_empty_string_against_string() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="e" type="xs:string"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("", tk, &schema).is_ok());
    }

    #[test]
    fn test_list_type() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="intList">
                    <xs:list itemType="xs:integer"/>
                </xs:simpleType>
                <xs:element name="e" type="intList"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");

        let result = validate_simple_type("1 2 3", tk, &schema).unwrap();
        assert!(result.typed_value.is_list());

        // Non-integer item should fail
        assert!(validate_simple_type("1 abc 3", tk, &schema).is_err());
    }

    #[test]
    fn test_union_type() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="intOrString">
                    <xs:union memberTypes="xs:integer xs:string"/>
                </xs:simpleType>
                <xs:element name="e" type="intOrString"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");

        // Integer matches first
        let result = validate_simple_type("42", tk, &schema).unwrap();
        assert!(result.member_type.is_some());
        assert_eq!(result.typed_value.type_code, XmlTypeCode::Integer);

        // Non-integer matches xs:string
        let result = validate_simple_type("hello", tk, &schema).unwrap();
        assert!(result.member_type.is_some());
        assert_eq!(result.typed_value.type_code, XmlTypeCode::String);
    }
}

// ---------------------------------------------------------------------------
// XSD 1.1 tests: schema_type propagation and to_xpath_value conversion
// ---------------------------------------------------------------------------

#[cfg(all(test, feature = "xsd11"))]
mod xsd11_tests {
    use super::*;
    use crate::navigator::RoXmlNavigator;
    use crate::pipeline::load_and_process_schema;
    use crate::types::sequence::resolve_list_item_schema_type;
    use crate::xpath::XPathValue;

    fn load_schema(xsd: &str) -> SchemaSet {
        let mut schema_set = SchemaSet::xsd11();
        load_and_process_schema(xsd.as_bytes(), "test.xsd", &mut schema_set, None)
            .expect("failed to load schema");
        schema_set
    }

    fn element_type(schema_set: &SchemaSet, local_name: &str) -> TypeKey {
        let name_id = schema_set.name_table.add(local_name);
        let elem_key = schema_set
            .lookup_element(None, name_id)
            .expect("element not found");
        schema_set.arenas.elements[elem_key]
            .resolved_type
            .expect("element has no resolved type")
    }

    #[test]
    fn test_atomic_schema_type_set() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="score">
                    <xs:restriction base="xs:integer">
                        <xs:minInclusive value="0"/>
                        <xs:maxInclusive value="100"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="score"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let result = validate_simple_type("50", tk, &schema).unwrap();
        assert!(
            result.typed_value.schema_type.is_some(),
            "atomic value should have schema_type set"
        );
    }

    #[test]
    fn test_list_schema_type_set() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="intList">
                    <xs:list itemType="xs:integer"/>
                </xs:simpleType>
                <xs:element name="e" type="intList"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let result = validate_simple_type("1 2 3", tk, &schema).unwrap();
        assert!(
            result.typed_value.schema_type.is_some(),
            "list value should have schema_type set"
        );
    }

    #[test]
    fn test_union_schema_type_set() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="intOrString">
                    <xs:union memberTypes="xs:integer xs:string"/>
                </xs:simpleType>
                <xs:element name="e" type="intOrString"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let result = validate_simple_type("42", tk, &schema).unwrap();

        // Outer union value has schema_type
        assert!(
            result.typed_value.schema_type.is_some(),
            "union value should have schema_type set"
        );

        // Inner value also has schema_type (from matched member)
        if let XmlValueKind::Union(ref inner) = result.typed_value.value {
            assert!(
                inner.schema_type.is_some(),
                "inner union value should have schema_type set from matched member"
            );
        } else {
            panic!("expected Union variant");
        }
    }

    #[test]
    fn test_list_to_xpath_value() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="intList">
                    <xs:list itemType="xs:integer"/>
                </xs:simpleType>
                <xs:element name="e" type="intList"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let result = validate_simple_type("1 2 3", tk, &schema).unwrap();

        // Resolve list item schema type
        let list_sk = tk.as_simple().expect("should be simple type");
        let item_sk = resolve_list_item_schema_type(list_sk, &schema);

        let xpath_val: XPathValue<RoXmlNavigator<'static>> =
            result.typed_value.to_xpath_value(item_sk);

        // Should produce a sequence of 3 items
        let items = xpath_val.into_vec();
        assert_eq!(items.len(), 3, "list should produce 3 XPath items");

        // Each item should have schema_type set
        for item in &items {
            let val = item.as_atomic().expect("should be atomic");
            assert!(
                val.schema_type.is_some(),
                "each list item should have schema_type"
            );
        }
    }

    #[test]
    fn test_union_to_xpath_value() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="intOrString">
                    <xs:union memberTypes="xs:integer xs:string"/>
                </xs:simpleType>
                <xs:element name="e" type="intOrString"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let result = validate_simple_type("42", tk, &schema).unwrap();

        let xpath_val: XPathValue<RoXmlNavigator<'static>> =
            result.typed_value.to_xpath_value(None);

        // Union unwraps to the inner atomic value
        let items = xpath_val.into_vec();
        assert_eq!(items.len(), 1, "union should unwrap to single item");
        assert!(items[0].is_atomic());
    }

    #[test]
    fn test_atomic_to_xpath_value() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="e" type="xs:integer"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let result = validate_simple_type("42", tk, &schema).unwrap();

        let xpath_val: XPathValue<RoXmlNavigator<'static>> =
            result.typed_value.to_xpath_value(None);

        let items = xpath_val.into_vec();
        assert_eq!(items.len(), 1, "atomic should produce single item");
        assert!(items[0].is_atomic());
        assert_eq!(
            items[0].as_atomic().unwrap().type_code,
            XmlTypeCode::Integer
        );
    }

    // -----------------------------------------------------------------------
    // Assertion facet tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_assertion_even_integer() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="evenInt">
                    <xs:restriction base="xs:integer">
                        <xs:assertion test="$value mod 2 = 0"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="evenInt"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("4", tk, &schema).is_ok());
        assert!(validate_simple_type("0", tk, &schema).is_ok());
        assert!(validate_simple_type("-2", tk, &schema).is_ok());

        let err = validate_simple_type("3", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");

        let err = validate_simple_type("7", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
    }

    #[test]
    fn test_assertion_positive_value() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="posInt">
                    <xs:restriction base="xs:integer">
                        <xs:assertion test="$value gt 0"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="posInt"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("1", tk, &schema).is_ok());
        assert!(validate_simple_type("100", tk, &schema).is_ok());

        let err = validate_simple_type("0", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");

        let err = validate_simple_type("-5", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
    }

    #[test]
    fn test_assertion_string_length() {
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="shortStr">
                    <xs:restriction base="xs:string">
                        <xs:assertion test="string-length($value) le 5"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="shortStr"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("hello", tk, &schema).is_ok());
        assert!(validate_simple_type("", tk, &schema).is_ok());
        assert!(validate_simple_type("abcde", tk, &schema).is_ok());

        let err = validate_simple_type("toolong", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
    }

    #[test]
    fn test_assertion_inherited() {
        // Assertion inherited from base type through derivation chain
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="positiveInt">
                    <xs:restriction base="xs:integer">
                        <xs:assertion test="$value gt 0"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:simpleType name="smallPositiveInt">
                    <xs:restriction base="positiveInt">
                        <xs:maxInclusive value="10"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="smallPositiveInt"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("5", tk, &schema).is_ok());
        assert!(validate_simple_type("10", tk, &schema).is_ok());

        // Fails maxInclusive
        assert!(validate_simple_type("11", tk, &schema).is_err());
        // Fails inherited assertion ($value gt 0)
        let err = validate_simple_type("0", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
    }

    #[test]
    fn test_assertion_compile_error() {
        // Invalid XPath in assertion test → cvc-assertion error
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="badAssert">
                    <xs:restriction base="xs:integer">
                        <xs:assertion test="$value @@@ invalid"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="badAssert"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        let err = validate_simple_type("42", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
        assert!(err.message.contains("compile"));
    }

    #[test]
    fn test_assertion_with_other_facets() {
        // Assertion combined with pattern and range facets
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="evenScore">
                    <xs:restriction base="xs:integer">
                        <xs:minInclusive value="0"/>
                        <xs:maxInclusive value="100"/>
                        <xs:assertion test="$value mod 2 = 0"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="evenScore"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("50", tk, &schema).is_ok());
        assert!(validate_simple_type("0", tk, &schema).is_ok());
        assert!(validate_simple_type("100", tk, &schema).is_ok());

        // Fails assertion (odd number)
        let err = validate_simple_type("51", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");

        // Fails range (out of bounds)
        assert!(validate_simple_type("102", tk, &schema).is_err());
        assert!(validate_simple_type("-2", tk, &schema).is_err());
    }

    #[test]
    fn test_assertion_multiple() {
        // Multiple assertion facets on the same type
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="constrained">
                    <xs:restriction base="xs:integer">
                        <xs:assertion test="$value gt 0"/>
                        <xs:assertion test="$value mod 2 = 0"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="constrained"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        // Must satisfy both: positive AND even
        assert!(validate_simple_type("2", tk, &schema).is_ok());
        assert!(validate_simple_type("4", tk, &schema).is_ok());

        // Positive but odd -> fails second assertion
        let err = validate_simple_type("3", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");

        // Even but not positive -> fails first assertion
        let err = validate_simple_type("0", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
    }

    #[test]
    fn test_assertion_boolean_value() {
        // Assertion on xs:boolean type
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="mustBeTrue">
                    <xs:restriction base="xs:boolean">
                        <xs:assertion test="$value"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="mustBeTrue"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        assert!(validate_simple_type("true", tk, &schema).is_ok());
        assert!(validate_simple_type("1", tk, &schema).is_ok());

        let err = validate_simple_type("false", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
    }

    #[test]
    fn test_assertion_union_with_list_member_item_typing() {
        // Union whose member is a list of xs:integer.
        // The assertion uses `instance of` on each list item, which requires
        // item_schema_type to be propagated through the union validation path.
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="intList">
                    <xs:list itemType="xs:integer"/>
                </xs:simpleType>
                <xs:simpleType name="unionWithList">
                    <xs:union memberTypes="intList">
                        <xs:simpleType>
                            <xs:restriction base="xs:string"/>
                        </xs:simpleType>
                    </xs:union>
                </xs:simpleType>
                <xs:simpleType name="checkedUnion">
                    <xs:restriction base="unionWithList">
                        <xs:assertion test="every $i in $value satisfies $i instance of xs:integer"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="checkedUnion"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        // Space-separated integers should match the intList member and pass the assertion
        assert!(validate_simple_type("1 2 3", tk, &schema).is_ok());
    }

    // -----------------------------------------------------------------------
    // xpathDefaultNamespace cascade for assertion facets
    // -----------------------------------------------------------------------

    #[test]
    fn test_assertion_xpath_default_ns_schema_level_fallback() {
        // Schema-level xpathDefaultNamespace set to the XS namespace.
        // The assertion uses an unprefixed type name `integer` which should
        // resolve to xs:integer via the default element namespace cascade.
        let schema = load_schema(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        xpathDefaultNamespace="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="checkedInt">
                    <xs:restriction base="xs:integer">
                        <xs:assertion test="$value instance of integer"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="checkedInt"/>
            </xs:schema>"#,
        );
        let tk = element_type(&schema, "e");
        // Unprefixed `integer` resolves to xs:integer → assertion passes
        assert!(validate_simple_type("42", tk, &schema).is_ok());
    }

    #[test]
    fn test_assertion_xpath_default_ns_assertion_level_overrides_schema() {
        // Schema-level xpathDefaultNamespace is the XS namespace, but the
        // assertion element overrides it with ##local.  Now unprefixed
        // `integer` resolves to no-namespace, which has no matching type,
        // so the assertion must fail.
        let schema = load_schema(
            r###"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        xpathDefaultNamespace="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="checkedInt">
                    <xs:restriction base="xs:integer">
                        <xs:assertion test="$value instance of integer"
                                      xpathDefaultNamespace="##local"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="checkedInt"/>
            </xs:schema>"###,
        );
        let tk = element_type(&schema, "e");
        // ##local overrides → `integer` resolves to no-namespace → assertion fails
        let err = validate_simple_type("42", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
    }

    #[test]
    fn test_assertion_xpath_default_ns_target_namespace_token() {
        // xpathDefaultNamespace="##targetNamespace" with a non-XS target namespace.
        // Unprefixed `integer` resolves to http://example.com/ns (the target
        // namespace), not to the XS namespace, so `instance of integer` must
        // fail — proving the token is correctly resolved.
        let schema = load_schema(
            r###"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        targetNamespace="http://example.com/ns"
                        xmlns:tns="http://example.com/ns"
                        xpathDefaultNamespace="##targetNamespace">
                <xs:simpleType name="checkedInt">
                    <xs:restriction base="xs:integer">
                        <xs:assertion test="$value instance of integer"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:element name="e" type="tns:checkedInt"/>
            </xs:schema>"###,
        );
        let ns_id = schema.name_table.add("http://example.com/ns");
        let name_id = schema.name_table.add("e");
        let elem_key = schema
            .lookup_element(Some(ns_id), name_id)
            .expect("element not found");
        let tk = schema.arenas.elements[elem_key]
            .resolved_type
            .expect("element has no resolved type");
        // ##targetNamespace → http://example.com/ns → `integer` is NOT xs:integer → fails
        let err = validate_simple_type("42", tk, &schema).unwrap_err();
        assert_eq!(err.constraint, "cvc-assertion");
    }
}