dcclib.validation ¶
SchemaDownloadError ¶
Bases: Exception
Raised when a DCC schema (or the list of released versions) could not be
fetched from PTB's servers. This error signals a
network/availability problem rather than an invalid document. Callers that
need to validate offline should provide a local schema file instead (e.g.
XsdValidator.from_file / the CLI --schema option).
SchematronFinding
dataclass
¶
SchematronFinding(node: etree.Element)
Class to represent a single finding of a schematron validation.
A finding is created for every schematron rule that fired, either because an
sch:assert failed or because an sch:report matched.
Create a SchematronFinding from an SVRL element.
| Parameters: |
|
|---|
SchematronFindingKind ¶
Bases: StrEnum
The kind of schematron rule that produced a finding.
The values are the SVRL element names, so a finding keeps the vocabulary of the SVRL document it was parsed from.
default_severity
property
¶
default_severity: Severity
The severity of a rule of this kind with no role attribute.
A failing sch:assert states that the document is wrong, while a
matching sch:report merely reports something about it.
SchematronValidationResult
dataclass
¶
SchematronValidationResult(findings: list[SchematronFinding] | None = None)
Class to represent the result of an XML validation against a schematron file.
warnings
property
¶
warnings: list[SchematronFinding]
All findings that warn about the document without invalidating it.
by_severity ¶
by_severity(severity: Severity) -> list[SchematronFinding]
All findings of a given severity.
| Parameters: |
|
|---|
| Returns: |
|
|---|
SchematronValidator ¶
SchematronValidator(svrl_content: str)
Bases: Constructible, Validatable
Class to validate XML files against a schematron file.
Create a SchematronValidator.
| Parameters: |
|
|---|
for_dcc
classmethod
¶
for_dcc()
Create a SchematronValidator for the DCC schematron.
| Returns: |
|
|---|
from_file
classmethod
¶
from_file(schematron_path: str)
Create a SchematronValidator from a file path.
| Parameters: |
|
|---|
| Returns: |
|
|---|
from_str
classmethod
¶
from_str(schematron_content: str)
Create a SchematronValidator from a string.
| Parameters: |
|
|---|
| Returns: |
|
|---|
from_tree
classmethod
¶
from_tree(tree: etree.Element)
Create a SchematronValidator from an etree.Element tree.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_file ¶
validate_file(xml_path: str) -> SchematronValidationResult
Validate an XML file against a schematron file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_str ¶
validate_str(xml_content: str) -> SchematronValidationResult
Validate an XML file against a schematron file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_tree ¶
validate_tree(tree: etree.Element) -> SchematronValidationResult
Validate an XML file against a schematron file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Severity ¶
Bases: StrEnum
How much a validation finding matters.
Being a :class:~enum.StrEnum keeps the members usable wherever a plain
string is expected: they render and compare equal to "error",
"warning" and "info", and serialise to those values in JSON.
Validatable ¶
Bases: ABC
Abstract base class for classes that can validate a tree structure, file, or string representation.
validate_file
abstractmethod
¶
validate_file(xml_path: str)
Validate a file path.
| Parameters: |
|
|---|
validate_str
abstractmethod
¶
validate_str(xml_content: str)
Validate a string representation.
| Parameters: |
|
|---|
validate_tree
abstractmethod
¶
validate_tree(tree: etree.Element)
Validate a tree structure.
| Parameters: |
|
|---|
XmlError
dataclass
¶
XmlError(log_error)
Class to represent an error that occurred during XML validation.
XsdValidationResult
dataclass
¶
XsdValidationResult(is_valid: bool, errors: list[XmlError] | None = None)
Class to represent the result of an XML validation against a schema.
XsdValidator ¶
XsdValidator(xmlschema_doc: etree.Element, parser: etree.XMLParser)
Bases: Constructible, Validatable
Class to validate XML files against a schema.
Create an XsdValidator.
| Parameters: |
|
|---|
from_auto_detection
classmethod
¶
from_auto_detection()
Create an XsdValidator by auto-detecting the schema version.
| Returns: |
|
|---|
from_file
classmethod
¶
from_file(schema_path: str)
Create an XsdValidator from a file path.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
from_str
classmethod
¶
from_str(schema_content: str)
Create an XsdValidator from a string.
| Parameters: |
|
|---|
| Returns: |
|
|---|
from_tree
classmethod
¶
from_tree(tree: etree.Element)
Create an XsdValidator from an XML schema tree.
| Parameters: |
|
|---|
| Returns: |
|
|---|
from_version
classmethod
¶
from_version(version: str)
Create an XsdValidator from a schema version string.
The released XSD schema for the version is downloaded (on first use) and
cached locally via dcc_json_toolkit.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_file ¶
validate_file(xml_path: str) -> XsdValidationResult
Validate an XML file against an XSD schema.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_str ¶
validate_str(xml_content: str) -> XsdValidationResult
Validate an XML file against an XSD schema.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_tree ¶
validate_tree(tree: etree.Element) -> XsdValidationResult
Validate an XML tree against an XSD schema.
| Parameters: |
|
|---|
| Returns: |
|
|---|
compile_schematron_to_svrl ¶
compile_schematron_to_svrl(schematron: str) -> str
Compile a schematron file to a svrl file.
| Parameters: |
|
|---|
get_released_schema_versions
cached
¶
get_released_schema_versions() -> list[str]
Fetch the list of released DCC schema versions from PTB (https://www.ptb.de/dcc/releases.json).
| Returns: |
|
|---|
| Raises: |
|
|---|
resolve_severity ¶
resolve_severity(kind: SchematronFindingKind, role: str = '', flag: str = '') -> Severity
Determine the severity of a schematron finding.
| Parameters: |
|
|---|
| Returns: |
|
|---|
errors ¶
SchemaDownloadError ¶
Bases: Exception
Raised when a DCC schema (or the list of released versions) could not be
fetched from PTB's servers. This error signals a
network/availability problem rather than an invalid document. Callers that
need to validate offline should provide a local schema file instead (e.g.
XsdValidator.from_file / the CLI --schema option).
SchematronFinding
dataclass
¶
SchematronFinding(node: etree.Element)
Class to represent a single finding of a schematron validation.
A finding is created for every schematron rule that fired, either because an
sch:assert failed or because an sch:report matched.
Create a SchematronFinding from an SVRL element.
| Parameters: |
|
|---|
XmlError
dataclass
¶
XmlError(log_error)
Class to represent an error that occurred during XML validation.
results ¶
SchematronValidationResult
dataclass
¶
SchematronValidationResult(findings: list[SchematronFinding] | None = None)
Class to represent the result of an XML validation against a schematron file.
warnings
property
¶
warnings: list[SchematronFinding]
All findings that warn about the document without invalidating it.
by_severity ¶
by_severity(severity: Severity) -> list[SchematronFinding]
All findings of a given severity.
| Parameters: |
|
|---|
| Returns: |
|
|---|
XsdValidationResult
dataclass
¶
XsdValidationResult(is_valid: bool, errors: list[XmlError] | None = None)
Class to represent the result of an XML validation against a schema.
schematron ¶
SchematronValidator ¶
SchematronValidator(svrl_content: str)
Bases: Constructible, Validatable
Class to validate XML files against a schematron file.
Create a SchematronValidator.
| Parameters: |
|
|---|
for_dcc
classmethod
¶
for_dcc()
Create a SchematronValidator for the DCC schematron.
| Returns: |
|
|---|
from_file
classmethod
¶
from_file(schematron_path: str)
Create a SchematronValidator from a file path.
| Parameters: |
|
|---|
| Returns: |
|
|---|
from_str
classmethod
¶
from_str(schematron_content: str)
Create a SchematronValidator from a string.
| Parameters: |
|
|---|
| Returns: |
|
|---|
from_tree
classmethod
¶
from_tree(tree: etree.Element)
Create a SchematronValidator from an etree.Element tree.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_file ¶
validate_file(xml_path: str) -> SchematronValidationResult
Validate an XML file against a schematron file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_str ¶
validate_str(xml_content: str) -> SchematronValidationResult
Validate an XML file against a schematron file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_tree ¶
validate_tree(tree: etree.Element) -> SchematronValidationResult
Validate an XML file against a schematron file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
compile_schematron_to_svrl ¶
compile_schematron_to_svrl(schematron: str) -> str
Compile a schematron file to a svrl file.
| Parameters: |
|
|---|
severity ¶
SchematronFindingKind ¶
Bases: StrEnum
The kind of schematron rule that produced a finding.
The values are the SVRL element names, so a finding keeps the vocabulary of the SVRL document it was parsed from.
default_severity
property
¶
default_severity: Severity
The severity of a rule of this kind with no role attribute.
A failing sch:assert states that the document is wrong, while a
matching sch:report merely reports something about it.
Severity ¶
Bases: StrEnum
How much a validation finding matters.
Being a :class:~enum.StrEnum keeps the members usable wherever a plain
string is expected: they render and compare equal to "error",
"warning" and "info", and serialise to those values in JSON.
resolve_severity ¶
resolve_severity(kind: SchematronFindingKind, role: str = '', flag: str = '') -> Severity
Determine the severity of a schematron finding.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validatable ¶
Validatable ¶
Bases: ABC
Abstract base class for classes that can validate a tree structure, file, or string representation.
validate_file
abstractmethod
¶
validate_file(xml_path: str)
Validate a file path.
| Parameters: |
|
|---|
validate_str
abstractmethod
¶
validate_str(xml_content: str)
Validate a string representation.
| Parameters: |
|
|---|
validate_tree
abstractmethod
¶
validate_tree(tree: etree.Element)
Validate a tree structure.
| Parameters: |
|
|---|
xsd ¶
XsdValidator ¶
XsdValidator(xmlschema_doc: etree.Element, parser: etree.XMLParser)
Bases: Constructible, Validatable
Class to validate XML files against a schema.
Create an XsdValidator.
| Parameters: |
|
|---|
from_auto_detection
classmethod
¶
from_auto_detection()
Create an XsdValidator by auto-detecting the schema version.
| Returns: |
|
|---|
from_file
classmethod
¶
from_file(schema_path: str)
Create an XsdValidator from a file path.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
from_str
classmethod
¶
from_str(schema_content: str)
Create an XsdValidator from a string.
| Parameters: |
|
|---|
| Returns: |
|
|---|
from_tree
classmethod
¶
from_tree(tree: etree.Element)
Create an XsdValidator from an XML schema tree.
| Parameters: |
|
|---|
| Returns: |
|
|---|
from_version
classmethod
¶
from_version(version: str)
Create an XsdValidator from a schema version string.
The released XSD schema for the version is downloaded (on first use) and
cached locally via dcc_json_toolkit.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_file ¶
validate_file(xml_path: str) -> XsdValidationResult
Validate an XML file against an XSD schema.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_str ¶
validate_str(xml_content: str) -> XsdValidationResult
Validate an XML file against an XSD schema.
| Parameters: |
|
|---|
| Returns: |
|
|---|
validate_tree ¶
validate_tree(tree: etree.Element) -> XsdValidationResult
Validate an XML tree against an XSD schema.
| Parameters: |
|
|---|
| Returns: |
|
|---|
detect_schema_version ¶
detect_schema_version(tree: etree.Element) -> str
Detect the schema version of an XML file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
get_released_schema_versions
cached
¶
get_released_schema_versions() -> list[str]
Fetch the list of released DCC schema versions from PTB (https://www.ptb.de/dcc/releases.json).
| Returns: |
|
|---|
| Raises: |
|
|---|
get_schema_path
cached
¶
get_schema_path(version: str) -> str
Resolve a DCC schema version to a local XSD file path.
Uses dcc_json_toolkit to download (on first use) and cache the released
XSD schema for the given version, returning the path to the locally cached
dcc.xsd. Results are memoised so each version is fetched at most once.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|