xmlschema 0.0.5

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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 xmlschema. All rights reserved.

//! Reading `.xsd` documents, including the ways they can be wrong.
//!
//! A schema that fails to parse must say why. Silently producing an
//! empty schema is the worst outcome available: every document then
//! validates, and the caller believes it was checked.

use xmlschema::{Content, parse_schema, validate};

fn schema(body: &str) -> String {
    format!(
        r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">{body}</xs:schema>"#
    )
}

#[test]
fn a_minimal_schema_parses() {
    let s =
        parse_schema(&schema(r#"<xs:element name="note" type="xs:string"/>"#))
            .expect("valid schema");
    assert!(s.element("note").is_some());
    assert!(s.element("missing").is_none());
}

#[test]
fn a_target_namespace_is_recorded() {
    let s = parse_schema(
        r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
             targetNamespace="urn:example">
             <xs:element name="a" type="xs:string"/>
           </xs:schema>"#,
    )
    .expect("valid schema");
    assert_eq!(s.target_namespace.as_deref(), Some("urn:example"));
}

#[test]
fn a_schema_without_a_target_namespace_has_none() {
    let s = parse_schema(&schema(r#"<xs:element name="a" type="xs:string"/>"#))
        .expect("valid schema");
    assert_eq!(s.target_namespace, None);
}

#[test]
fn malformed_xml_is_reported_as_such() {
    let e = parse_schema("<xs:schema><unclosed></xs:schema>")
        .expect_err("not well-formed");
    assert!(e.to_string().contains("well-formed"), "{e}");
}

#[test]
fn an_empty_document_has_no_root_element() {
    let e = parse_schema("").expect_err("no root");
    assert!(!e.to_string().is_empty());
}

#[test]
fn a_non_schema_root_is_rejected() {
    // Pointing at the wrong file is a common mistake, and validating
    // everything against an empty schema would hide it.
    let e = parse_schema("<html><body/></html>").expect_err("not a schema");
    assert!(e.to_string().contains("xs:schema"), "{e}");
}

#[test]
fn a_schema_need_not_declare_a_top_level_element() {
    // This used to assert the opposite. A schema whose purpose is to
    // be imported declares types, groups or attributes and no
    // elements at all, and refusing those rejected 282 schemas the
    // W3C suite calls valid.
    let only_a_type = parse_schema(&schema(
        r#"<xs:simpleType name="code">
             <xs:restriction base="xs:string">
               <xs:maxLength value="4"/>
             </xs:restriction>
           </xs:simpleType>"#,
    ))
    .expect("a schema of types alone is valid");
    assert!(only_a_type.elements.is_empty());
    assert!(only_a_type.named_simple_types.contains_key("code"));

    // An entirely empty schema is still a schema.
    assert!(parse_schema(&schema("")).is_ok());
}

/// A reference this schema cannot resolve names something in an
/// imported namespace, which is unenforceable here -- not invalid.
#[test]
fn an_unresolvable_reference_is_not_a_schema_error() {
    let s = parse_schema(&schema(
        r#"<xs:element name="r">
             <xs:complexType><xs:sequence>
               <xs:element ref="other:thing"/>
             </xs:sequence></xs:complexType>
           </xs:element>"#,
    ))
    .expect("an unresolved ref does not invalidate the schema");

    // It keeps its name and cardinality so ordering still works, and
    // accepts any content because there is nothing to check against.
    let doc = oxml::parse("<r><thing>anything at all</thing></r>")
        .expect("well-formed");
    assert!(validate(&doc, &s).is_valid());
}

#[test]
fn an_element_without_a_name_is_rejected() {
    let e = parse_schema(&schema(r#"<xs:element type="xs:string"/>"#))
        .expect_err("no name");
    assert!(e.to_string().contains("name"), "{e}");
}

#[test]
fn cardinality_attributes_are_read() {
    let s = parse_schema(&schema(
        r#"<xs:element name="r">
             <xs:complexType><xs:sequence>
               <xs:element name="a" type="xs:string" minOccurs="0"/>
               <xs:element name="b" type="xs:string" maxOccurs="unbounded"/>
               <xs:element name="c" type="xs:string" minOccurs="2" maxOccurs="5"/>
             </xs:sequence></xs:complexType>
           </xs:element>"#,
    ))
    .expect("valid schema");

    let Some(Content::Sequence(parts)) = s.element("r").map(|p| &*p.content)
    else {
        panic!("expected a sequence");
    };
    assert_eq!(parts[0].occurs.min, 0);
    assert_eq!(parts[0].occurs.max, Some(1));
    assert_eq!(parts[1].occurs.max, None, "unbounded");
    assert_eq!(parts[2].occurs.min, 2);
    assert_eq!(parts[2].occurs.max, Some(5));
}

#[test]
fn an_unparseable_cardinality_falls_back_rather_than_failing() {
    // A malformed maxOccurs should not take the whole schema down; the
    // XSD default is the safest reading.
    let s = parse_schema(&schema(
        r#"<xs:element name="r">
             <xs:complexType><xs:sequence>
               <xs:element name="a" type="xs:string" maxOccurs="lots"/>
               <xs:element name="b" type="xs:string" minOccurs="many"/>
             </xs:sequence></xs:complexType>
           </xs:element>"#,
    ))
    .expect("valid schema");
    let Some(Content::Sequence(parts)) = s.element("r").map(|p| &*p.content)
    else {
        panic!("expected a sequence");
    };
    assert_eq!(parts[0].occurs.max, Some(1));
    assert_eq!(parts[1].occurs.min, 1);
}

#[test]
fn xs_all_permits_any_order_and_forbids_repetition() {
    // This used to assert that `xs:all` was *rejected*, on the
    // reasoning that accepting it and validating nothing would be
    // worse than refusing. That was right while it was unimplemented.
    // It is implemented now, and the property worth pinning is that it
    // is not a sequence with the ordering relaxed.
    let s = parse_schema(&schema(
        r#"<xs:element name="r">
             <xs:complexType><xs:all>
               <xs:element name="a" type="xs:string"/>
               <xs:element name="b" type="xs:string"/>
             </xs:all></xs:complexType>
           </xs:element>"#,
    ))
    .expect("xs:all parses");

    let valid = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &s).is_valid()
    };
    assert!(valid("<r><a>1</a><b>2</b></r>"), "declared order");
    assert!(valid("<r><b>2</b><a>1</a></r>"), "any order is the point");
    assert!(!valid("<r><a>1</a><a>2</a><b>3</b></r>"), "at most once");
    assert!(!valid("<r><a>1</a></r>"), "b is required");
    assert!(
        !valid("<r><a>1</a><b>2</b><c>3</c></r>"),
        "c is not declared"
    );
}

#[test]
fn an_element_with_no_type_at_all_is_unconstrained() {
    let s = parse_schema(&schema(r#"<xs:element name="anything"/>"#))
        .expect("valid schema");
    assert!(matches!(
        s.element("anything").map(|p| &*p.content),
        Some(Content::Any)
    ));
}

#[test]
fn a_choice_is_parsed_as_a_choice() {
    let s = parse_schema(&schema(
        r#"<xs:element name="r">
             <xs:complexType><xs:choice>
               <xs:element name="a" type="xs:string"/>
               <xs:element name="b" type="xs:string"/>
             </xs:choice></xs:complexType>
           </xs:element>"#,
    ))
    .expect("valid schema");
    assert!(matches!(
        s.element("r").map(|p| &*p.content),
        Some(Content::Choice(_))
    ));
}

#[test]
fn simple_content_extension_resolves_to_its_base() {
    let s = parse_schema(&schema(
        r#"<xs:element name="priced">
             <xs:complexType><xs:simpleContent>
               <xs:extension base="xs:decimal">
                 <xs:attribute name="currency" type="xs:string"/>
               </xs:extension>
             </xs:simpleContent></xs:complexType>
           </xs:element>"#,
    ))
    .expect("valid schema");
    assert!(matches!(
        s.element("priced").map(|p| &*p.content),
        Some(Content::Simple(_))
    ));
}

#[test]
fn a_named_simple_type_is_resolved_by_reference() {
    let s = parse_schema(&schema(
        r#"<xs:simpleType name="Code">
             <xs:restriction base="xs:string">
               <xs:enumeration value="A"/>
               <xs:enumeration value="B"/>
             </xs:restriction>
           </xs:simpleType>
           <xs:element name="code" type="Code"/>"#,
    ))
    .expect("valid schema");
    let Some(Content::Simple(st)) = s.element("code").map(|p| &*p.content)
    else {
        panic!("expected a simple type");
    };
    assert_eq!(st.facets.enumeration, ["A", "B"]);
}

#[test]
fn attributes_and_their_use_are_read() {
    let s = parse_schema(&schema(
        r#"<xs:element name="r">
             <xs:complexType>
               <xs:sequence/>
               <xs:attribute name="req" type="xs:string" use="required"/>
               <xs:attribute name="opt" type="xs:string"/>
             </xs:complexType>
           </xs:element>"#,
    ))
    .expect("valid schema");
    let attrs = &s.element("r").expect("element").attributes;
    assert_eq!(attrs.len(), 2);
    let req = attrs.iter().find(|a| a.name == "req").expect("req");
    let opt = attrs.iter().find(|a| a.name == "opt").expect("opt");
    assert!(req.required);
    assert!(!opt.required);
}

#[test]
fn the_error_type_displays_its_message() {
    let e = parse_schema("<html/>").expect_err("not a schema");
    assert_eq!(e.to_string(), format!("{e}"));
    assert!(!format!("{e}").is_empty());
}

/// A schema is itself an XML document with a content model, and one
/// that breaks it is invalid however sensible its declarations look.
#[test]
fn a_schema_breaking_xsds_own_structure_is_rejected() {
    // Two annotations, where at most one is permitted.
    assert!(
        parse_schema(&schema(
            r#"<xs:complexType name="t">
                 <xs:annotation><xs:documentation>a</xs:documentation></xs:annotation>
                 <xs:annotation><xs:documentation>b</xs:documentation></xs:annotation>
               </xs:complexType>"#,
        ))
        .is_err(),
        "at most one xs:annotation"
    );

    // An annotation that is not first.
    assert!(
        parse_schema(&schema(
            r#"<xs:complexType name="t">
                 <xs:simpleContent><xs:extension base="xs:string"/></xs:simpleContent>
                 <xs:annotation><xs:documentation>a</xs:documentation></xs:annotation>
               </xs:complexType>"#,
        ))
        .is_err(),
        "xs:annotation must come first"
    );

    // Mutually exclusive children.
    assert!(
        parse_schema(&schema(
            r#"<xs:complexType name="t">
                 <xs:sequence/>
                 <xs:choice/>
               </xs:complexType>"#,
        ))
        .is_err(),
        "a complexType has one content model, not two"
    );

    // Both a named type and an inline one.
    assert!(
        parse_schema(&schema(
            r#"<xs:element name="e" type="xs:string">
                 <xs:simpleType><xs:restriction base="xs:string"/></xs:simpleType>
               </xs:element>"#,
        ))
        .is_err(),
        "a type attribute excludes an inline type"
    );

    // Both `name` and `ref`.
    assert!(
        parse_schema(&schema(r#"<xs:element name="e" ref="other"/>"#,))
            .is_err(),
        "name and ref are exclusive"
    );

    // And a well-formed schema still parses.
    assert!(
        parse_schema(&schema(
            r#"<xs:complexType name="t">
                 <xs:annotation><xs:documentation>a</xs:documentation></xs:annotation>
                 <xs:sequence/>
               </xs:complexType>"#,
        ))
        .is_ok(),
        "one annotation, first, with one content model"
    );
}

/// A wildcard's namespace constraint takes four forms.
#[test]
fn a_wildcard_namespace_constraint_is_read_in_every_form() {
    let with = |ns: &str| {
        format!(
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                          targetNamespace="urn:t" xmlns:t="urn:t">
                 <xs:element name="r">
                   <xs:complexType><xs:sequence>
                     <xs:any namespace='{ns}'  processContents="skip"/>
                   </xs:sequence></xs:complexType>
                 </xs:element>
               </xs:schema>"#
        )
    };
    // Each form parses; `##other` and a list are the ones that were
    // never exercised.
    for ns in [
        "##any",
        "##other",
        "urn:a urn:b",
        "##targetNamespace ##local",
    ] {
        assert!(parse_schema(&with(ns)).is_ok(), "namespace={ns}");
    }
    // A wildcard with no `namespace` attribute means `##any`.
    let bare = r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="r">
          <xs:complexType><xs:sequence><xs:any/></xs:sequence></xs:complexType>
        </xs:element></xs:schema>"#;
    assert!(parse_schema(bare).is_ok());
}

/// `##other` admits anything outside the target namespace, which
/// includes an unqualified element.
#[test]
fn other_excludes_only_the_target_namespace() {
    let xsd = r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                             targetNamespace="urn:t"
                             xmlns:t="urn:t"
                             elementFormDefault="qualified">
        <xs:element name="r">
          <xs:complexType><xs:sequence>
            <xs:any namespace='##other' processContents="skip"/>
          </xs:sequence></xs:complexType>
        </xs:element></xs:schema>"#;
    let s = parse_schema(xsd).expect("schema parses");
    let ok =
        oxml::parse(r#"<r xmlns="urn:t"><foo xmlns="urn:elsewhere"/></r>"#)
            .expect("well-formed");
    assert!(
        validate(&ok, &s).is_valid(),
        "a foreign namespace is admitted"
    );

    let no = oxml::parse(r#"<r xmlns="urn:t"><foo xmlns="urn:t"/></r>"#)
        .expect("well-formed");
    assert!(!validate(&no, &s).is_valid(), "the target namespace is not");
}

/// A document that is not a schema is rejected, and says why.
#[test]
fn a_document_that_is_not_a_schema_is_rejected() {
    // Well-formed XML, but not an xs:schema.
    let e = parse_schema("<notSchema/>").expect_err("not a schema");
    assert!(e.to_string().contains("xs:schema"), "{e}");

    // Not well-formed at all.
    let e = parse_schema("<a><unclosed></a>").expect_err("not well-formed");
    assert!(e.to_string().contains("well-formed"), "{e}");

    // A document with no root element is not well-formed XML, so the
    // parser refuses it before a schema is ever considered — which is
    // why `validate`'s own no-root branch cannot be reached through
    // the public API.
    assert!(oxml::parse("<!-- nothing -->").is_err());
}