Skip to main content

Schema

Struct Schema 

Source
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) &gt; 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

Source

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);
Source

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

Source

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.

Source

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.

Source

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.

Source

pub fn from_document( document: &Document, options: &SchemaOptions, ) -> Result<Schema>

Compiles a schema from an already-parsed XML document.

§Errors

As Schema::from_str_with.

Source

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.

Source

pub fn validate_with( &self, document: &Document, options: &ValidateOptions, ) -> Result<Report>

Validates a document against this schema, with options.

§Errors

As Schema::validate.

Source

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.

Source

pub fn model(&self) -> &SchemaModel

The parsed model, after includes and abstractions are expanded.

Source

pub fn namespaces(&self) -> &Namespaces

The namespace bindings every expression is evaluated against.

Source

pub fn id(&self) -> Option<&str>

The schema’s @id.

Source

pub fn title(&self) -> Option<&str>

The schema’s title.

Source

pub fn schema_version(&self) -> Option<&str>

The schema’s @schemaVersion.

Source

pub fn version(&self) -> XPathVersion

The XPath version this schema’s expressions use.

Source

pub fn query_binding(&self) -> &QueryBinding

The query language binding the schema declares.

Source

pub fn phases(&self) -> impl Iterator<Item = &str>

The identifiers of the schema’s phases, in document order.

Source

pub fn default_phase(&self) -> Option<&str>

The schema’s @defaultPhase.

Source

pub fn keys(&self) -> &[Key]

The keys, indexed before any pattern runs.

Source

pub fn patterns(&self) -> &[Pattern]

The patterns that will run, in order.

Trait Implementations§

Source§

impl Clone for Schema

Source§

fn clone(&self) -> Schema

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Schema

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.