fix(schema): enforce NameAndTypeOK nillable clause in particle restriction (#145)

A restricting element may not be nillable unless the base element it restricts
is also nillable. The element particle (Term.element) now carries a nillable
flag, populated from the element's nillable attribute.

XSTS invalid-schemas-accepted 144 to 142, no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 17:25:10 +02:00
parent ef28eac926
commit bdd9e96634
9 changed files with 36 additions and 21 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 nillable clause. A restricting element may not be `nillable` unless the base element it restricts is also nillable. Caught two invalid schemas: invalid-schemas-accepted 144 → 142, no other bucket moved.
- 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.

View File

@ -107,7 +107,14 @@ public extension PureXML.Schema {
/// `block` is the element's `{disallowed substitutions}` (its own `block`, or
/// the schema's `blockDefault`), needed for the NameAndTypeOK block-superset
/// rule in Particle Valid (Restriction); empty when nothing is blocked.
case element(name: PureXML.Model.QualifiedName, type: ElementType?, typeName: String?, valueConstraint: ValueConstraint? = nil, block: Set<DerivationMethod> = [])
case element(
name: PureXML.Model.QualifiedName,
type: ElementType?,
typeName: String?,
valueConstraint: ValueConstraint? = nil,
block: Set<DerivationMethod> = [],
nillable: Bool = false,
)
case group(Group)
case wildcard(Wildcard)
}

View File

@ -40,7 +40,7 @@ extension PureXML.Schema.ComplexValidator {
counts[position] += 1
}
for (index, member) in group.particles.enumerated() where counts[index] < member.minOccurs {
if case let .element(name, _, _, _, _) = member.term {
if case let .element(name, _, _, _, _, _) = member.term {
errors.append(XSDFailure(reason: "element '\(name.localName)' is required but missing", at: path))
}
}
@ -48,7 +48,7 @@ extension PureXML.Schema.ComplexValidator {
static func memberMatches(_ term: PureXML.Schema.Term, _ name: PureXML.Model.QualifiedName) -> Bool {
switch term {
case let .element(declared, _, _, _, _): declared.localName == name.localName && declared.namespaceURI == name.namespaceURI
case let .element(declared, _, _, _, _, _): declared.localName == name.localName && declared.namespaceURI == name.namespaceURI
case let .wildcard(wildcard): wildcard.admits(name)
case .group: false
}
@ -77,7 +77,7 @@ extension PureXML.Schema.ComplexValidator {
static func matchedParticle(of term: PureXML.Schema.Term) -> PureXML.Schema.MatchedParticle? {
switch term {
case let .element(_, type, _, valueConstraint, _): .element(type: type, valueConstraint: valueConstraint)
case let .element(_, type, _, valueConstraint, _, _): .element(type: type, valueConstraint: valueConstraint)
case let .wildcard(wildcard): .wildcard(wildcard)
case .group: nil
}

View File

@ -34,7 +34,7 @@ extension PureXML.Schema.ComplexValidator {
fileprivate static func label(of term: XSDTerm) -> XSDTermLabel {
switch term {
case let .element(name, _, _, _, _): .name(name)
case let .element(name, _, _, _, _, _): .name(name)
case let .wildcard(wildcard): .wildcard(wildcard)
case .group: .wildcard(XSDWildcard())
}
@ -64,7 +64,7 @@ extension PureXML.Schema.ComplexValidator {
fileprivate static func collectTypes(_ term: XSDTerm, into result: inout [String: XSDElementType]) {
switch term {
case let .element(name, type, _, _, _):
case let .element(name, type, _, _, _, _):
if let type { result[key(name)] = type }
case let .group(group):
for member in group.particles {

View File

@ -247,7 +247,7 @@ extension PureXML.Schema {
private mutating func term(_ term: PureXML.Schema.Term) -> (start: Int, accept: Int) {
switch term {
case let .element(name, type, _, constraint, _):
case let .element(name, type, _, constraint, _, _):
let fragment = labeled(.name(name))
states[fragment.start].elementType = type
states[fragment.start].valueConstraint = constraint

View File

@ -66,17 +66,19 @@ extension PureXML.Schema {
return emptiable(base)
}
switch (restricted.term, base.term) {
case let (.element(restrictedName, _, restrictedTypeName, restrictedValue, restrictedBlock), .element(baseName, baseType, baseTypeName, baseValue, baseBlock)):
case let (.element(rName, _, rTypeName, rValue, rBlock, rNil), .element(bName, bType, bTypeName, bValue, bBlock, bNil)):
// 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), and if
// the base is fixed the restriction must be fixed to the same value.
// restriction's {disallowed substitutions} must be a superset of the
// base's (it may block more, never fewer); if the base is fixed the
// restriction must be fixed to the same value; and it may not be
// nillable unless the base element is.
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)):
&& sameName(rName, bName)
&& rBlock.isSuperset(of: bBlock)
&& (!rNil || bNil)
&& fixedValueOK(rValue, bValue, bType, types)
&& elementTypeRestrictionOK(rTypeName, bTypeName, types, derivation)
case let (.element(name, _, _, _, _, _), .wildcard(wildcard)):
return rangeSubsumed(restricted, base) && wildcard.admits(name)
case let (.wildcard(restrictedWildcard), .wildcard(baseWildcard)):
return rangeSubsumed(restricted, base) && narrows(restrictedWildcard, baseWildcard)
@ -91,7 +93,7 @@ extension PureXML.Schema {
// element restricting a once-or-more base group.
return rangeWithinWildcard(derivedMin: 1, derivedMax: 1, wildcardMin: base.minOccurs, wildcardMax: base.maxOccurs)
&& groupValid(Group(compositor: .sequence, particles: [restricted]), baseGroup, types, derivation)
case let (.group, .element(baseName, _, baseTypeName, _, _)):
case let (.group, .element(baseName, _, baseTypeName, _, _, _)):
// The mirror of RecurseAsIfGroup, by the same cardinality argument as
// group/wildcard: a base element accepts only its own name, its own
// occurrence count of times. The derived group is a valid restriction
@ -310,7 +312,7 @@ extension PureXML.Schema {
/// element by name, a nested wildcard by narrowing.
private static func leavesAdmitted(_ particle: Particle, by wildcard: Wildcard) -> Bool {
switch particle.term {
case let .element(name, _, _, _, _): wildcard.admits(name)
case let .element(name, _, _, _, _, _): wildcard.admits(name)
case let .wildcard(inner): narrows(inner, wildcard)
case let .group(group): group.particles.allSatisfy { leavesAdmitted($0, by: wildcard) }
}
@ -328,7 +330,7 @@ extension PureXML.Schema {
_ derivation: [String: TypeDerivation],
) -> Bool {
switch particle.term {
case let .element(name, _, typeName, _, _):
case let .element(name, _, typeName, _, _, _):
sameName(name, baseName) && elementTypeRestrictionOK(typeName, baseTypeName, types, derivation)
case .wildcard:
false

View File

@ -111,7 +111,7 @@ public extension PureXML.Schema {
private static func collect(_ term: Term, into result: inout [String: ElementType]) {
switch term {
case let .element(name, type, _, _, _):
case let .element(name, type, _, _, _, _):
if let type { result[name.description] = type }
case let .group(group):
for member in group.particles {

View File

@ -74,6 +74,7 @@ extension PureXML.Schema.XSDParser {
typeName: elementTypeName(node),
valueConstraint: valueConstraint(of: node),
block: methodSet(XSDNode.attribute(node, "block") ?? context.blockDefault),
nillable: ["true", "1"].contains(XSDNode.attribute(node, "nillable")),
),
)
}

View File

@ -99,8 +99,11 @@ struct XSTSSuiteTests {
/// 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.
/// Enforcing the NameAndTypeOK nillable clause (a restriction may not be nillable
/// unless the base element is) then caught two more invalid schemas:
/// invalid-schemas accepted 144 → 142, no other bucket moved.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 144
private let knownSchemaInvalidAccepted = 142
private let knownInstanceValidRejected = 3
private let knownInstanceInvalidAccepted = 132