Validation¶
pyxsd is a lax validator: it never aborts on bad data. Every problem it
finds is recorded as a ValidationIssue in a single
ValidationReport reachable at PyXSD.report.
A run can produce a complete object tree and a full error report at the same
time.
Note
“Lax validator” here means reporting is non-fatal. It is separate from
the parse mode (PyXSD(mode=...), --mode), which controls what is
bound into the tree when a document is invalid. The report is always
strict regardless of mode — see Parse modes and binding.
from pyxsd import PyXSD
parser = PyXSD(xmlFileInput="inventory.xml", xsdFile="schema.xsd")
if parser.report.has_errors:
for issue in parser.report.issues:
print(issue.format())
Report API¶
- class pyxsd.validation.ValidationReport[source]¶
An ordered collection of validation issues.
Reports are owned by a
PyXSDrun and filled in by the binding machinery as it walks the instance document.- phase: str¶
The phase new issues are attributed to unless one is passed explicitly. The parser flips this between schema compilation and instance binding.
- add_warning(message, *, code, element=None, phase=None)[source]¶
Record a recoverable-severity issue.
- for_phase(phase)[source]¶
Only the issues attributed to phase (
"schema"/"instance").- Parameters:
phase (str)
- Return type:
- property issues: list[ValidationIssue]¶
All recorded issues, in the order they were found.
- property errors: list[ValidationIssue]¶
Only the error-severity issues.
- property warnings: list[ValidationIssue]¶
Only the warning-severity issues.
- class pyxsd.validation.ValidationIssue(severity, code, message, element=None, phase='instance')[source]¶
A single problem found while validating a document.
severity: theIssueSeverityof the issue.code: a short, stable, machine-readable tag describing the kind of problem (for example"missing-attribute"or"order"). Codes are documented in the validation guide and are safe to match on in tooling.message: a human-readable, single-line description.element: the name of the element or type the issue was found in, when known.phase: the pipeline stage that produced the issue, either"schema"(reading the XSD, composing included schemas, building the generated classes) or"instance"(binding the XML document). Tooling and the conformance runner use this to check each stage independently.
- Parameters:
severity (IssueSeverity)
code (str)
message (str)
element (str | None)
phase (str)
- severity: IssueSeverity¶
Issue codes¶
Codes are stable strings. ERROR-level codes fail under --strict or
report.has_errors; WARNING-level codes never affect exit status.
Code |
Severity |
Meaning |
|---|---|---|
|
ERROR |
Child order violates the sequence content model. |
|
ERROR |
Element is not declared in the content model and no wildcard allows it. |
|
ERROR |
|
|
ERROR |
Fewer occurrences than |
|
ERROR |
More occurrences than |
|
ERROR |
A required attribute is absent. |
|
WARNING |
Attribute not declared in the schema. |
|
ERROR |
Attribute value fails its declared type. |
|
WARNING |
Attribute declared |
|
ERROR |
Attribute present with a value differing from |
|
ERROR |
Referenced type could not be resolved. |
|
ERROR |
A prefixed name uses a namespace prefix that is not bound in scope (namespaced mode). |
|
ERROR |
Text content failed lexical validation for its type. |
|
ERROR |
Element default value is not valid for the element’s type. |
|
ERROR |
Element content differs from its |
|
ERROR |
|
|
ERROR |
Instance of an abstract element declaration. |
|
ERROR |
Direct instance of an abstract type (allowed only via |
|
ERROR |
Substitution-group member blocked by the head’s |
|
ERROR |
|
|
ERROR |
Document root matches no global element declaration. |
|
ERROR |
More than one global element matches the document root. |
|
ERROR |
Key field missing or duplicate key value. |
|
ERROR |
Duplicate value under a |
|
ERROR |
Keyref value has no matching key/unique value. |
|
WARNING |
Identity-constraint XPath uses an unsupported construct. |
|
ERROR |
Referenced |
|
ERROR |
Group reference cycle. |
|
ERROR |
Referenced |
|
ERROR |
Nested attributeGroup reference cycle. |
|
ERROR |
Substitution group references a missing head. |
|
ERROR |
Element |
|
ERROR |
Derivation violates the base type’s |
|
ERROR |
The schema file itself is malformed or unreadable. |
|
WARNING |
Malformed schemaLocation hint in the instance document. |
|
ERROR |
Missing or malformed included/imported schema file. |
|
ERROR |
Namespace-only |
|
WARNING |
A repeated include/redefine was deduplicated. |
|
ERROR |
Include target namespace mismatch. |
|
WARNING |
Parser internal inconsistency — please report. |
Strict mode¶
pyxsd --strict exits with status 1 when the report contains any
ERROR-severity issue. Warnings never affect the exit code.