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
//! XML Schema 1.0 validation of UBL 2.1 Invoice and `CreditNote` documents against the
//! official OASIS UBL 2.1 schemas, in pure Rust.
//!
//! This is not a general XML Schema processor: it implements exactly the constructs
//! that the schemas reachable from `UBL-Invoice-2.1.xsd` and `UBL-CreditNote-2.1.xsd`
//! use (see the inventory below) and refuses any other construct when a schema set is
//! loaded ([`LoadErrorKind::Unsupported`]), so that a schema it cannot handle is never
//! half-checked.
//!
//! ```
//! let schema = einvoice_xsd::Schema::ubl_2_1()?;
//! let report = schema.validate(
//! r#"<CreditNote xmlns="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
//! xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
//! <cbc:ID>CN-1</cbc:ID><cbc:IssueDate>2024-02-30</cbc:IssueDate>
//! </CreditNote>"#,
//! )?;
//! // Sorted by the position of the element concerned: the incomplete CreditNote
//! // first, then its invalid date.
//! let kinds: Vec<&str> = report.errors.iter().map(|e| e.kind.as_str()).collect();
//! assert_eq!(kinds, ["missing-element", "invalid-value"]);
//! assert_eq!((report.errors[1].line, report.errors[1].element.as_str()), (3, "{urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2}IssueDate"));
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! # Schemas
//!
//! Thirteen of the fifteen schema documents reachable from the two entry points are
//! vendored byte for byte in `schemas/oasis-ubl-2.1/` and embedded
//! ([`UBL_2_1_FILES`]); [`Schema::ubl_2_1`] loads both entry points into one schema
//! set, as an XML Schema processor given both does. [`Schema::load`] loads any set of
//! schema documents within the supported subset, from memory: `xs:import` and
//! `xs:include` are resolved as paths relative to the importing document, never
//! fetched.
//!
//! The other two are the ETSI `XAdES` schemas (`UBL-XAdESv132-2.1.xsd` and
//! `UBL-XAdESv141-2.1.xsd`), which the OASIS package includes but no licence allows
//! to redistribute, so they are not shipped. Their two namespaces
//! ([`XADES_NAMESPACES`]) are the only ones whose import may be left out: an
//! `xs:import` of one of them whose document is not in the set is not loaded, and is
//! listed by [`Schema::omitted_imports`]; any other missing document is a
//! [`LoadErrorKind::MissingFile`] error. The UBL schemas only import these
//! namespaces and never refer to their components, so `XAdES` elements can only occur
//! where a lax wildcard allows them (in `ext:ExtensionContent` and in `ds:Object`,
//! where a `XAdES` signature puts its `xades:QualifyingProperties`): without the
//! schemas they have no declaration and are validated laxly, as `xs:anyType`. Their
//! content models, values and attributes are then not checked; their descendants that
//! have a global declaration (`ds:DigestMethod`, `cbc:` elements, ...) still are. A
//! user who has the two files can add them to the embedded ones and load the
//! complete set with [`Schema::load`]: they are then loaded and validated strictly.
//!
//! # Validation
//!
//! [`Schema::validate`] parses the document (namespace-aware, internal DTD subset
//! allowed for entities, no external resources) and returns a [`Report`] listing
//! every validity error with a stable [`ErrorKind`], the line and column of the
//! element concerned and a message. The document element must match a global element
//! declaration of the set. Validation follows XML Schema 1.0 Second Edition:
//!
//! * content models are compiled to deterministic automata (Unique Particle
//! Attribution is checked at load); `minOccurs`/`maxOccurs` on elements,
//! sequences, choices and wildcards;
//! * wildcards: `##any` and `##other`, `processContents` `strict`, `lax` and `skip`.
//! Lax content without a declaration is validated as `xs:anyType`, whose own
//! content is lax: an element anywhere below that has a global declaration (for
//! instance a `cbc:` element inside `ext:ExtensionContent`) is validated against it;
//! * attributes: declared uses (`required`/`optional`), attribute wildcards, the
//! four `xsi:` attributes (`xsi:type` with derivation checks and abstract types,
//! `xsi:nil` refused since no declaration is nillable, `xsi:schemaLocation` and
//! `xsi:noNamespaceSchemaLocation` ignored as hints);
//! * simple values: whitespace processing and the lexical space of every built-in
//! type the schemas use, the `enumeration` facet, and uniqueness of `xs:ID` values.
//!
//! The decimal and integer types have no digit limit, dates follow the XML Schema
//! 1.0 rules (no year zero, at least four year digits, timezones up to ±14:00,
//! `24:00:00` as end of day), `anyURI` is checked as XML Schema 1.0 defines it
//! (escaping per `XLink` 1.0 section 5.4, then RFC 2396 as amended by RFC 2732).
//!
//! # Comparison with xmllint
//!
//! The test suite compares every verdict with xmllint (libxml2 2.9.14), which loads
//! the complete OASIS schema set (`XAdES` schemas included), on the CEN EN 16931 UBL
//! test corpus and on synthetic mutations covering each construct (`tests/oracle/`).
//! Every corpus case agrees. The other differences are listed with their cause in
//! `tests/oracle/disagreements.tsv`: the cases where xmllint departs from XML Schema
//! 1.0 and this crate does not, and the cases with `XAdES` content, which this crate
//! validates laxly with the shipped schemas. The `xmllint-oracle` example also runs
//! this crate with the complete set, where the second kind does not occur.
//!
//! # Licences
//!
//! The code is MIT; the embedded schemas keep the terms stated in their own headers
//! (OASIS, UN/CEFACT, W3C). [`LICENCES`] holds the full text of each, as shipped in
//! the crate's `LICENSES/` directory, for programs that must show them to their
//! users.
//!
//! # Inventory of the UBL 2.1 schemas
//!
//! Counted over the thirteen vendored documents by the `inventory` test, which fails
//! when this table no longer matches them. Annotations are not descended into.
//!
//! In short: 13 schema documents, all with `elementFormDefault="qualified"`; 22
//! `xs:import` (two of them, of the `XAdES` namespaces, not loaded) and one
//! `xs:include` (the extension content type, included by the common extension
//! components); 1,581 global element declarations and 1,168 named complex types;
//! content models made of `xs:sequence` and `xs:choice` of element references, local
//! elements and wildcards, with `minOccurs` 0 or 1 and `maxOccurs` 1 or `unbounded`
//! only. There are no anonymous types, no groups or attribute groups, no substitution
//! groups, no identity constraints, no default or fixed values, no `xs:all`, no lists
//! or unions, and no facets.
//!
//! * The UBL extension point: `ext:UBLExtensions` holds one or more `ext:UBLExtension`,
//! each ending with `ext:ExtensionContent`, whose type is a sequence of exactly one
//! `xs:any namespace="##other" processContents="lax"` (`##other` relative to the
//! extension components namespace, so neither that namespace nor no namespace).
//! The signature extension (`sig:UBLDocumentSignatures`) is declared and imported,
//! so it is validated strictly; any other extension content is validated laxly.
//! * The CCTS core component types (`CCTS_CCT_SchemaModule-2.1.xsd`) are complex types
//! with simple content extending `xs:decimal`, `xs:base64Binary`,
//! `xs:normalizedString` and `xs:string` with optional supplementary-component
//! attributes; the unqualified data types extend them (or `xs:date`, `xs:dateTime`,
//! `xs:time`, `xs:boolean`), and seven of them are simple-content restrictions
//! making one attribute required (`currencyID`, `mimeCode`, `unitCode`). Every
//! `cbc:` type extends one of these.
//! * The W3C XML Signature schema brings the rest: mixed content, nested and
//! repeated choices and sequences, strict and lax wildcards, `xs:ID` attributes,
//! local elements typed directly with built-in types, and the three simple types
//! (restrictions of `xs:base64Binary` twice and of `xs:integer`).
//!
//! The supported subset also covers what the two `XAdES` schemas add, so that the
//! complete OASIS set loads: an attribute wildcard (`xs:anyAttribute`, strict), an
//! abstract type with two complex-content restrictions, and a simple type
//! restricting `xs:string` with the `enumeration` facet.
//!
//! INVENTORY-START
//! | Construct | Count |
//! |---|---:|
//! | `built-in type xs:ID` | 9 |
//! | `built-in type xs:anyURI` | 15 |
//! | `built-in type xs:base64Binary` | 11 |
//! | `built-in type xs:boolean` | 1 |
//! | `built-in type xs:date` | 1 |
//! | `built-in type xs:dateTime` | 1 |
//! | `built-in type xs:decimal` | 4 |
//! | `built-in type xs:integer` | 2 |
//! | `built-in type xs:language` | 2 |
//! | `built-in type xs:normalizedString` | 26 |
//! | `built-in type xs:string` | 20 |
//! | `built-in type xs:time` | 1 |
//! | `xs:annotation` | 2566 |
//! | `xs:any` | 13 |
//! | `xs:any @maxOccurs="1"` | 1 |
//! | `xs:any @maxOccurs="unbounded"` | 5 |
//! | `xs:any @minOccurs="0"` | 6 |
//! | `xs:any @minOccurs="1"` | 1 |
//! | `xs:any @namespace="##any"` | 2 |
//! | `xs:any @namespace="##other"` | 11 |
//! | `xs:any @processContents="lax"` | 11 |
//! | `xs:attribute` | 62 |
//! | `xs:attribute @use="optional"` | 49 |
//! | `xs:attribute @use="required"` | 12 |
//! | `xs:choice` | 6 |
//! | `xs:choice @maxOccurs="unbounded"` | 3 |
//! | `xs:choice @minOccurs="0"` | 1 |
//! | `xs:complexType` | 1168 |
//! | `xs:complexType @mixed="true"` | 8 |
//! | `xs:element (global)` | 1581 |
//! | `xs:element (local, name)` | 22 |
//! | `xs:element (local, name) @minOccurs="0"` | 4 |
//! | `xs:element (local, ref)` | 2278 |
//! | `xs:element (local, ref) @maxOccurs="1"` | 1657 |
//! | `xs:element (local, ref) @maxOccurs="unbounded"` | 603 |
//! | `xs:element (local, ref) @minOccurs="0"` | 2026 |
//! | `xs:element (local, ref) @minOccurs="1"` | 233 |
//! | `xs:extension (in xs:simpleContent)` | 905 |
//! | `xs:import` | 22 |
//! | `xs:import @namespace="http://uri.etsi.org/01903/v1.3.2#"` | 1 |
//! | `xs:import @namespace="http://uri.etsi.org/01903/v1.4.1#"` | 1 |
//! | `xs:import @namespace="http://www.w3.org/2000/09/xmldsig#"` | 1 |
//! | `xs:import @namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"` | 2 |
//! | `xs:import @namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"` | 5 |
//! | `xs:import @namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"` | 2 |
//! | `xs:import @namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"` | 1 |
//! | `xs:import @namespace="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2"` | 2 |
//! | `xs:import @namespace="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2"` | 1 |
//! | `xs:import @namespace="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2"` | 1 |
//! | `xs:import @namespace="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2"` | 4 |
//! | `xs:import @namespace="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2"` | 1 |
//! | `xs:include` | 1 |
//! | `xs:restriction (in xs:simpleContent)` | 7 |
//! | `xs:restriction (in xs:simpleType)` | 3 |
//! | `xs:schema` | 13 |
//! | `xs:schema @attributeFormDefault="unqualified"` | 12 |
//! | `xs:schema @elementFormDefault="qualified"` | 13 |
//! | `xs:sequence` | 255 |
//! | `xs:sequence @maxOccurs="unbounded"` | 3 |
//! | `xs:sequence @minOccurs="0"` | 3 |
//! | `xs:simpleContent` | 912 |
//! | `xs:simpleType` | 3 |
//! INVENTORY-END
pub use Builtin;
pub use ;
pub use ;
pub use ;
pub use ;
/// The `encoding` pseudo-attribute of the XML declaration, if any.