space_packet_parser.xtce.validation

XTCE document validation classes and utilities.

Attributes

logger

Exceptions

XtceValidationError

Exception raised during XTCE validation.

Classes

ValidationLevel

Validation levels for XTCE documents.

ValidationError

Represents a validation error or warning.

ValidationResult

Results of XTCE document validation.

Functions

_get_cache_dir(→ pathlib.Path)

Get cross-platform cache directory for space_packet_parser.

_get_cache_path(→ pathlib.Path)

Get cache file path for a schema URL using SHA-256 hash.

_read_from_cache(→ Optional[bytes])

Read cached schema content, return None if not found or unreadable.

_write_to_cache(→ None)

Write content to cache, creating directories as needed.

_fix_known_schema_issues(→ bytes)

Fix known issues in the official XTCE XSD schema.

_load_schema(→ tuple[lxml.etree.XMLSchema, str])

Load XSD schema from URL or local path

_find_schema_url(→ str)

Find the XSD location from the root attributes of the document

_validate_xtce_schema(→ ValidationResult)

Validate XML document against XSD schema.

_validate_xtce_structure(→ ValidationResult)

Validate XTCE document structure and reference integrity.

validate_xtce(→ ValidationResult)

Validate an XTCE XML document.

Module Contents

space_packet_parser.xtce.validation.logger
class space_packet_parser.xtce.validation.ValidationLevel(*args, **kwds)

Bases: enum.Enum

Validation levels for XTCE documents.

SCHEMA = 'schema'
STRUCTURE = 'structure'
ALL = 'all'
class space_packet_parser.xtce.validation.ValidationError

Represents a validation error or warning.

message: str
error_code: str
xpath_location: str | None = None
line_number: int | None = None
column_number: int | None = None
context: dict[str, Any] | None
__str__() str

String representation of validation error.

class space_packet_parser.xtce.validation.ValidationResult

Results of XTCE document validation.

valid: bool
validation_level: ValidationLevel
errors: list[ValidationError] = []
schema_version: str | None = None
schema_location: str | None = None
validation_time_ms: float | None = None
__bool__()
add_error(message: str, error_code: str, xpath_location: str | None = None, line_number: int | None = None, context: dict[str, Any] | None = None)

Add a validation error.

__str__() str

String representation of validation result.

exception space_packet_parser.xtce.validation.XtceValidationError(message: str, validation_result: ValidationResult | None = None)

Bases: Exception

Exception raised during XTCE validation.

validation_result = None
space_packet_parser.xtce.validation._get_cache_dir() pathlib.Path

Get cross-platform cache directory for space_packet_parser.

space_packet_parser.xtce.validation._get_cache_path(schema_url: str) pathlib.Path

Get cache file path for a schema URL using SHA-256 hash.

space_packet_parser.xtce.validation._read_from_cache(cache_path: pathlib.Path) bytes | None

Read cached schema content, return None if not found or unreadable.

space_packet_parser.xtce.validation._write_to_cache(cache_path: pathlib.Path, content: bytes) None

Write content to cache, creating directories as needed.

space_packet_parser.xtce.validation._fix_known_schema_issues(schema_content: bytes) bytes

Fix known issues in the official XTCE XSD schema.

The official OMG XTCE schema references xml:base but doesn’t declare the xml namespace, causing lxml validation to fail.

Parameters:

schema_content (bytes) – The schema content as bytes

Returns:

The fixed schema content as bytes

Return type:

bytes

space_packet_parser.xtce.validation._load_schema(schema_location: str | pathlib.Path, timeout: int = 30) tuple[lxml.etree.XMLSchema, str]

Load XSD schema from URL or local path

Parameters:
  • schema_location (Union[str, Path]) – URL or local path to the XSD schema document

  • timeout (int) – Timeout in seconds for URL downloads

Returns:

Parsed XMLSchema object and version string

Return type:

tuple[ElementTree.XMLSchema, str]

Raises:

XtceValidationError – If schema cannot be loaded or parsed

space_packet_parser.xtce.validation._find_schema_url(xml_tree: lxml.etree.ElementTree) str

Find the XSD location from the root attributes of the document

Parameters:

xml_tree (ElementTree.ElementTree) – XML tree of document being validated

Returns:

schema_location – URL of XSD

Return type:

str

space_packet_parser.xtce.validation._validate_xtce_schema(xml_tree: lxml.etree.ElementTree, local_xsd: str | pathlib.Path | None = None, timeout: int = 30) ValidationResult

Validate XML document against XSD schema.

Parameters:
  • xml_tree (ElementTree.ElementTree) – XTCE XML tree object

  • local_xsd (Optional[Union[str, Path]]) – Optional local schema location. If specified, schema references in root element (or lack thereof) are ignored.

  • timeout (int) – Timeout in seconds for schema downloads

Returns:

Truthy if result is valid, Falsy otherwise

Return type:

ValidationResult

space_packet_parser.xtce.validation._validate_xtce_structure(xml_tree: lxml.etree.ElementTree) ValidationResult

Validate XTCE document structure and reference integrity.

This performs structural validation beyond XSD schema validation, checking XTCE-specific business rules and reference integrity.

Parameters:

xml_tree (ElementTree.ElementTree) – Parsed XML tree of the XTCE document

Returns:

Truthy if result is valid, Falsy otherwise

Return type:

ValidationResult

space_packet_parser.xtce.validation.validate_xtce(xml_source: str | pathlib.Path | lxml.etree.ElementTree, level: str = 'all', timeout: int = 30, print_results: bool = True, raise_on_error: bool = True, local_xsd: str | pathlib.Path | None = None) ValidationResult

Validate an XTCE XML document.

This is the main validation entry point for XTCE documents. It can perform schema or structural validation based on the level parameter.

Parameters:
  • xml_source (Union[str, Path, ElementTree.ElementTree]) – Path to XML file, XML string content, or ElementTree

  • level (str) – Validation level: “schema”, “structure”, or “all”. Default “all”.

  • timeout (int) – Timeout in seconds for schema downloads

  • print_results (bool) – Default True. Prints results before returning Truthy or Falsy result.

  • raise_on_error (bool) – Default True. If False, returns a ValidationResult object with information about the validation results. If True, raises an exception unless the ValidationResult reports valid.

  • local_xsd (Optional[str, Path]) – Local path to an XSD for schema validation. If not provided and schema validation is requested, XSD is retrieved from schema reference attribute in document root.

Returns:

Truthy if result is valid, Falsy otherwise

Return type:

ValidationResult