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
//! Element and attribute declarations
//!
//! This module defines XSD element and attribute declarations.

use crate::ids::{ElementKey, IdentityConstraintKey, NameId, SimpleTypeKey, TypeKey};
use crate::parser::location::SourceRef;
use crate::schema::model::DerivationSet;

/// Scope of a declaration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeclarationScope {
    /// Global declaration (top-level in schema)
    Global,
    /// Local declaration (within complex type)
    Local,
}

/// Value constraint kind
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValueConstraint {
    /// Default value (can be overridden)
    Default(String),
    /// Fixed value (cannot be changed)
    Fixed(String),
}

impl ValueConstraint {
    /// Get the value
    pub fn value(&self) -> &str {
        match self {
            ValueConstraint::Default(v) => v,
            ValueConstraint::Fixed(v) => v,
        }
    }

    /// Check if this is a fixed constraint
    pub fn is_fixed(&self) -> bool {
        matches!(self, ValueConstraint::Fixed(_))
    }
}

/// Type reference (can be resolved or unresolved)
#[derive(Debug, Clone)]
pub enum TypeReference {
    /// Resolved type key
    Resolved(TypeKey),
    /// Unresolved reference (namespace + local name)
    Unresolved {
        namespace: Option<NameId>,
        local_name: NameId,
    },
}

/// Element declaration
///
/// Represents an xs:element declaration, either global or local.
#[derive(Debug, Clone)]
pub struct ElementDecl {
    /// Element name
    pub name: NameId,

    /// Target namespace
    pub target_namespace: Option<NameId>,

    /// Source location for error reporting
    pub source: Option<SourceRef>,

    /// Declaration scope (global or local)
    pub scope: DeclarationScope,

    /// Type definition (resolved or reference)
    pub type_def: Option<TypeReference>,

    /// Value constraint (default or fixed)
    pub value_constraint: Option<ValueConstraint>,

    /// Nillable flag (allows xsi:nil="true")
    pub nillable: bool,

    /// Abstract flag (must be substituted)
    pub is_abstract: bool,

    /// Substitution group affiliation
    pub substitution_group: Option<ElementRef>,

    /// Disallowed substitutions (block attribute)
    pub disallowed_substitutions: DerivationSet,

    /// Substitution group exclusions (final attribute)
    pub substitution_group_exclusions: DerivationSet,

    /// Identity constraints defined on this element
    pub identity_constraints: Vec<IdentityConstraintKey>,

    /// ID attribute value
    pub id: Option<String>,

    /// XSD 1.1: Type alternatives (conditional type assignment)
    pub type_alternatives: Vec<TypeAlternative>,

    /// Form (qualified/unqualified) - for local elements
    pub form: Option<FormKind>,
}

/// Reference to an element (for substitution groups)
#[derive(Debug, Clone)]
pub enum ElementRef {
    /// Resolved element key
    Resolved(ElementKey),
    /// Unresolved reference
    Unresolved {
        namespace: Option<NameId>,
        local_name: NameId,
    },
}

/// XSD 1.1: Type alternative
#[derive(Debug, Clone)]
pub struct TypeAlternative {
    /// XPath test expression
    pub test: Option<String>,
    /// Type to use when test passes
    pub type_def: TypeReference,
    /// Source location
    pub source: Option<SourceRef>,
}

/// Form kind (qualified/unqualified)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormKind {
    Qualified,
    Unqualified,
}

impl ElementDecl {
    /// Create a new global element declaration
    pub fn new_global(name: NameId, target_namespace: Option<NameId>) -> Self {
        Self {
            name,
            target_namespace,
            source: None,
            scope: DeclarationScope::Global,
            type_def: None,
            value_constraint: None,
            nillable: false,
            is_abstract: false,
            substitution_group: None,
            disallowed_substitutions: DerivationSet::empty(),
            substitution_group_exclusions: DerivationSet::empty(),
            identity_constraints: Vec::new(),
            id: None,
            type_alternatives: Vec::new(),
            form: None,
        }
    }

    /// Create a new local element declaration
    pub fn new_local(name: NameId, target_namespace: Option<NameId>) -> Self {
        Self {
            name,
            target_namespace,
            source: None,
            scope: DeclarationScope::Local,
            type_def: None,
            value_constraint: None,
            nillable: false,
            is_abstract: false,
            substitution_group: None,
            disallowed_substitutions: DerivationSet::empty(),
            substitution_group_exclusions: DerivationSet::empty(),
            identity_constraints: Vec::new(),
            id: None,
            type_alternatives: Vec::new(),
            form: None,
        }
    }

    /// Check if this is a global element
    pub fn is_global(&self) -> bool {
        self.scope == DeclarationScope::Global
    }

    /// Check if this is a local element
    pub fn is_local(&self) -> bool {
        self.scope == DeclarationScope::Local
    }

    /// Check if this element can be substituted
    pub fn is_substitutable(&self) -> bool {
        !self.is_abstract && self.substitution_group_exclusions.is_empty()
    }
}

/// Attribute declaration
///
/// Represents an xs:attribute declaration, either global or local.
#[derive(Debug, Clone)]
pub struct AttributeDecl {
    /// Attribute name
    pub name: NameId,

    /// Target namespace
    pub target_namespace: Option<NameId>,

    /// Source location for error reporting
    pub source: Option<SourceRef>,

    /// Declaration scope (global or local)
    pub scope: DeclarationScope,

    /// Simple type definition (attributes always have simple types)
    pub type_def: Option<SimpleTypeReference>,

    /// Value constraint (default or fixed)
    pub value_constraint: Option<ValueConstraint>,

    /// ID attribute value
    pub id: Option<String>,

    /// Form (qualified/unqualified) - for local attributes
    pub form: Option<FormKind>,

    /// XSD 1.1: Inheritable attribute (§3.2.6, §3.3.5.6)
    pub inheritable: bool,
}

/// Reference to a simple type
#[derive(Debug, Clone)]
pub enum SimpleTypeReference {
    /// Resolved simple type key
    Resolved(SimpleTypeKey),
    /// Built-in type
    BuiltIn(crate::types::simple::BuiltInType),
    /// Unresolved reference
    Unresolved {
        namespace: Option<NameId>,
        local_name: NameId,
    },
}

impl AttributeDecl {
    /// Create a new global attribute declaration
    pub fn new_global(name: NameId, target_namespace: Option<NameId>) -> Self {
        Self {
            name,
            target_namespace,
            source: None,
            scope: DeclarationScope::Global,
            type_def: None,
            value_constraint: None,
            id: None,
            form: None,
            inheritable: false,
        }
    }

    /// Create a new local attribute declaration
    pub fn new_local(name: NameId, target_namespace: Option<NameId>) -> Self {
        Self {
            name,
            target_namespace,
            source: None,
            scope: DeclarationScope::Local,
            type_def: None,
            value_constraint: None,
            id: None,
            form: None,
            inheritable: false,
        }
    }

    /// Check if this is a global attribute
    pub fn is_global(&self) -> bool {
        self.scope == DeclarationScope::Global
    }

    /// Check if this is a local attribute
    pub fn is_local(&self) -> bool {
        self.scope == DeclarationScope::Local
    }

    /// Check if this attribute has a default value
    pub fn has_default(&self) -> bool {
        matches!(self.value_constraint, Some(ValueConstraint::Default(_)))
    }

    /// Check if this attribute has a fixed value
    pub fn has_fixed(&self) -> bool {
        matches!(self.value_constraint, Some(ValueConstraint::Fixed(_)))
    }
}

/// Notation declaration
///
/// Represents an xs:notation declaration.
#[derive(Debug, Clone)]
pub struct NotationDecl {
    /// Notation name
    pub name: NameId,

    /// Target namespace
    pub target_namespace: Option<NameId>,

    /// Public identifier
    pub public: String,

    /// System identifier (optional)
    pub system: Option<String>,

    /// Source location
    pub source: Option<SourceRef>,

    /// ID attribute value
    pub id: Option<String>,
}

impl NotationDecl {
    /// Create a new notation declaration
    pub fn new(name: NameId, public: String) -> Self {
        Self {
            name,
            target_namespace: None,
            public,
            system: None,
            source: None,
            id: None,
        }
    }
}

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

    #[test]
    fn test_element_decl_global() {
        let elem = ElementDecl::new_global(NameId(1), Some(NameId(2)));
        assert!(elem.is_global());
        assert!(!elem.is_local());
        assert_eq!(elem.scope, DeclarationScope::Global);
    }

    #[test]
    fn test_element_decl_local() {
        let elem = ElementDecl::new_local(NameId(1), None);
        assert!(elem.is_local());
        assert!(!elem.is_global());
        assert_eq!(elem.scope, DeclarationScope::Local);
    }

    #[test]
    fn test_element_substitutable() {
        let mut elem = ElementDecl::new_global(NameId(1), None);
        assert!(elem.is_substitutable());

        elem.is_abstract = true;
        assert!(!elem.is_substitutable());
    }

    #[test]
    fn test_attribute_decl_global() {
        let attr = AttributeDecl::new_global(NameId(1), Some(NameId(2)));
        assert!(attr.is_global());
        assert!(!attr.is_local());
    }

    #[test]
    fn test_attribute_decl_local() {
        let attr = AttributeDecl::new_local(NameId(1), None);
        assert!(attr.is_local());
        assert!(!attr.is_global());
    }

    #[test]
    fn test_value_constraint() {
        let default_val = ValueConstraint::Default("foo".to_string());
        assert!(!default_val.is_fixed());
        assert_eq!(default_val.value(), "foo");

        let fixed_val = ValueConstraint::Fixed("bar".to_string());
        assert!(fixed_val.is_fixed());
        assert_eq!(fixed_val.value(), "bar");
    }

    #[test]
    fn test_attribute_value_constraint() {
        let mut attr = AttributeDecl::new_local(NameId(1), None);
        assert!(!attr.has_default());
        assert!(!attr.has_fixed());

        attr.value_constraint = Some(ValueConstraint::Default("test".to_string()));
        assert!(attr.has_default());
        assert!(!attr.has_fixed());

        attr.value_constraint = Some(ValueConstraint::Fixed("test".to_string()));
        assert!(!attr.has_default());
        assert!(attr.has_fixed());
    }

    #[test]
    fn test_notation_decl() {
        let notation = NotationDecl::new(NameId(1), "http://example.com/public".to_string());
        assert_eq!(notation.name, NameId(1));
        assert_eq!(notation.public, "http://example.com/public");
        assert!(notation.system.is_none());
    }
}