pub struct Schema { /* private fields */ }Expand description
A compiled Schematron schema, ready to validate documents.
Compiling is the expensive part; validating is not. Compile once and reuse.
Schema is immutable and Send + Sync, so one schema can validate
documents on many threads at the same time.
§Examples
use schematron::{Document, Schema};
let schema = Schema::from_str(r#"
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern>
<rule context="line">
<assert test="number(@qty) > 0">Quantity must be positive.</assert>
</rule>
</pattern>
</schema>
"#)?;
let document = Document::from_str("<order><line qty='-1'/></order>")?;
let report = schema.validate(&document)?;
assert_eq!(report.count_failures(), 1);Implementations§
Source§impl Schema
impl Schema
Sourcepub fn portability(&self) -> Vec<Lint>
pub fn portability(&self) -> Vec<Lint>
Constructs that behave differently under other Schematron processors.
These are not mistakes. A schema that uses @subject is correct,
and this crate implements it as the standard describes. But the ISO
reference implementation — the XSLT skeleton most other tools are
built on — behaves differently for each of the constructs reported
here, and a schema author has no way to discover that. Every one is
backed by a divergence recorded in spec/conformance/, established
by running both implementations.
They are deliberately kept out of Schema::lint. A linter that
reports correct code as a problem gets switched off, and then it
catches nothing; portability is a separate question, asked separately.
§Examples
use schematron::Schema;
let schema = Schema::from_str(
r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern>
<rule context="a" flag="warning">
<assert test="b">needs a b</assert>
</rule>
</pattern>
</schema>"#,
)
.unwrap();
// Correct, and portable nowhere: the reference drops a rule's flag.
assert!(schema.lint().is_empty());
assert_eq!(schema.portability().len(), 1);Sourcepub fn lint(&self) -> Vec<Lint>
pub fn lint(&self) -> Vec<Lint>
Inspects this schema for constructs that are legal but probably wrong.
Returns lints in schema order, so the output reads down the file. An
empty result does not mean the schema is correct — only that none of
the patterns in spec/linting/ matched.
For constructs that are correct here but behave differently under
other processors, see Schema::portability, which is deliberately
separate.
§Examples
use schematron::Schema;
let schema = Schema::from_str(r#"
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern><rule context="a"><assert test="b">m</assert></rule></pattern>
</schema>
"#)?;
assert!(schema.lint().is_empty());Source§impl Schema
impl Schema
Sourcepub fn from_str(source: &str) -> Result<Schema>
pub fn from_str(source: &str) -> Result<Schema>
Compiles a schema from source text.
Named from_str rather than implementing std::str::FromStr
deliberately: the trait’s signature has no room for
SchemaOptions, and a schema that cannot resolve its includes is
not much use.
§Errors
Returns Error::XmlParse if the schema is not well-formed XML,
Error::Schema if it is not a valid Schematron schema, and
Error::XPathSyntax if any expression in it does not compile.
Sourcepub fn from_str_with(source: &str, options: &SchemaOptions) -> Result<Schema>
pub fn from_str_with(source: &str, options: &SchemaOptions) -> Result<Schema>
Compiles a schema from source text, with options.
§Errors
As Schema::from_str, plus Error::Resolve,
Error::IncludeCycle, and Error::IncludeDepth from include
resolution.
Sourcepub fn from_path<P: AsRef<Path>>(path: P) -> Result<Schema>
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Schema>
Reads and compiles a schema from a file.
The file’s path becomes the base URI, so relative includes resolve against the directory the schema lives in.
§Errors
As Schema::from_str_with, plus Error::Io if the file cannot be
read.
Sourcepub fn from_document(
document: &Document,
options: &SchemaOptions,
) -> Result<Schema>
pub fn from_document( document: &Document, options: &SchemaOptions, ) -> Result<Schema>
Sourcepub fn validate(&self, document: &Document) -> Result<Report>
pub fn validate(&self, document: &Document) -> Result<Report>
Validates a document against this schema, using default options.
§Errors
Returns Error::XPathEval if an expression fails at runtime, and
Error::UnknownPhase if a phase was named that does not exist. A
document that simply breaks the rules is not an error: that is a
Report with failures in it.
Sourcepub fn validate_with(
&self,
document: &Document,
options: &ValidateOptions,
) -> Result<Report>
pub fn validate_with( &self, document: &Document, options: &ValidateOptions, ) -> Result<Report>
Sourcepub fn uses_document_function(&self) -> bool
pub fn uses_document_function(&self) -> bool
Whether any expression in this schema calls XPath document().
A schema that does not is validated against the caller’s document directly; one that does needs a working copy of the tree that loaded documents can be merged into.
Sourcepub fn model(&self) -> &SchemaModel
pub fn model(&self) -> &SchemaModel
The parsed model, after includes and abstractions are expanded.
Sourcepub fn namespaces(&self) -> &Namespaces
pub fn namespaces(&self) -> &Namespaces
The namespace bindings every expression is evaluated against.
Sourcepub fn schema_version(&self) -> Option<&str>
pub fn schema_version(&self) -> Option<&str>
The schema’s @schemaVersion.
Sourcepub fn version(&self) -> XPathVersion
pub fn version(&self) -> XPathVersion
The XPath version this schema’s expressions use.
Sourcepub fn query_binding(&self) -> &QueryBinding
pub fn query_binding(&self) -> &QueryBinding
The query language binding the schema declares.
Sourcepub fn phases(&self) -> impl Iterator<Item = &str>
pub fn phases(&self) -> impl Iterator<Item = &str>
The identifiers of the schema’s phases, in document order.
Sourcepub fn default_phase(&self) -> Option<&str>
pub fn default_phase(&self) -> Option<&str>
The schema’s @defaultPhase.