pyxsd.relaxng.datatypes

RELAX NG datatype protocols and the built-in/XSD 1.0 libraries.

RELAX NG delegates lexical acceptance and value equality to datatype libraries. A factory compiles a type name, its parameters, and the schema-side context into a reusable Datatype. Every operation receives the context of the string it interprets, so a value pattern can compare a schema-side QName against an instance-side QName in different prefix scopes.

The empty library URI is the RELAX NG built-in library. It has exactly the two datatypes of RELAX NG 1.0 §6.2.9: string accepts every string and compares by identity, and token accepts every string and compares after XML-whitespace normalization. Parameters are not allowed.

XSD_LIBRARY adapts pyxsd’s existing XSD built-in classes. The adapter follows XSD 1.0 semantics, not the XSD compiler’s default 1.1 profile:

  • the XSD 1.1-only types anyAtomicType, dateTimeStamp, dayTimeDuration, yearMonthDuration (and precisionDecimal) are rejected explicitly rather than accepted as strings;

  • lexical year 0000 is rejected for date, dateTime, gYear and gYearMonth; lexical -0001 is 1 BCE, so 1 BCE is a leap year and -0001-02-29 is valid while -0004-02-29 (4 BCE) is not;

  • temporal value equality, bounds, and decoding use the XSD 1.0 astronomical mapping for negative years (lexical y < 0 is astronomical y + 1), not the shared classes’ XSD 1.1 numbering;

  • duration equality uses the six-dimensional XSD 1.0 value space, so P1Y and P12M are distinct values;

  • duration bound facets use the XSD 1.0 §3.2.6.2 four-reference-date partial order, and an indeterminate comparison is false for every bound facet, as §3.2.6.3 requires;

  • anyURI uses the XSD 1.0 lexical space: the XLink 5.4 escaping procedure is applied, and the result must be an RFC 2396 URI reference as amended by RFC 2732. XSD 1.1’s “any XML character sequence” rule is not used. Equality remains identity of the lexical URI-reference representation; XSD 1.0 defines no anyURI normalization.

Scheme-specific URI syntax is not checked, matching XSD 1.0’s conformance note; the generic URI-reference grammar is.

The XSD adapter rejects the whiteSpace and enumeration parameters as the RELAX NG datatype guidelines require. Repeated pattern parameters are intersected; every other parameter may appear at most once. Facets are validated when the datatype is compiled, and an inapplicable facet is a parameter error.

allows, equal, decode and encode are independent operations. decode and encode are optional pyxsd capabilities, not RELAX NG conformance: a custom datatype may implement only validation, in which case the inherited methods raise UnsupportedDatatypeOperation. A datatype that genuinely provides decoding must declare supports_decode = True; overriding Datatype.decode() is not by itself a capability declaration, because an implementation may override it only to report the operation as unsupported. Value equality deliberately ignores facet parameters because RELAX NG’s datatypeEqual service has no parameters.

A compiled datatype is frozen, hashable, and value-comparable. Its repr is deterministic because the canonical choice order of the pattern IR sorts leaves by repr. The schema-side DatatypeContext is retained for later value matching; for context-insensitive types it does not participate in datatype equality, while QName and NOTATION datatypes compare by their schema-side namespace bindings.

Attributes

Exceptions

DatatypeError

Base class for datatype capability and parameter failures.

UnsupportedDatatypeError

A datatype library or type is outside the adapter's capabilities.

DatatypeParameterError

A datatype parameter is unknown, inapplicable, or inconsistent.

DatatypeRegistrationError

A datatype-library registration is invalid or would replace an identity.

UnsupportedDatatypeOperation

The datatype has no decoder or encoder for the requested operation.

Classes

DatatypeContext

The schema- or instance-side context a lexical value is read in.

DatatypeParameter

One RELAX NG datatype parameter: an XSD facet name and its lexical value.

Datatype

The compiled behavior of one RELAX NG datatype.

DatatypeFactory

A datatype library: compile a type name and parameters into a datatype.

ScalarDatatype

A frozen scalar datatype backed by one pyxsd XSD built-in class.

BuiltinDatatypeFactory

RELAX NG's built-in datatype library: string and token.

XsdDatatypeFactory

The RELAX NG XSD datatype library with XSD 1.0 semantics.

DatatypeLibraryRegistration

A custom datatype library factory and its stable configuration identity.

DatatypeLibraryRegistry

The available datatype libraries with fixed built-in/XSD identities.

Functions

datatype_supports_decode(datatype)

Return whether datatype explicitly declares decoder support.

datatype_supports_encode(datatype)

Return whether datatype explicitly declares encoder support.

resolve_datatype_selector(selector)

Resolve a selector to (library_uri, type_name).

Module Contents

pyxsd.relaxng.datatypes.BUILTIN_LIBRARY = ''
pyxsd.relaxng.datatypes.XSD_LIBRARY = 'http://www.w3.org/2001/XMLSchema-datatypes'
pyxsd.relaxng.datatypes.BUILTIN_FINGERPRINT = 'pyxsd.relaxng.builtin/1'
pyxsd.relaxng.datatypes.XSD_FINGERPRINT = 'pyxsd.relaxng.xsd-1.0/3'
pyxsd.relaxng.datatypes.DatatypeSelector
exception pyxsd.relaxng.datatypes.DatatypeError

Base class for datatype capability and parameter failures.

exception pyxsd.relaxng.datatypes.UnsupportedDatatypeError

A datatype library or type is outside the adapter’s capabilities.

exception pyxsd.relaxng.datatypes.DatatypeParameterError

A datatype parameter is unknown, inapplicable, or inconsistent.

exception pyxsd.relaxng.datatypes.DatatypeRegistrationError

A datatype-library registration is invalid or would replace an identity.

exception pyxsd.relaxng.datatypes.UnsupportedDatatypeOperation

The datatype has no decoder or encoder for the requested operation.

class pyxsd.relaxng.datatypes.DatatypeContext

The schema- or instance-side context a lexical value is read in.

base_uri is retained for datatypes that resolve relative references. namespaces maps in-scope prefixes to namespace URIs; the empty prefix is the default namespace. The mapping is copied into an immutable, hashable form at construction.

base_uri: str | None
namespaces: collections.abc.Mapping[str, str]
class pyxsd.relaxng.datatypes.DatatypeParameter

One RELAX NG datatype parameter: an XSD facet name and its lexical value.

name: str
value: str
class pyxsd.relaxng.datatypes.Datatype

The compiled behavior of one RELAX NG datatype.

supports_decode is an explicit, schema-static capability declaration for decode(); supports_encode is the matching declaration for encode(). The base class does not infer capability from method overrides: a subclass may override decode or encode only to raise UnsupportedDatatypeOperation, and such a datatype remains nondecodable/nonencodable for shape selection until it declares support. The flags are implementation behavior, not datatype semantic identity; they do not participate in equality, hashing, or pattern identity.

Implementations that serve as pattern payloads must also be immutable, hashable, value-comparable, and deterministically repr-able, because the pattern IR interns and canonically orders them.

supports_decode: ClassVar[bool] = False
supports_encode: ClassVar[bool] = False
abstractmethod allows(lexical, context)

Return whether lexical is a legal value in context.

Parameters:
Return type:

bool

abstractmethod equal(left, left_context, right, right_context)

Return whether two lexical values denote the same datatype value.

Parameters:
Return type:

bool

decode(lexical, context)

Decode lexical into a Python value when support is declared.

Parameters:
Return type:

Any

encode(value, context)

Encode a Python value as a lexical form, when the datatype supports it.

Parameters:
Return type:

str

pyxsd.relaxng.datatypes.datatype_supports_decode(datatype)

Return whether datatype explicitly declares decoder support.

This reads the class-level Datatype.supports_decode declaration. It is not an inference from a decode override and does not call decode. Instance state cannot turn capability on or off.

Parameters:

datatype (object)

Return type:

bool

pyxsd.relaxng.datatypes.datatype_supports_encode(datatype)

Return whether datatype explicitly declares encoder support.

This reads the class-level Datatype.supports_encode declaration. It never infers support from an encode override and never calls encode. Instance state cannot turn capability on or off.

Parameters:

datatype (object)

Return type:

bool

class pyxsd.relaxng.datatypes.DatatypeFactory

A datatype library: compile a type name and parameters into a datatype.

abstractmethod create(type_name, parameters, context)

Compile one datatype or report why it is unavailable.

Parameters:
Return type:

Datatype

class pyxsd.relaxng.datatypes.ScalarDatatype

A frozen scalar datatype backed by one pyxsd XSD built-in class.

context is the schema-side context retained for later value matching. It is excluded from equality for context-insensitive types; semantic_context carries the namespace bindings that actually affect QName/NOTATION identity.

supports_decode: ClassVar[bool] = True
supports_encode: ClassVar[bool] = True
library_uri: str
type_name: str
xsd_type: type[Any]
parameters: tuple[DatatypeParameter, ...]
context: DatatypeContext
constraints: pyxsd.facets.FacetConstraints
white_space: str
semantic_context: tuple[tuple[str, str], ...]
temporal_bounds: tuple[tuple[str, fractions.Fraction, str, bool], ...] = ()
duration_bounds: tuple[tuple[str, _Xsd10Duration, str], ...] = ()
allows(lexical, context)

Return whether lexical is a legal value in context.

Parameters:
Return type:

bool

equal(left, left_context, right, right_context)

Compare two values in this datatype’s value space.

Facet parameters do not participate: RELAX NG’s datatypeEqual service has no parameters. A lexical form outside the base datatype has no value and therefore compares unequal to everything.

Parameters:
Return type:

bool

decode(lexical, context)

Decode a validated lexical form into its Python value.

hexBinary and base64Binary return bytes; boolean returns a Python bool; QName and NOTATION return a context-resolved QName; the four year-bearing temporal types return a validated lexical str subclass with a lexical() method; every other type returns its XSD value object (an int, Decimal, float, or str subclass). Facets are enforced, so an out-of-range or pattern-violating lexical form raises TypeError or ValueError instead of producing a value outside the datatype.

Parameters:
Return type:

Any

encode(value, context)

Encode a Python value as a canonical lexical form.

Expected inputs are bytes-like for binary types, bool or int for boolean, int for the integer family, Decimal or int for decimal, int or float for float/double, str for string and temporal types, and QName or str for QName and NOTATION. The produced lexical form is validated against the datatype’s facets. A QName prefix must be bound in context; a resolved QName must denote the same namespace there, so no unresolved prefix is invented.

Parameters:
Return type:

str

class pyxsd.relaxng.datatypes.BuiltinDatatypeFactory

RELAX NG’s built-in datatype library: string and token.

create(type_name, parameters, context)

Compile one datatype or report why it is unavailable.

Parameters:
Return type:

Datatype

class pyxsd.relaxng.datatypes.XsdDatatypeFactory

The RELAX NG XSD datatype library with XSD 1.0 semantics.

create(type_name, parameters, context)

Compile one datatype or report why it is unavailable.

Parameters:
Return type:

Datatype

pyxsd.relaxng.datatypes.resolve_datatype_selector(selector)

Resolve a selector to (library_uri, type_name).

A bare string names a type in the built-in library. The documented xsd: shorthand names a type in the XSD library; it is a fixed alias, never a prefix lookup against the datatype context. A tuple is an explicit (library_uri, type_name) selector.

Parameters:

selector (DatatypeSelector)

Return type:

tuple[str, str]

class pyxsd.relaxng.datatypes.DatatypeLibraryRegistration

A custom datatype library factory and its stable configuration identity.

factory: DatatypeFactory
fingerprint: str
class pyxsd.relaxng.datatypes.DatatypeLibraryRegistry

The available datatype libraries with fixed built-in/XSD identities.

library_uris()

The registered library URIs in deterministic order.

Return type:

tuple[str, …]

registration(uri)

Return the registration for uri or raise an unsupported failure.

Parameters:

uri (str)

Return type:

DatatypeLibraryRegistration

register(uri, registration)

Register a custom datatype library under uri.

The built-in and XSD identities are fixed and cannot be replaced. Every custom fingerprint must be stable and process-independent, so a blank string or a captured object address is rejected.

Parameters:
Return type:

None

resolve(selector)

Resolve a selector without constructing a datatype.

Parameters:

selector (DatatypeSelector)

Return type:

tuple[str, str]

create(selector, parameters, context)

Compile the selected datatype.

Parameters:
Return type:

Datatype