pyxsd.schematron.schema

Public facade for standalone ISO Schematron compilation and validation.

One SchematronSchema holds the immutable compiled schema produced by the source-load, resource-closure, abstract-expansion, and compilation stages. It may be shared: every SchematronSchema.validate() call opens its own expression session, variables, document registry, and execution budget, so no per-instance state or parser posture crosses validations.

Top-level sch:param declarations are immutable compiled definitions with query-fragment defaults, and SchematronSchema.validate() accepts session-local parameters overrides for them. Top-level library xsl:param declarations use the separate stylesheet_parameters channel because the two namespaces can collide. Typed sch:let/@as variables, dynamic check metadata, and XSLT 3.0 message text value templates evaluate through the same per-validation session.

Raw schema and instance sources are admitted once through the supplied ResourcePolicy or XML parser policy. An already-built XDM document or node is used unchanged, and a captured common Document contributes exactly its coherent to_xdm() tree. A document without a capture, or with a replaced root or edited bound tree, raises XdmInputError; its raw source is never reparsed.

A compile-time resource policy authorizes schema includes, external extends, and imported XSLT declarations. The whole closure compiles inside the policy’s per-compilation scope, and every dependency read names its containing resource as the base, so no dependency is ever authorized as a fresh root. It is not validation authority: a validation without an explicit resource_policy reads only local files, so callers grant instance and subordinate-document access per validation.

Classes

SchematronSchema

One immutable compiled Schematron schema, safe to share.

Functions

compile(source, *[, query_binding, resource_policy, ...])

Compile one Schematron schema; shorthand for SchematronSchema.compile.

Module Contents

class pyxsd.schematron.schema.SchematronSchema(compiled)

One immutable compiled Schematron schema, safe to share.

Construct one with compile() or the module-level compile(). The object stores no per-instance resources or budgets; a validation receives fresh state.

Parameters:

compiled (pyxsd.schematron.ir.CompiledSchema)

classmethod compile(source, *, query_binding=None, resource_policy=None, xml_policy=None, xml_limits=None, limits=None)

Load, resolve, expand, and compile one Schematron schema.

source is a local path, bytes-like object, or readable text/binary stream. A remote reference requires an authorizing resource_policy; without one it is a policy denial rather than a filesystem path.

resource_policy owns reading and parsing the schema and every include, external extends, and XSLT module it names. The whole closure compiles inside the policy’s per-compilation scope, the same scope pyxsd.compile() enters, so a policy that distinguishes a root from a dependency authorizes none of those reads as a fresh root and accounts every one against the same closure budgets. xml_policy and xml_limits configure parsing only when no resource policy is supplied; combining them is rejected before any input is read. They govern the root and every dependency parsed without a policy, so a bound the root satisfies also bounds each include and external extends.

query_binding overrides the authored @queryBinding. limits bounds resource closure, expansion, and expression compilation. Each field is one compile-stage budget: max_expanded_elements counts the elements that includes, external extends, and abstract expansion produce together, so a value every stage fits alone can still be exhausted by their sum. Compile failures raise SchematronError subclasses with the responsible source occurrence where one exists.

Parameters:
Return type:

SchematronSchema

validate(instance, *, phase=None, parameters=None, stylesheet_parameters=None, resource_policy=None, xml_policy=None, xml_limits=None, limits=None)

Validate one instance against this compiled schema.

instance is a raw local path, bytes-like object, or readable stream, an XDM document or subtree, or a captured common Document. Raw input is read once and those admitted bytes are parsed separately by the source index and the XDM builder, plus the supplied policy’s parser when one is present; every view describes the same revision. XDM input passes through unchanged; a common document contributes its coherent to_xdm() tree and is never reparsed. Parser options are rejected for already-parsed input.

phase selects the active patterns. None and #DEFAULT use the schema’s defaultPhase when declared and otherwise activate every pattern; #ALL activates every pattern without phase variables or a phase @from; #ANY evaluates only declared 2025 phase/@when conditions in document order and selects the first true phase, or activates every pattern when none match. Any other value names a declared phase and ignores that phase’s @when. A selected named or default phase may bound the original document’s candidate roots with @from, intersected with any supplied validation subtree; subordinate pattern/@documents documents stay unrestricted. An unknown name raises SchematronEvaluationError with stage="phase".

parameters supplies values for the compiled schema’s top-level sch:param declarations. pyxsd implements the standard’s optional user-supplied value this way: a key must name a declared parameter and a value is a primitive, XDM item, or sequence, converted through the host’s XDM machinery. An undeclared name, an unsupported Python value, or a failed conversion raises SchematronEvaluationError with stage="parameters". Overrides are session-local and never mutate the compiled schema; omitted parameters keep their evaluated defaults.

stylesheet_parameters supplies values for the schema’s top-level library xsl:param declarations, a separate channel from parameters because a schema parameter and an xsl:param can share a lexical name. Keys are the host’s Clark-name, EQName, or lexical-QName parameter names; values pass through the host’s XDM conversion and each declaration’s @as type. An undeclared or static name, a duplicate expanded name, a failed conversion, or a missing required parameter (including an XSLT 3.0 typed parameter with no default) raises SchematronEvaluationError with stage="parameters". Omitted defaulted parameters keep their declared default, and XSLT 2.0 does not apply the XSLT 3.0 implicit-mandatory rule. Bindings are session-local.

resource_policy authorizes raw instances and every subordinate document()/doc()/pattern/@documents read. Compilation authority is never reused, so None reaches only local files. limits bounds retained outcomes and shared execution work; an exhausted limit raises SchematronLimitError and never returns a partial report.

Parameters:
Return type:

pyxsd.schematron.model.SchematronReport

pyxsd.schematron.schema.compile(source, *, query_binding=None, resource_policy=None, xml_policy=None, xml_limits=None, limits=None)

Compile one Schematron schema; shorthand for SchematronSchema.compile.

Every argument has the same meaning as on the class method. The result is immutable and may serve concurrent validations of distinct documents.

Parameters:
Return type:

SchematronSchema