fix(schema): enforce NameAndTypeOK fixed-value clause in particle restriction (#145)

When the base element has a fixed value constraint, a restricting element must
also be fixed to the same value, compared in the base type's value space so a
value-equal pair written in different lexical forms is not wrongly flagged.

XSTS invalid-schemas-accepted 148 to 144, no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 17:08:55 +02:00
parent d43d1bb1bb
commit ef28eac926
3 changed files with 33 additions and 3 deletions

View File

@ -22,6 +22,8 @@ Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-s
### Fixed
- XSTS invalid-schemas-accepted (#145): Particle Valid (Restriction) now enforces the NameAndTypeOK fixed-value clause. When the base element has a `fixed` value constraint, the restricting element must also be `fixed` to the same value (compared in the base type's value space, so a value-equal pair in different lexical forms is not wrongly flagged). Caught four invalid schemas: invalid-schemas-accepted 148 → 144, no other bucket moved.
- XSTS invalid-schemas-accepted (#145): Particle Valid (Restriction) now also covers anonymous complex types. The named-type subset check was keyed by type name, so an inline `complexType` deriving by `complexContent` restriction (the common `<element><complexType><complexContent><restriction base="...">` shape) was never validated against its base. Such restrictions are now compiled and checked with the same algorithm. Caught fifteen invalid schemas: invalid-schemas-accepted 163 → 148, no other bucket moved.
- XSTS invalid-schemas-accepted (#145): Particle Valid (Restriction) now enforces the NameAndTypeOK block-superset clause. A restricting element's `{disallowed substitutions}` (its `block`, or the schema's `blockDefault`) must be a superset of the base element's, so a restriction may block more substitutions but never fewer. The element particle now carries this `block` set. Caught sixteen invalid schemas: invalid-schemas-accepted 179 → 163, no other bucket moved.

View File

@ -66,13 +66,15 @@ extension PureXML.Schema {
return emptiable(base)
}
switch (restricted.term, base.term) {
case let (.element(restrictedName, _, restrictedTypeName, _, restrictedBlock), .element(baseName, _, baseTypeName, _, baseBlock)):
case let (.element(restrictedName, _, restrictedTypeName, restrictedValue, restrictedBlock), .element(baseName, baseType, baseTypeName, baseValue, baseBlock)):
// NameAndTypeOK: beyond name, occurrence, and type derivation, the
// restricting element's {disallowed substitutions} must be a superset
// of the base's (a restriction may block more, never fewer).
// of the base's (a restriction may block more, never fewer), and if
// the base is fixed the restriction must be fixed to the same value.
return rangeSubsumed(restricted, base)
&& sameName(restrictedName, baseName)
&& restrictedBlock.isSuperset(of: baseBlock)
&& fixedValueOK(restrictedValue, baseValue, baseType, types)
&& elementTypeRestrictionOK(restrictedTypeName, baseTypeName, types, derivation)
case let (.element(name, _, _, _, _), .wildcard(wildcard)):
return rangeSubsumed(restricted, base) && wildcard.admits(name)
@ -282,6 +284,28 @@ extension PureXML.Schema {
lhs.localName == rhs.localName && (lhs.namespaceURI ?? "") == (rhs.namespaceURI ?? "")
}
/// NameAndTypeOK value-constraint clause: when the base element is `fixed`,
/// the restricting element must also be `fixed` to the same value (compared in
/// the base type's value space, so `1` and `01` match). A base with no fixed
/// value, or a `default`, imposes no constraint on the restriction.
private static func fixedValueOK(_ restrictedValue: ValueConstraint?, _ baseValue: ValueConstraint?, _ baseType: ElementType?, _ types: [String: ElementType]) -> Bool {
guard let baseFixed = baseValue?.fixedValue else { return true }
guard let restrictedFixed = restrictedValue?.fixedValue else { return false }
return atomicType(baseType, types)?.valueMatches(restrictedFixed, literal: baseFixed) ?? (restrictedFixed == baseFixed)
}
/// The atomic simple type an element's type resolves to (following a
/// `typeReference` through the types map, bounded against cycles), or nil for
/// a complex, list, or union type.
private static func atomicType(_ type: ElementType?, _ types: [String: ElementType], _ depth: Int = 0) -> PureXML.Schema.SimpleType? {
guard depth < 32 else { return nil }
switch type {
case let .simple(simple): return simple
case let .typeReference(key): return atomicType(types[key], types, depth + 1)
default: return nil
}
}
/// Whether every leaf a particle can contain is admitted by `wildcard`: an
/// element by name, a nested wildcard by narrowing.
private static func leavesAdmitted(_ particle: Particle, by wildcard: Wildcard) -> Bool {

View File

@ -95,8 +95,12 @@ struct XSTSSuiteTests {
/// declarations, which the named-type rule never saw) with the same
/// Particle-Valid-Restriction algorithm then caught fifteen more invalid schemas:
/// invalid-schemas accepted 163 → 148, no other bucket moved.
/// Enforcing the NameAndTypeOK fixed-value clause (a restriction of a `fixed`
/// base element must be fixed to the same value, compared in the base type's
/// value space) then caught four more invalid schemas: invalid-schemas accepted
/// 148 → 144, no other bucket moved.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 148
private let knownSchemaInvalidAccepted = 144
private let knownInstanceValidRejected = 3
private let knownInstanceInvalidAccepted = 132