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
//! APIs for parsing RELAX NG schema document and validate XML document using parsed schema.
//!
//! The current implementation supports both Full syntax and Compact Syntax.
//!
//! # Streaming validation
//! This crate supports SAX handler-based validators. \
//! By generating handlers from parsed schemas and configuring them as custom SAX handlers
//! in the parser, it enables document parsing and validation at the same time.
//!
//! ## Example
//! ```rust
//! use anyxml::{
//! relaxng::RelaxNGSchema,
//! sax::{DefaultSAXHandler, XMLReader},
//! };
//!
//! // example of RELAX NG Tutorial 1. Getting started
//! // https://relaxng.org/tutorial-20011203.html
//! const SCHEMA: &str = r#"<element name="addressBook" xmlns="http://relaxng.org/ns/structure/1.0">
//! <zeroOrMore>
//! <element name="card">
//! <element name="name"><text/></element>
//! <element name="email"><text/></element>
//! </element>
//! </zeroOrMore>
//! </element>
//! "#;
//! const DOCUMENT: &str = r#"<addressBook>
//! <card>
//! <name>John Smith</name>
//! <email>js@example.com</email>
//! </card>
//! <card>
//! <name>Fred Bloggs</name>
//! <email>fb@example.net</email>
//! </card>
//! </addressBook>"#;
//!
//! let mut schema = RelaxNGSchema::parse_str(SCHEMA, None, Some(DefaultSAXHandler)).unwrap();
//! let validator = schema.new_validate_handler(DefaultSAXHandler);
//! let mut reader = XMLReader::builder().set_handler(validator).build();
//! reader.parse_str(DOCUMENT, None).unwrap();
//! // `last_error` holds the last validation error.
//! assert!(reader.handler.last_error.is_ok());
//! ```
//!
//! # Document tree validation
//! It is also possible to validate an already constructed document tree.
//!
//! Compared to streaming validation, a limitation is that elements in the document tree do
//! not have positional information within the document's string representation, so less
//! information is available from errors. \
//! On the other hand, there is the advantage of being able to validate subtrees of the
//! document tree. This is useful when validating an XML document embedded within another
//! document.
//!
//! ## Example
//! ```rust
//! use anyxml::{
//! relaxng::RelaxNGSchema,
//! sax::{DefaultSAXHandler, XMLReader},
//! tree::TreeBuildHandler,
//! };
//!
//! // a part of example of RELAX NG Tutorial 1. Getting started
//! // https://relaxng.org/tutorial-20011203.html
//! const SCHEMA: &str = r#"<element name="card" xmlns="http://relaxng.org/ns/structure/1.0">
//! <element name="name"><text/></element>
//! <element name="email"><text/></element>
//! </element>
//! "#;
//! const DOCUMENT: &str = r#"<addressBook>
//! <card>
//! <name>John Smith</name>
//! <email>js@example.com</email>
//! </card>
//! <card>
//! <name>Fred Bloggs</name>
//! <email>fb@example.net</email>
//! </card>
//! </addressBook>"#;
//!
//! let mut schema = RelaxNGSchema::parse_str(SCHEMA, None, Some(DefaultSAXHandler)).unwrap();
//! let mut reader = XMLReader::builder().set_handler(TreeBuildHandler::default()).build();
//! reader.parse_str(DOCUMENT, None).unwrap();
//! let document = reader.handler.document;
//! let element = document.document_element().unwrap();
//! // document subtree validation
//! let card = element.first_element_child().unwrap();
//! assert!(schema.validate(&card, DefaultSAXHandler).is_ok());
//! ```
//!
//! # Compact Syntax support
//! This crate supports schemas in Compact Syntax as well as Full Syntax (XML Syntax). \
//! To parse a Compact Syntax schema, use the methods of [`RelaxNGSchema`], just as you
//! would with Full Syntax.
//!
//! Due to technical limitations, the schema is constructed by reading all resources
//! into memory and building the RELAX NG document tree, rather than parsing them in
//! a streaming manner. \
//! The constructed document tree can be received as an event via a handler. \
//! While conversion from Compact Syntax to Full Syntax is not explicitly supported, it is
//! possible to construct canonicalized XML or an XML document tree by forwarding events
//! received from the handler to the [`CanonicalizeHandler`](crate::c14n::CanonicalizeHandler)
//! or [`TreeBuildHandler`](crate::tree::TreeBuildHandler).
//!
//! ## Limitation
//! Schemas called via `include` or `externalRef` can be either Full Syntax or Compact Syntax. \
//! However, the current implementation identifies resources solely based on whether the URI
//! ends with `.rnc`. Therefore, Compact Syntax schemas whose URIs do not end with `.rnc` will
//! not be parsed correctly. The same applies to Full Syntax schemas whose URIs end with `.rnc`. \
//! As a workaround, it is possible to handle this by converting the resource using a custom
//! [`EntityResolver`](crate::sax::EntityResolver) beforehand.
//! In the future, it is planned to provide reliable resource type identification by allowing
//! the media type to be set on [`InputSource`](crate::sax::InputSource).
//!
//! ## Example
//! ```rust
//! use anyxml::{
//! relaxng::RelaxNGSchema,
//! sax::{DefaultSAXHandler, XMLReader},
//! };
//!
//! // conversion of the example of RELAX NG Tutorial 1. Getting started
//! // https://relaxng.org/tutorial-20011203.html
//! const SCHEMA: &str = r#"element addressBook {
//! element card {
//! element name { text },
//! element email { text }
//! }*
//! }"#;
//! const DOCUMENT: &str = r#"<addressBook>
//! <card>
//! <name>John Smith</name>
//! <email>js@example.com</email>
//! </card>
//! <card>
//! <name>Fred Bloggs</name>
//! <email>fb@example.net</email>
//! </card>
//! </addressBook>"#;
//!
//! let mut schema = RelaxNGSchema::parse_compact_str(SCHEMA, None, Some(DefaultSAXHandler)).unwrap();
//! let validator = schema.new_validate_handler(DefaultSAXHandler);
//! let mut reader = XMLReader::builder().set_handler(validator).build();
//! reader.parse_str(DOCUMENT, None).unwrap();
//! // `last_error` holds the last validation error.
//! assert!(reader.handler.last_error.is_ok());
//! ```
//!
//! # XSD types support
//! While the RELAX NG specification itself defines only two built-in data types
//! —`string` and `token`—this is often insufficient in practical use.
//!
//! This crate provides additional support for data types defined in
//! [XML Schema Part 2: Datatypes Second Edition](https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/),
//! in accordance with the [Guidelines for using W3C XML Schema Datatypes with RELAX NG](https://relaxng.org/xsd-20010907.html).
//!
//! To enable support for XSD types, schema authors must specify `"http://www.w3.org/2001/XMLSchema-datatypes"`
//! as the data type library, as indicated in the guidelines above.
//!
//! # Reference
//! - [ISO/IEC 19757-2:2008 Information technology — Document Schema Definition Language (DSDL)Part 2: Regular-grammar-based validation — RELAX NG](https://www.iso.org/standard/52348.html)
//! - [Guidelines for using W3C XML Schema Datatypes with RELAX NG](https://relaxng.org/xsd-20010907.html)
use Read;
use crate::;
pub use RncParseError;
pub use ValidateHandler;
/// RELAX NG namespace
pub const XML_RELAX_NG_NAMESPACE: &str = "http://relaxng.org/ns/structure/1.0";
/// RELAX NG annotation namespace
pub const XML_RELAX_NG_ANNOTATION_NAMESPACE: &str =
"http://relaxng.org/ns/compatibility/annotations/1.0";
/// Parsed RELAX NG schema.