diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a45025..acba5a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- A content-bearing derived group is no longer accepted as a restriction of a base single element. XSD 1.0 §3.9.6 "Particle Valid (Restriction)" defines a case for an element restricting a base group (RecurseAsIfGroup) but NONE for the reverse, so a derived `sequence`/`choice`/`all` that can contribute a child never validly restricts a single base element, regardless of how its leaves are named or counted. The oracle had a deliberate non-spec "mirror" extension that bounded such a derived group by the element's effective occurrence range and accepted it; that masked a real violation inside a `choice`-to-`choice` restriction, where a derived `sequence` branch was allowed to map onto a base element branch it cannot legally restrict. The case now returns invalid (a content-FREE derived group is still a valid restriction of any emptiable base, handled separately). Catches particlesHb011. XSTS invalid-schemas-accepted 17 → 16, valid-schemas-rejected held at 0. + - A defaulted or fixed `xs:IDREF`/`xs:IDREFS` attribute now participates in document-scoped ID/IDREF resolution (cvc-id). When the instance omits the attribute its `default`/`fixed` value applies, so a defaulted IDREF must still resolve to a matching `xs:ID`; the reference was recorded only for attributes physically present, so a dangling defaulted IDREF was wrongly accepted. The supplied value is now recorded for ID/IDREF checking exactly as a present value would be (shared by the tree and streaming validators, which both call `validateAttributes`). A required absent attribute is still reported as missing, and a defaulted IDREF that does resolve to an ID stays valid. Catches idZ012. XSTS invalid-instances-accepted 20 → 19, valid-instances-rejected held at 0. - An `xs:all` group with `minOccurs="0"` now requires its members once the group is present in the instance. The group's `minOccurs="0"` makes the GROUP optional (it occurs zero or one times), not each member optional: an absent group (no matching children) is valid, but a present group (any matching child) must still satisfy every member's own `minOccurs`. The parser previously forced each member's `minOccurs` to 0 when the all-group was optional, so a non-empty but incomplete optional all-group (a required member missing) was wrongly accepted. Members now keep their declared occurrence, and the absent-group case is handled at validation time by skipping the required-member check only when the all-group contributes no children at all; a required all-group (`minOccurs="1"`) is unchanged. The fix is applied identically on the tree and streaming paths (both call `allStructureErrors`). Because members now keep their true `minOccurs`, the particle-restriction check (cos-group-restrict) also sees them correctly: a complexContent restriction that relaxes a required member of an optional all-group base is now rejected too, so this catches mgZ001 (instance) and particlesK006 (schema). XSTS invalid-instances-accepted 21 → 20 and invalid-schemas-accepted 18 → 17, valid-instances-rejected and valid-schemas-rejected both held at 0. - A nilled element (an `xsi:nil="true"` on a nillable declaration) whose declaration carries a fixed `{value constraint}` is now rejected during instance validation (cvc-elt.3.2.2). A nillable element MAY declare a fixed value (the schema is valid); the conflict arises only when an instance actually nils such an element, since a nilled element has no value and so cannot take its fixed one. The check rides the existing `nilErrors` chokepoint (shared by the tree and streaming validators), using the matched particle's constraint with the by-local-name element-constraint map as a fallback, so it is enforced identically on both paths; a nillable element that supplies its fixed value, and a nilled element with no fixed value constraint, are both unaffected. Catches addB065. XSTS invalid-instances-accepted 22 → 21, valid-instances-rejected held at 0. diff --git a/Sources/Schema/ParticleRestriction.swift b/Sources/Schema/ParticleRestriction.swift index 973e5ba..1e71b67 100644 --- a/Sources/Schema/ParticleRestriction.swift +++ b/Sources/Schema/ParticleRestriction.swift @@ -84,23 +84,19 @@ extension PureXML.Schema { return rangeSubsumed(restricted, base) && narrows(restrictedWildcard, baseWildcard) case let (.element, .group(baseGroup)): return elementRestrictsGroup(restricted, base, baseGroup, types, derivation) - 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 - // iff every leaf it can contain is that element (same name, and a type - // that derives from the base's) AND the group's effective total - // occurrence range is within the base element's. Comparing the derived - // group's *own* occurrence to the element's would be wrong (a derived - // `sequence(a,a)` emits two a's at occurrence {1,1}); the effective - // range is the count that must be bounded. - return leavesRestrict(restricted, toElement: baseName, baseTypeName, types, derivation) - && rangeWithinWildcard( - derivedMin: restricted.effectiveOccurrenceMin(), - derivedMax: restricted.effectiveOccurrenceMax(), - wildcardMin: base.minOccurs, - wildcardMax: base.maxOccurs, - ) + case (.group, .element): + // XSD 1.0 §3.9.6 "Particle Valid (Restriction)" defines NO case for a + // derived group against a base element: the only element/group pairing + // is the reverse (an element restricting a base group, via + // RecurseAsIfGroup). A derived group that can contribute content + // therefore never validly restricts a single base element, regardless + // of how its leaves are named or counted. (A CONTENT-FREE derived group + // was already accepted above as a valid restriction of any emptiable + // base; only a content-bearing one reaches here.) This is the spec rule + // the corpus enforces -- e.g. a `choice(seq(e1), e2, e3)` restricting a + // `choice(e1, e2, e3)` is invalid because the derived `seq(e1)` branch + // has no base element it can validly restrict (particlesHb011). + return false case let (.group(restrictedGroup), .group(baseGroup)) where restrictedGroup.compositor == .sequence && baseGroup.compositor == .choice: // MapAndSum (Sequence:Choice, XSD 1.0 §3.9.6): each derived particle is // a valid restriction of some base branch, AND the derived sequence's @@ -344,27 +340,6 @@ extension PureXML.Schema { } } - /// Whether every leaf a particle can contain is the named base element (same - /// name, and a type that derives from the base element's). A wildcard leaf is - /// rejected: it can match names other than the base element's, so it is not a - /// subset. Used when the base content model is a single element. - private static func leavesRestrict( - _ particle: Particle, - toElement baseName: PureXML.Model.QualifiedName, - _ baseTypeName: String?, - _ types: [String: ElementType], - _ derivation: [String: TypeDerivation], - ) -> Bool { - switch particle.term { - case let .element(name, _, typeName, _, _, _): - sameName(name, baseName) && elementTypeRestrictionOK(typeName, baseTypeName, types, derivation) - case .wildcard: - false - case let .group(group): - group.particles.allSatisfy { leavesRestrict($0, toElement: baseName, baseTypeName, types, derivation) } - } - } - /// Whether a derived effective total range fits within a base wildcard's /// occurrence range: at least the wildcard's minimum, at most its maximum. private static func rangeWithinWildcard(derivedMin: Int, derivedMax: Int?, wildcardMin: Int, wildcardMax: Int?) -> Bool { diff --git a/Tests/XSDParticleRestrictionTests.swift b/Tests/XSDParticleRestrictionTests.swift index ae51054..4001ae6 100644 --- a/Tests/XSDParticleRestrictionTests.swift +++ b/Tests/XSDParticleRestrictionTests.swift @@ -178,28 +178,30 @@ struct XSDParticleRestrictionTests { )?.contains("not a valid restriction") == true) } - @Test("A group restricting a base that is a single element is bounded by that element's range") + @Test("A content-bearing derived group never restricts a base single element") func test_groupRestrictsSingleElement() { // mgE014: base sequence(title?) normalizes to the bare element title{0,1}; - // the derived sequence(title{0,0}) normalizes to the empty group, which - // accepts only the empty sequence, valid against an emptiable element. + // the derived sequence(title{0,0}) normalizes to the empty (content-free) + // group, which accepts only the empty sequence, valid against an emptiable + // element. This is the content-free path, not a derived-group/base-element rule. #expect(compiles( base: "", restriction: "", )) - // A derived choice repeated within the element's range is valid (the base - // element, normalized from a single-member sequence, is compositor-agnostic). - #expect(compiles( + // XSD 1.0 §3.9.6 defines NO restriction rule for a derived group against a base + // element (only the reverse, an element against a base group). A content-bearing + // derived group restricting a single base element is therefore invalid, even + // when its leaves share the element's name and its effective count fits. + #expect(restrictionError( base: "", restriction: "", - )) - // A derived group whose effective count exceeds the base element's maxOccurs is - // rejected: sequence(a){1,10} emits up to ten a's, the base allows three. + )?.contains("not a valid restriction") == true) + // The same holds when the derived group's effective count exceeds the base + // element's maxOccurs, and when a leaf is differently named. #expect(restrictionError( base: "", restriction: "", )?.contains("not a valid restriction") == true) - // A derived leaf with a different name than the base element is rejected. #expect(restrictionError( base: "", restriction: "", diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 40fbd34..a5d54f8 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -31,7 +31,7 @@ struct XSTSSuiteTests { /// history of how each bucket reached its current value (every rule and its exact /// delta) lives in CHANGELOG.md and docs/xsts-deviations.md, not inline here. private let knownSchemaValidRejected = 0 - private let knownSchemaInvalidAccepted = 17 + private let knownSchemaInvalidAccepted = 16 private let knownInstanceValidRejected = 0 /// Instance bucket (133 -> 22), per-step deltas in CHANGELOG.md: xsi:type must derive from the declared /// type; anyType cannot stand in for anySimpleType; an untyped substitutionGroup member inherits its head's