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
//! Substitution group helpers for validation and UPA checks.

use std::collections::{HashMap, HashSet};

use crate::ids::{ElementKey, NameId, TypeKey};
use crate::schema::model::{DerivationSet, SchemaSet};

/// Map from substitution group head to all substitutable element names.
pub type SubstitutionGroupMap = HashMap<ElementKey, HashSet<(NameId, Option<NameId>)>>;

/// Build a substitution group membership map for the schema set.
pub fn build_substitution_group_map(schema_set: &SchemaSet) -> SubstitutionGroupMap {
    build_substitution_group_map_inner(schema_set, false)
}

/// Build a substitution group membership map that includes abstract members.
///
/// Per XSD 1.1 (W3C Bugzilla 4337), abstract elements participate in the
/// substitution group for schema-time UPA / cos-element-consistent (EDC)
/// constraints, even though they cannot appear in instances. This variant is
/// used by UPA/EDC schema-time validation under XSD 1.1.
pub fn build_substitution_group_map_with_abstract(schema_set: &SchemaSet) -> SubstitutionGroupMap {
    build_substitution_group_map_inner(schema_set, true)
}

fn build_substitution_group_map_inner(
    schema_set: &SchemaSet,
    include_abstract: bool,
) -> SubstitutionGroupMap {
    let mut member_map: HashMap<ElementKey, Vec<ElementKey>> = HashMap::new();
    for (member_key, elem) in schema_set.arenas.elements.iter() {
        for head_key in &elem.resolved_substitution_groups {
            member_map.entry(*head_key).or_default().push(member_key);
        }
    }

    let mut result = HashMap::new();
    let mut seen_heads = HashSet::new();
    for (head_key, _) in schema_set.arenas.elements.iter() {
        let head_key = resolve_element_key(schema_set, head_key);
        if !seen_heads.insert(head_key) {
            continue;
        }

        let head_elem = match schema_set.arenas.elements.get(head_key) {
            Some(elem) => elem,
            None => continue,
        };
        let mut names = HashSet::new();
        if let Some(name) = head_elem.name {
            if !head_elem.is_abstract || include_abstract {
                names.insert((name, head_elem.target_namespace));
            }
        }

        let (effective_block, effective_final) =
            effective_element_constraints(schema_set, head_elem);
        if !effective_block.contains_substitution() {
            let head_type = head_elem.resolved_type;
            let exclude = derivation_exclusions(effective_block, effective_final);

            let mut stack = member_map.get(&head_key).cloned().unwrap_or_default();
            let mut visited = HashSet::new();
            while let Some(member_key) = stack.pop() {
                if !visited.insert(member_key) {
                    continue;
                }
                if let Some(member) = resolved_element(schema_set, member_key) {
                    if let Some(name) = member.name {
                        if (!member.is_abstract || include_abstract)
                            && is_substitutable(
                                schema_set,
                                head_type,
                                exclude,
                                member.resolved_type,
                            )
                        {
                            names.insert((name, member.target_namespace));
                        }
                    }
                }
                if let Some(nested) = member_map.get(&member_key) {
                    for &next in nested {
                        if !visited.contains(&next) {
                            stack.push(next);
                        }
                    }
                }
            }
        }

        result.insert(head_key, names);
    }

    result
}

fn resolve_element_key(schema_set: &SchemaSet, key: ElementKey) -> ElementKey {
    schema_set
        .arenas
        .elements
        .get(key)
        .and_then(|elem| elem.resolved_ref)
        .unwrap_or(key)
}

fn resolved_element(
    schema_set: &SchemaSet,
    key: ElementKey,
) -> Option<&crate::arenas::ElementDeclData> {
    let key = resolve_element_key(schema_set, key);
    schema_set.arenas.elements.get(key)
}

pub(crate) fn derivation_exclusions(
    effective_block: DerivationSet,
    effective_final: DerivationSet,
) -> DerivationSet {
    // Per §3.3.6.3 / §3.9.6 the exclusion set is built solely from the
    // head *element's* {substitution group exclusions} (element `final`)
    // and, for instance-time checks, its `block` attribute.  The head
    // *type's* {final} is intentionally excluded here: is_type_derived_from
    // walks the full derivation chain and the type's own finality is a
    // property of the type hierarchy, not the element declaration.
    (effective_block | effective_final) & derivation_mask()
}

fn derivation_mask() -> DerivationSet {
    DerivationSet::EXTENSION
        | DerivationSet::RESTRICTION
        | DerivationSet::LIST
        | DerivationSet::UNION
}

pub(crate) fn is_substitutable(
    schema_set: &SchemaSet,
    head_type: Option<TypeKey>,
    exclude: DerivationSet,
    member_type: Option<TypeKey>,
) -> bool {
    let any_type = TypeKey::Complex(schema_set.any_type_key());
    let head_type = head_type.unwrap_or(any_type);
    let member_type = member_type.unwrap_or(any_type);
    schema_set.is_type_derived_from(member_type, head_type, exclude)
}

pub(crate) fn effective_element_constraints(
    _schema_set: &SchemaSet,
    element: &crate::arenas::ElementDeclData,
) -> (DerivationSet, DerivationSet) {
    // Both block and final_derivation are resolved at assembly time
    // (the assembler applies blockDefault/finalDefault to empty/absent entries).
    (element.block, element.final_derivation)
}

/// Check if `candidate_key` is validly substitutable for `head_key`
/// per XSD §3.3.6.3 / §3.9.6 NameAndTypeOK.
pub(crate) fn is_element_substitutable_for(
    schema_set: &SchemaSet,
    head_key: ElementKey,
    candidate_key: ElementKey,
) -> bool {
    let Some(head_elem) = schema_set.arenas.elements.get(head_key) else {
        return false;
    };
    let Some(candidate_elem) = schema_set.arenas.elements.get(candidate_key) else {
        return false;
    };

    // Check declared substitution group membership (direct or transitive).
    // Walk candidate's declared heads to find head_key.
    let mut visited = HashSet::new();
    let mut stack: Vec<ElementKey> = candidate_elem.resolved_substitution_groups.clone();
    let mut is_member = false;
    while let Some(sg_head) = stack.pop() {
        if !visited.insert(sg_head) {
            continue;
        }
        if sg_head == head_key {
            is_member = true;
            break;
        }
        if let Some(sg_elem) = schema_set.arenas.elements.get(sg_head) {
            stack.extend_from_slice(&sg_elem.resolved_substitution_groups);
        }
    }
    if !is_member {
        return false;
    }

    // Check block constraints on the head element
    let (effective_block, effective_final) = effective_element_constraints(schema_set, head_elem);
    if effective_block.contains_substitution() {
        return false;
    }

    // Check type derivation with exclusion mask
    let exclude = derivation_exclusions(effective_block, effective_final);
    is_substitutable(
        schema_set,
        head_elem.resolved_type,
        exclude,
        candidate_elem.resolved_type,
    )
}

/// Check e-props-correct.4: member type must be validly substitutable for
/// head type subject to the head's `{substitution group exclusions}` (= `final`).
///
/// Unlike `is_element_substitutable_for`, this does NOT check `block` because
/// `block` controls instance-time substitution, not affiliation legality.
fn check_substitution_group_affiliation(
    schema_set: &SchemaSet,
    head_key: ElementKey,
    member_key: ElementKey,
) -> bool {
    let Some(head_elem) = schema_set.arenas.elements.get(head_key) else {
        return false;
    };
    let Some(member_elem) = schema_set.arenas.elements.get(member_key) else {
        return false;
    };
    let (_, effective_final) = effective_element_constraints(schema_set, head_elem);
    let exclude = derivation_exclusions(DerivationSet::empty(), effective_final);
    is_substitutable(
        schema_set,
        head_elem.resolved_type,
        exclude,
        member_elem.resolved_type,
    )
}

/// Validate all declared substitution group memberships.
///
/// Reports `e-props-correct.4` if a member element's type is not validly
/// substitutable for its head element's type (respecting `final` constraints).
///
/// Note: This uses only the head's `{substitution group exclusions}` (= `final`),
/// NOT the head's `block` attribute. The `block` attribute controls instance-time
/// substitution, not schema-level affiliation legality.
pub fn validate_all_substitution_groups(schema_set: &SchemaSet) -> crate::SchemaResult<()> {
    for (member_key, elem) in schema_set.arenas.elements.iter() {
        for &head_key in &elem.resolved_substitution_groups {
            if !check_substitution_group_affiliation(schema_set, head_key, member_key) {
                let member_name = elem
                    .name
                    .map(|n| schema_set.name_table.resolve(n).to_string())
                    .unwrap_or_else(|| "<anonymous>".to_string());
                let head_name = schema_set
                    .arenas
                    .elements
                    .get(head_key)
                    .and_then(|h| h.name)
                    .map(|n| schema_set.name_table.resolve(n).to_string())
                    .unwrap_or_else(|| "<anonymous>".to_string());
                let location = elem
                    .source
                    .as_ref()
                    .and_then(|s| schema_set.source_maps.locate(s));
                return Err(crate::error::SchemaError::structural(
                    "e-props-correct.4",
                    format!(
                        "Element '{}' is not a valid member of the substitution group \
                         headed by '{}': type derivation is blocked by 'final' constraint",
                        member_name, head_name
                    ),
                    location,
                ));
            }
        }
    }

    // §3.3.6.1.5: substitution group affiliation must be acyclic. Walk each
    // element's resolved substitution-group chain and reject any back-edge.
    for (start_key, _) in schema_set.arenas.elements.iter() {
        let mut visited = HashSet::new();
        let mut stack = vec![start_key];
        while let Some(current) = stack.pop() {
            if !visited.insert(current) {
                continue;
            }
            let Some(decl) = schema_set.arenas.elements.get(current) else {
                continue;
            };
            for &head in &decl.resolved_substitution_groups {
                if head == start_key {
                    let elem = &schema_set.arenas.elements[start_key];
                    let elem_name = elem
                        .name
                        .map(|n| schema_set.name_table.resolve(n).to_string())
                        .unwrap_or_else(|| "<anonymous>".to_string());
                    let location = elem
                        .source
                        .as_ref()
                        .and_then(|s| schema_set.source_maps.locate(s));
                    return Err(crate::error::SchemaError::structural(
                        "e-props-correct",
                        format!(
                            "Substitution group cycle detected involving element '{}' \
                             (§3.3.6.1.5)",
                            elem_name
                        ),
                        location,
                    ));
                }
                stack.push(head);
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::location::SourceRef;

    fn element_data(
        name: NameId,
        type_key: TypeKey,
        source: Option<SourceRef>,
    ) -> crate::arenas::ElementDeclData {
        crate::arenas::ElementDeclData {
            name: Some(name),
            target_namespace: None,
            ref_name: None,
            type_ref: None,
            inline_type: None,
            substitution_group: Vec::new(),
            default_value: None,
            fixed_value: None,
            nillable: false,
            is_abstract: false,
            min_occurs: 1,
            max_occurs: Some(1),
            block: DerivationSet::empty(),
            final_derivation: DerivationSet::empty(),
            form: None,
            id: None,
            alternatives: Vec::new(),
            identity_constraints: Vec::new(),
            pending_ic_refs: vec![],
            annotation: None,
            source,
            resolved_type: Some(type_key),
            resolved_ref: None,
            resolved_substitution_groups: Vec::new(),
            deferred_type_error: None,
        }
    }

    #[test]
    fn test_substitution_group_type_derivation_allows_member() {
        let mut schema_set = SchemaSet::new();
        let head_name = schema_set.name_table.add("head");
        let member_name = schema_set.name_table.add("member");
        let head_type = TypeKey::Simple(schema_set.builtin_types().decimal);
        let member_type = TypeKey::Simple(schema_set.builtin_types().int);

        let head_key = schema_set
            .arenas
            .alloc_element(element_data(head_name, head_type, None));
        let member_key =
            schema_set
                .arenas
                .alloc_element(element_data(member_name, member_type, None));
        schema_set
            .arenas
            .elements
            .get_mut(member_key)
            .unwrap()
            .resolved_substitution_groups
            .push(head_key);

        let map = build_substitution_group_map(&schema_set);
        let names = map.get(&head_key).unwrap();
        assert!(names.contains(&(member_name, None)));
    }

    #[test]
    fn test_substitution_group_element_final_blocks_member() {
        let mut schema_set = SchemaSet::new();
        let head_name = schema_set.name_table.add("head");
        let member_name = schema_set.name_table.add("member");
        let head_type = TypeKey::Simple(schema_set.builtin_types().decimal);
        let member_type = TypeKey::Simple(schema_set.builtin_types().int);

        let mut head = element_data(head_name, head_type, None);
        head.final_derivation = DerivationSet::RESTRICTION;
        let head_key = schema_set.arenas.alloc_element(head);
        let member_key =
            schema_set
                .arenas
                .alloc_element(element_data(member_name, member_type, None));
        schema_set
            .arenas
            .elements
            .get_mut(member_key)
            .unwrap()
            .resolved_substitution_groups
            .push(head_key);

        let map = build_substitution_group_map(&schema_set);
        let names = map.get(&head_key).unwrap();
        assert!(!names.contains(&(member_name, None)));
    }

    // Per §3.3.4 the {substitution group exclusions} are derived solely from the
    // *element* declaration's `final` attribute.  The head *type's* {final} does
    // not gate substitution group membership.  This test verifies that setting
    // final_derivation on the head type alone does NOT block the member.
    #[test]
    fn test_substitution_group_type_final_does_not_block_member() {
        let mut schema_set = SchemaSet::new();
        let head_name = schema_set.name_table.add("head");
        let member_name = schema_set.name_table.add("member");
        let head_type = schema_set.builtin_types().decimal;
        let member_type = TypeKey::Simple(schema_set.builtin_types().int);

        // Mark the head *type* as final for restriction — this must not block the member.
        if let Some(type_def) = schema_set.arenas.simple_types.get_mut(head_type) {
            type_def.final_derivation = DerivationSet::RESTRICTION;
        }

        let head_key = schema_set.arenas.alloc_element(element_data(
            head_name,
            TypeKey::Simple(head_type),
            None,
        ));
        let member_key =
            schema_set
                .arenas
                .alloc_element(element_data(member_name, member_type, None));
        schema_set
            .arenas
            .elements
            .get_mut(member_key)
            .unwrap()
            .resolved_substitution_groups
            .push(head_key);

        let map = build_substitution_group_map(&schema_set);
        let names = map.get(&head_key).unwrap();
        // The type's {final} must NOT gate membership; only the element's final does.
        assert!(names.contains(&(member_name, None)));
    }

    #[test]
    fn test_substitution_group_block_substitution_keeps_head_only() {
        let mut schema_set = SchemaSet::new();
        let head_name = schema_set.name_table.add("head");
        let member_name = schema_set.name_table.add("member");
        let head_type = TypeKey::Simple(schema_set.builtin_types().decimal);
        let member_type = TypeKey::Simple(schema_set.builtin_types().int);

        let mut head = element_data(head_name, head_type, None);
        head.block = DerivationSet::SUBSTITUTION;
        let head_key = schema_set.arenas.alloc_element(head);
        let member_key =
            schema_set
                .arenas
                .alloc_element(element_data(member_name, member_type, None));
        schema_set
            .arenas
            .elements
            .get_mut(member_key)
            .unwrap()
            .resolved_substitution_groups
            .push(head_key);

        let map = build_substitution_group_map(&schema_set);
        let names = map.get(&head_key).unwrap();
        assert!(names.contains(&(head_name, None)));
        assert!(!names.contains(&(member_name, None)));
    }

    #[test]
    fn test_substitution_group_block_default_blocks_member() {
        // Assembly would apply blockDefault to elements without an explicit block.
        // This test simulates that: head.block = SUBSTITUTION (inherited from blockDefault).
        let mut schema_set = SchemaSet::new();
        let head_name = schema_set.name_table.add("head");
        let member_name = schema_set.name_table.add("member");
        let head_type = TypeKey::Simple(schema_set.builtin_types().decimal);
        let member_type = TypeKey::Simple(schema_set.builtin_types().int);

        let mut head = element_data(head_name, head_type, None);
        head.block = DerivationSet::SUBSTITUTION;
        let head_key = schema_set.arenas.alloc_element(head);
        let member_key =
            schema_set
                .arenas
                .alloc_element(element_data(member_name, member_type, None));
        schema_set
            .arenas
            .elements
            .get_mut(member_key)
            .unwrap()
            .resolved_substitution_groups
            .push(head_key);

        let map = build_substitution_group_map(&schema_set);
        let names = map.get(&head_key).unwrap();
        assert!(names.contains(&(head_name, None)));
        assert!(!names.contains(&(member_name, None)));
    }

    #[test]
    fn test_substitution_group_final_default_blocks_member() {
        // Assembly would apply finalDefault to elements without an explicit final.
        // This test simulates that: head.final_derivation = RESTRICTION (inherited from finalDefault).
        let mut schema_set = SchemaSet::new();
        let head_name = schema_set.name_table.add("head");
        let member_name = schema_set.name_table.add("member");
        let head_type = TypeKey::Simple(schema_set.builtin_types().decimal);
        let member_type = TypeKey::Simple(schema_set.builtin_types().int);

        let mut head = element_data(head_name, head_type, None);
        head.final_derivation = DerivationSet::RESTRICTION;
        let head_key = schema_set.arenas.alloc_element(head);
        let member_key =
            schema_set
                .arenas
                .alloc_element(element_data(member_name, member_type, None));
        schema_set
            .arenas
            .elements
            .get_mut(member_key)
            .unwrap()
            .resolved_substitution_groups
            .push(head_key);

        let map = build_substitution_group_map(&schema_set);
        let names = map.get(&head_key).unwrap();
        assert!(names.contains(&(head_name, None)));
        assert!(!names.contains(&(member_name, None)));
    }
}