pub struct Schema { /* private fields */ }Expand description
The top-level compiled schema. Cheap to clone (one Arc bump).
All names are fully resolved at this point; the validator looks up
types and elements by QName.
Implementations§
Source§impl Schema
impl Schema
Sourcepub fn compile_str(xsd: &str) -> Result<Schema, SchemaCompileError>
pub fn compile_str(xsd: &str) -> Result<Schema, SchemaCompileError>
Compile a schema from a single XSD source string. Equivalent to
compile_with using a NoResolver —
any <xs:import> or <xs:include> directive is rejected.
Sourcepub fn compile_str_with_options(
xsd: &str,
options: SchemaOptions,
) -> Result<Schema, SchemaCompileError>
pub fn compile_str_with_options( xsd: &str, options: SchemaOptions, ) -> Result<Schema, SchemaCompileError>
Compile a schema with explicit SchemaOptions (the version
knob today; more options may land here). The resolver is
NoResolver — imports / includes are rejected.
Sourcepub fn compile_with<R: SchemaResolver>(
xsd: &str,
resolver: R,
) -> Result<Schema, SchemaCompileError>
pub fn compile_with<R: SchemaResolver>( xsd: &str, resolver: R, ) -> Result<Schema, SchemaCompileError>
Compile a schema, resolving any <xs:import> / <xs:include> /
<xs:redefine> directives via the supplied SchemaResolver.
Cycles are detected (a schema importing one that already loaded is silently skipped); recursion is bounded at 64 levels deep.
Sourcepub fn compile_with_options<R: SchemaResolver>(
xsd: &str,
resolver: R,
options: SchemaOptions,
) -> Result<Schema, SchemaCompileError>
pub fn compile_with_options<R: SchemaResolver>( xsd: &str, resolver: R, options: SchemaOptions, ) -> Result<Schema, SchemaCompileError>
Compile a schema with both a SchemaResolver and explicit
SchemaOptions. This is the most general entry point; the
others all delegate here.
Sourcepub fn compile_file(
path: impl AsRef<Path>,
) -> Result<Schema, SchemaCompileError>
pub fn compile_file( path: impl AsRef<Path>, ) -> Result<Schema, SchemaCompileError>
Compile a schema from a file on disk, with relative-path
resolution of <xs:import> and <xs:include> against the
schema’s own directory.
Equivalent to:
let dir = path.parent().unwrap_or_else(|| Path::new("."));
Schema::compile_with(&fs::read_to_string(path)?, FsResolver::new(dir))Source§impl Schema
impl Schema
Sourcepub fn element(&self, name: &QName) -> Option<&Arc<ElementDecl>>
pub fn element(&self, name: &QName) -> Option<&Arc<ElementDecl>>
Look up a top-level element by qualified name.
Sourcepub fn attribute(&self, name: &QName) -> Option<&Arc<AttributeDecl>>
pub fn attribute(&self, name: &QName) -> Option<&Arc<AttributeDecl>>
Look up a top-level attribute by qualified name.
Sourcepub fn type_def(&self, name: &QName) -> Option<&TypeRef>
pub fn type_def(&self, name: &QName) -> Option<&TypeRef>
Look up a top-level type by qualified name. Returns None for
types not declared in this schema (callers handle built-ins via
the BuiltinType lookup).
pub fn target_namespace(&self) -> Option<&str>
Sourcepub fn elements(&self) -> impl Iterator<Item = (&QName, &Arc<ElementDecl>)>
pub fn elements(&self) -> impl Iterator<Item = (&QName, &Arc<ElementDecl>)>
Iterate every top-level element. Useful for instance validation to find a candidate root type.
Sourcepub fn types(&self) -> impl Iterator<Item = (&QName, &TypeRef)>
pub fn types(&self) -> impl Iterator<Item = (&QName, &TypeRef)>
Iterate every top-level type definition.
Sourcepub fn substitutes_for(&self, head: &QName) -> &[Arc<ElementDecl>]
pub fn substitutes_for(&self, head: &QName) -> &[Arc<ElementDecl>]
Substitution-group members for a given head, if any. Empty when no element substitutes for this head.
Source§impl Schema
impl Schema
pub fn validate_str(&self, xml: &str) -> Result<(), ValidationError>
pub fn validate_bytes(&self, xml: &[u8]) -> Result<(), ValidationError>
pub fn validate_str_opts( &self, xml: &str, opts: ValidationOptions, ) -> Result<(), ValidationError>
Sourcepub fn validate_doc(&self, doc: &Document) -> Result<(), ValidationError>
pub fn validate_doc(&self, doc: &Document) -> Result<(), ValidationError>
Validate an already-parsed Document
directly, without going through XML re-serialisation.
Equivalent to serialising the document and calling
validate_str, but skips that
round-trip — useful when the document came from
parse_str or
process_xincludes
and you’d otherwise burn a second parse pass.
Diagnostic positioning trade-off: source byte offsets aren’t
available from a Document (the original bytes are gone),
so reported issues carry line: None / column: None
instead of the precise positions you’d get from
validate_str. Element paths (/catalog/book[3]) are
reported the same either way.
Sourcepub fn validate_doc_opts(
&self,
doc: &Document,
opts: ValidationOptions,
) -> Result<(), ValidationError>
pub fn validate_doc_opts( &self, doc: &Document, opts: ValidationOptions, ) -> Result<(), ValidationError>
As validate_doc, but honours an
explicit ValidationOptions.
Sourcepub fn validate_doc_typed(
&self,
doc: &Document,
) -> (Result<(), ValidationError>, PsviTypes)
pub fn validate_doc_typed( &self, doc: &Document, ) -> (Result<(), ValidationError>, PsviTypes)
As validate_doc, but also returns
PsviTypes — a per-node map of the schema type that governed
each element during validation.
This is the post-schema-validation infoset entry point that
schema-aware XPath/XSLT processing builds on (typed atomization,
instance of element(*, T), data()). The returned table is
keyed by the addresses of doc’s nodes, so it is only valid
while doc is alive. Validation errors are reported in the
Result exactly as validate_doc reports them; the annotations
are returned regardless, so a caller that wants best-effort
typing of a partially-valid document can ignore the Result.