xmlschema 0.0.8

XML Schema (XSD) validation for Rust, with zero unsafe code
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 xmlschema. All rights reserved.

//! Whether one content model is a valid restriction of another.
//!
//! XSD calls this *Particle Valid (Restriction)*, and it is the
//! hardest constraint in the specification: not a structural rule but
//! a **subsumption relation** between whole content models. A
//! restriction must accept no document its base would reject, and
//! deciding that means relating every particle of the derived model to
//! one of the base's.
//!
//! The relation is defined case by case, on the pair of terms:
//!
//! | derived | base | rule |
//! |---|---|---|
//! | element | element | names match, occurrences narrow, type derives |
//! | element | wildcard | the base admits the element's namespace |
//! | wildcard | wildcard | the base admits every namespace the derived does |
//! | group | wildcard | every particle restricts the wildcard |
//! | sequence | sequence | order-preserving map; unmapped base particles emptiable |
//! | sequence | all | as above, unordered |
//! | choice | choice | order-preserving map, unmapped base particles need not be emptiable |
//! | sequence | choice | every particle restricts some branch |
//!
//! **Where a case cannot be decided, it is accepted.** Type derivation
//! between named types is the main one: establishing it needs the
//! whole type hierarchy, and guessing risks rejecting a schema that is
//! valid. Accepting leaves a test counted as a miss rather than as a
//! wrong answer, which is the direction this crate errs in
//! everywhere.

use oxml::{Document, NodeId};

/// Which compositor a model group uses.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Compositor {
    /// `xs:sequence` — in order.
    Sequence,
    /// `xs:choice` — one of.
    Choice,
    /// `xs:all` — any order, each at most once.
    All,
}

/// What a particle stands for.
#[derive(Debug, Clone)]
pub enum Term {
    /// An element declaration.
    Element {
        /// Its name.
        name: String,
        /// The type it names, if it names one rather than nesting it.
        type_name: Option<String>,
    },
    /// An `xs:any` wildcard, with its namespace constraint as written.
    Wildcard(String),
    /// A nested model group.
    Group {
        /// How its particles combine.
        compositor: Compositor,
        /// The particles themselves.
        particles: Vec<Particle>,
    },
}

/// A term with an occurrence range.
#[derive(Debug, Clone)]
pub struct Particle {
    /// Minimum occurrences.
    pub min: usize,
    /// Maximum occurrences, or `None` for unbounded.
    pub max: Option<usize>,
    /// What occurs.
    pub term: Term,
}

impl Particle {
    /// A group of one, occurring exactly once, is the particle it
    /// contains.
    ///
    /// The specification calls this eliminating a *pointless
    /// particle*, and it has to happen before the cases are matched:
    /// `<sequence><any/></sequence>` and a bare `<any/>` are the same
    /// content model, and only one of them has a rule against a group.
    #[must_use]
    pub fn collapsed(&self) -> &Self {
        match &self.term {
            Term::Group { particles, .. }
                if particles.len() == 1
                    && self.min == 1
                    && self.max == Some(1) =>
            {
                particles[0].collapsed()
            }
            _ => self,
        }
    }

    /// How many times this particle can match in total, counting what
    /// is inside it.
    ///
    /// The specification calls this the *effective total range*. A
    /// group of three elements occurring once matches three times, and
    /// comparing that against a wildcard's own range is how a group
    /// restricting a wildcard is decided.
    #[must_use]
    pub fn effective_total_range(&self) -> (usize, Option<usize>) {
        let (min, max) = match &self.term {
            Term::Element { .. } | Term::Wildcard(_) => (1, Some(1)),
            Term::Group {
                compositor,
                particles,
            } => {
                let inner: Vec<(usize, Option<usize>)> = particles
                    .iter()
                    .map(Particle::effective_total_range)
                    .collect();
                let maxes = |combine: fn(usize, usize) -> usize| {
                    inner.iter().try_fold(0usize, |acc, (_, m)| {
                        m.map(|m| combine(acc, m))
                    })
                };
                match compositor {
                    // One branch is taken, so the least and greatest
                    // any branch offers.
                    Compositor::Choice => (
                        inner.iter().map(|(m, _)| *m).min().unwrap_or(0),
                        maxes(usize::max),
                    ),
                    // Every particle contributes.
                    Compositor::Sequence | Compositor::All => (
                        inner.iter().map(|(m, _)| *m).sum(),
                        maxes(usize::saturating_add),
                    ),
                }
            }
        };
        (
            min.saturating_mul(self.min),
            match (max, self.max) {
                (Some(a), Some(b)) => Some(a.saturating_mul(b)),
                _ => None,
            },
        )
    }

    /// Whether this particle can match nothing at all.
    ///
    /// Needed because a restriction may leave a base particle
    /// unmapped, but only one the base could have skipped anyway.
    #[must_use]
    pub fn emptiable(&self) -> bool {
        if self.min == 0 {
            return true;
        }
        match &self.term {
            Term::Element { .. } | Term::Wildcard(_) => false,
            // A sequence is emptiable when every particle is; a choice
            // when any is; `all` behaves as a sequence here.
            Term::Group {
                compositor,
                particles,
            } => match compositor {
                Compositor::Choice => {
                    particles.is_empty()
                        || particles.iter().any(Particle::emptiable)
                }
                Compositor::Sequence | Compositor::All => {
                    particles.iter().all(Particle::emptiable)
                }
            },
        }
    }
}

/// Whether `derived` accepts nothing `base` would reject.
///
/// `resolve` answers whether one type name is validly derived from
/// another; it is consulted only for the element-to-element case.
#[must_use]
pub fn is_valid_restriction(
    derived: &Particle,
    base: &Particle,
    type_derives: &dyn Fn(&str, &str) -> bool,
) -> bool {
    // Pointless particles are eliminated first, so a group of one is
    // matched as the thing it wraps.
    let derived = derived.collapsed();
    let base = base.collapsed();

    match (&derived.term, &base.term) {
        // Elt:Elt:NameAndTypeOK
        (
            Term::Element {
                name: r,
                type_name: rt,
            },
            Term::Element {
                name: b,
                type_name: bt,
            },
        ) => {
            r == b
                && occurrence_ok(derived, base)
                && match (rt.as_deref(), bt.as_deref()) {
                    // Two named types: the relation decides.
                    (Some(r), Some(b)) => r == b || type_derives(r, b),
                    // A base with no named type is the ur-type, which
                    // everything derives from; and a derived type
                    // given inline cannot have its derivation
                    // established without the whole hierarchy. Both
                    // are accepted rather than guessed at.
                    _ => true,
                }
        }

        // Elt:Any:NSCompat -- an element restricting a wildcard. The
        // element has no namespace of its own in this crate's model,
        // so only the occurrence range is decided here.
        (Term::Element { .. }, Term::Wildcard(_)) => {
            occurrence_ok(derived, base)
        }

        // Any:Any:NSSubset
        (Term::Wildcard(r), Term::Wildcard(b)) => {
            occurrence_ok(derived, base) && namespace_subset(r, b)
        }

        // NSRecurseCheckCardinality -- a group restricting a
        // wildcard. Each particle is checked against the wildcard's
        // *term* rather than its occurrence range, and the range is
        // compared once against the group's effective total range.
        // Comparing each particle against the range instead rejects
        // three elements restricting `<any minOccurs="3"/>`, which is
        // exactly what a valid restriction of it looks like.
        (Term::Group { particles, .. }, Term::Wildcard(namespace)) => {
            let unbounded = Particle {
                min: 0,
                max: None,
                term: Term::Wildcard(namespace.clone()),
            };
            if !particles
                .iter()
                .all(|p| is_valid_restriction(p, &unbounded, type_derives))
            {
                return false;
            }
            let (min, max) = derived.effective_total_range();
            occurrence_ok(
                &Particle {
                    min,
                    max,
                    term: Term::Wildcard(String::new()),
                },
                base,
            )
        }

        // A wildcard cannot restrict an element or a group: it admits
        // more than either.
        (Term::Wildcard(_), Term::Element { .. } | Term::Group { .. }) => false,

        // A group restricting an element. The specification has no
        // case for this, but it eliminates *pointless particles*
        // first: a group of one, occurring exactly once, is the
        // particle it contains. Anything richer genuinely has no rule.
        (
            Term::Group {
                particles,
                compositor: _,
            },
            Term::Element { .. },
        ) => match particles.as_slice() {
            [only] if derived.min == 1 && derived.max == Some(1) => {
                is_valid_restriction(only, base, type_derives)
            }
            _ => false,
        },

        // An element restricting a group. Wrapping the element in a
        // group and recursing would be neat and does not terminate:
        // collapsing immediately unwraps it again. So the two cases
        // are written out.
        (
            Term::Element { .. },
            Term::Group {
                compositor,
                particles,
            },
        ) => {
            match compositor {
                // One branch is taken, so restricting any branch is
                // enough.
                Compositor::Choice => particles
                    .iter()
                    .any(|b| is_valid_restriction(derived, b, type_derives)),
                // Every other particle has to be skippable, or the
                // base required something the restriction dropped.
                Compositor::Sequence | Compositor::All => {
                    particles.iter().enumerate().any(|(i, b)| {
                        is_valid_restriction(derived, b, type_derives)
                            && particles
                                .iter()
                                .enumerate()
                                .all(|(j, other)| j == i || other.emptiable())
                    })
                }
            }
        }

        (
            Term::Group {
                compositor: rc,
                particles: rp,
            },
            Term::Group {
                compositor: bc,
                particles: bp,
            },
        ) => {
            occurrence_ok(derived, base)
                && groups_match(*rc, rp, *bc, bp, type_derives)
        }
    }
}

/// One model group restricting another, by compositor pair.
///
/// Split out of [`is_valid_restriction`] because the eight cases read
/// as a table and do not belong inside a match on term kinds.
fn groups_match(
    rc: Compositor,
    rp: &[Particle],
    bc: Compositor,
    bp: &[Particle],
    type_derives: &dyn Fn(&str, &str) -> bool,
) -> bool {
    match (rc, bc) {
        // RecurseUnordered / RecurseAsIfGroup: `all` imposes no
        // order, so neither does a restriction of it. This case has
        // to come first -- a combined `(Sequence | All, Sequence |
        // All)` arm swallows it, and then a sequence restricting an
        // `all` in a different order is rejected for an ordering the
        // base never required.
        (Compositor::Sequence | Compositor::All, Compositor::All) => {
            map_unordered(rp, bp, type_derives)
        }
        // Recurse: order is preserved, and anything the restriction
        // steps over must have been skippable.
        (Compositor::Sequence | Compositor::All, Compositor::Sequence) => {
            map_in_order(rp, bp, true, type_derives)
        }
        // RecurseLax: the base was free to skip any branch, so an
        // unmapped one need not be emptiable.
        (Compositor::Choice, Compositor::Choice) => {
            map_in_order(rp, bp, false, type_derives)
        }
        // MapAndSum: every particle restricts some branch.
        (Compositor::Sequence | Compositor::All, Compositor::Choice) => {
            rp.iter().all(|r| {
                bp.iter().any(|b| is_valid_restriction(r, b, type_derives))
            })
        }
        // A choice cannot restrict a sequence or an `all`: it drops
        // the requirement that every particle appear.
        (Compositor::Choice, _) => false,
    }
}

/// Occurrence Range OK: the derived range lies inside the base's.
fn occurrence_ok(derived: &Particle, base: &Particle) -> bool {
    if derived.min < base.min {
        return false;
    }
    match (derived.max, base.max) {
        // An unbounded base contains everything.
        (_, None) => true,
        // An unbounded derived does not fit a bounded base.
        (None, Some(_)) => false,
        (Some(r), Some(b)) => r <= b,
    }
}

/// Map every derived particle onto a distinct base particle, keeping
/// order.
///
/// `skipped_must_be_emptiable` distinguishes a sequence, where a base
/// particle the derivation drops must have been optional, from a
/// choice, where the base was free to skip any branch anyway.
fn map_in_order(
    derived: &[Particle],
    base: &[Particle],
    skipped_must_be_emptiable: bool,
    type_derives: &dyn Fn(&str, &str) -> bool,
) -> bool {
    let mut at = 0usize;
    for r in derived {
        let mut found = None;
        for (offset, b) in base[at..].iter().enumerate() {
            if is_valid_restriction(r, b, type_derives) {
                found = Some(at + offset);
                break;
            }
            // Anything stepped over must have been skippable.
            if skipped_must_be_emptiable && !b.emptiable() {
                return false;
            }
        }
        let Some(index) = found else {
            return false;
        };
        at = index + 1;
    }
    // Whatever is left must be skippable too.
    !skipped_must_be_emptiable || base[at..].iter().all(Particle::emptiable)
}

/// Map every derived particle onto a distinct base particle, in any
/// order, each used at most once.
fn map_unordered(
    derived: &[Particle],
    base: &[Particle],
    type_derives: &dyn Fn(&str, &str) -> bool,
) -> bool {
    let mut used = vec![false; base.len()];
    for r in derived {
        let Some(index) = base.iter().enumerate().position(|(i, b)| {
            !used[i] && is_valid_restriction(r, b, type_derives)
        }) else {
            return false;
        };
        used[index] = true;
    }
    base.iter()
        .zip(&used)
        .all(|(b, taken)| *taken || b.emptiable())
}

/// Whether `derived` admits no namespace `base` excludes.
#[must_use]
pub fn namespace_subset(derived: &str, base: &str) -> bool {
    if base == "##any" {
        return true;
    }
    if derived == "##any" {
        return false;
    }
    if base == derived {
        return true;
    }
    // `##other` is defined against a target namespace this function
    // does not have, so a list against it is left undecided -- and
    // undecided means accepted.
    if base == "##other" || derived == "##other" {
        return true;
    }
    let allowed: Vec<&str> = base.split_whitespace().collect();
    derived.split_whitespace().all(|n| allowed.contains(&n))
}

/// Read a model group out of the schema document.
///
/// Returns `None` for a host with no content model, and follows a
/// `group` reference through `groups`.
#[must_use]
pub fn particle_of(
    doc: &Document,
    host: NodeId,
    groups: &dyn Fn(&str) -> Option<NodeId>,
    depth: usize,
) -> Option<Particle> {
    if depth > 32 {
        return None;
    }
    let local = |id: NodeId| doc.element_name(id).map(|n| n.local.clone());
    let name = local(host)?;

    let compositor = match name.as_str() {
        "sequence" => Compositor::Sequence,
        "choice" => Compositor::Choice,
        "all" => Compositor::All,
        "group" => {
            // A reference stands for what it names, with this
            // occurrence's own range.
            let target = doc
                .attribute(host, "ref")
                .and_then(|r| groups(r.rsplit(':').next().unwrap_or(r)))?;
            let inner =
                ["sequence", "choice", "all"].into_iter().find_map(|g| {
                    doc.children(target)
                        .iter()
                        .copied()
                        .find(|&c| local(c).is_some_and(|n| n == g))
                })?;
            let mut particle = particle_of(doc, inner, groups, depth + 1)?;
            particle.min = min_occurs(doc, host);
            particle.max = max_occurs(doc, host);
            return Some(particle);
        }
        "element" => {
            return Some(Particle {
                min: min_occurs(doc, host),
                max: max_occurs(doc, host),
                term: Term::Element {
                    name: doc
                        .attribute(host, "name")
                        .or_else(|| doc.attribute(host, "ref"))
                        .unwrap_or_default()
                        .rsplit(':')
                        .next()
                        .unwrap_or_default()
                        .to_owned(),
                    type_name: doc
                        .attribute(host, "type")
                        .map(|t| t.rsplit(':').next().unwrap_or(t).to_owned()),
                },
            });
        }
        "any" => {
            return Some(Particle {
                min: min_occurs(doc, host),
                max: max_occurs(doc, host),
                term: Term::Wildcard(
                    doc.attribute(host, "namespace")
                        .unwrap_or("##any")
                        .to_owned(),
                ),
            });
        }
        _ => return None,
    };

    let particles = doc
        .children(host)
        .iter()
        .copied()
        .filter_map(|c| particle_of(doc, c, groups, depth + 1))
        .collect();
    Some(Particle {
        min: min_occurs(doc, host),
        max: max_occurs(doc, host),
        term: Term::Group {
            compositor,
            particles,
        },
    })
}

fn min_occurs(doc: &Document, id: NodeId) -> usize {
    doc.attribute(id, "minOccurs")
        .and_then(|v| v.parse().ok())
        .unwrap_or(1)
}

fn max_occurs(doc: &Document, id: NodeId) -> Option<usize> {
    match doc.attribute(id, "maxOccurs") {
        Some("unbounded") => None,
        Some(v) => v.parse().ok().or(Some(1)),
        None => Some(1),
    }
}