Expand description
§xmlschema
XML Schema (XSD) validation for Rust. Pure safe code, no C.
The schema member of the oxml
suite. Rust has had no pure-Rust XSD validator: the only option was
libxml, which binds libxml2 through C-FFI and so brings a build
toolchain, unsafe, and no WebAssembly support. This closes that
gap.
§Quick Start
use xmlschema::{parse_schema, validate};
let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="year" type="xs:integer"/>
</xs:sequence>
<xs:attribute name="lang" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
"#;
let schema = parse_schema(xsd)?;
let doc = oxml::parse(
"<book lang='en'><title>Dune</title><year>1965</year></book>"
)?;
assert!(validate(&doc, &schema).is_valid());§Every violation, not the first
Validation collects rather than short-circuits, and each violation carries the path to the element it is about. A caller fixing a document wants the whole list, not to re-run the validator after every edit:
use xmlschema::{parse_schema, validate};
let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="year" type="xs:integer"/>
</xs:sequence>
<xs:attribute name="lang" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
"#;
let schema = parse_schema(xsd)?;
let doc = oxml::parse("<book><year>not-a-number</year></book>")?;
let report = validate(&doc, &schema);
// Both problems, in one pass.
assert_eq!(report.violations.len(), 2);§Supported
- Elements,
xs:sequence,xs:choice, cardinality viaminOccurs/maxOccursincludingunbounded - Attributes, with
use="required" - Built-in simple types: string, boolean, decimal, integer, non-negative integer, double, date, dateTime, anyURI
- Restriction facets:
enumeration,pattern,length,minLength,maxLength,minInclusive,maxInclusive,minExclusive,maxExclusive - Named top-level simple types, referenced by
type
§Not yet supported
xs:all, xs:import / xs:include / xs:redefine, substitution
groups, identity constraints (xs:key, xs:keyref, xs:unique),
type derivation by extension for complex content, and union or list
simple types. A schema using one of these is not rejected: the
construct is skipped and the surrounding rules still apply.
Re-exports§
pub use model::AttributeDecl;pub use model::BuiltIn;pub use model::Content;pub use model::Facets;pub use model::Identity;pub use model::IdentityKind;pub use model::NamespaceConstraint;pub use model::Occurs;pub use model::Particle;pub use model::ProcessContents;pub use model::Schema;pub use model::SimpleType;pub use model::Variety;pub use model::Wildcard;pub use parse::SchemaError;pub use parse::parse_schema;pub use parse::parse_schema_with;pub use pattern::Pattern;pub use pattern::PatternError;pub use resolve::NoSchemas;pub use resolve::SchemaSource;pub use validate::Report;pub use validate::Violation;pub use validate::validate;
Modules§
- datatype
- The XSD 1.0 built-in datatypes.
- derive
- Whether one content model is a valid restriction of another.
- model
- The schema model: what an
.xsdsays, in a form a validator can walk. - parse
- Reading an
.xsdinto aSchema. - pattern
- A small regular-expression engine for
xs:pattern. - resolve
- Schema documents the caller supplies, for
xs:importandxs:include. - support
- What this crate does not enforce in a given schema.
- validate
- Validating a document against a
Schema.