From 0167c92790e23bfa86ef30172523ea2395a704cf Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Thu, 18 Jun 2026 16:30:33 +0200 Subject: [PATCH] fix(schema): a list/union substitution-group member derives only from anySimpleType; invalid-schemas-accepted 69 to 67 (#145) A substitution-group member whose type is a list or union derives only from xsd:anySimpleType (XSD Part 2), so it may validly affiliate only to a head typed anySimpleType, to the same type, or as a recorded restriction of the head's type (cos-equiv-derived-OK). substitutionTypeErrors skipped the derivation check entirely when either the member or the head type was a list/union, so a list/union-typed member affiliated to an unrelated head type (addB141: a union member of a list-typed head) was accepted. checkSingleElement now rejects a list/union member type that is not a recorded derivation of a non-ur head type, before the blanket list/union skip. A list/union member of an anySimpleType (or untyped, hence anyType) head, the same type, and a restriction of the head's list/union type are all left valid (isDerived covers the restriction chains via typeDerivesOrEqual). invalid-schemas-accepted 69 to 67, no false positive (valid-schemas-rejected and valid-instances-rejected held at 0). An adversarial review verified untyped and anySimpleType heads, multi-level restriction-of-list/union members, same-type and cross-namespace affiliations all still compile. --- CHANGELOG.md | 2 + Sources/Schema/SchemaSubstitutionType.swift | 7 ++++ Tests/SchemaSubstitutionListUnionTests.swift | 43 ++++++++++++++++++++ Tests/XSTSSuiteTests.swift | 2 +- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 Tests/SchemaSubstitutionListUnionTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index af25603..86d36e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- XSTS invalid-schemas-accepted (#145): a substitution-group member whose type is a list or union derives only from `anySimpleType`, so it may affiliate only to a head typed `anySimpleType` (or to the same type, or as a recorded restriction of the head's type). The substitution-type check skipped derivation entirely when either the member or head type was a list/union; a list/union-typed member affiliated to an unrelated head type (addB141: a union member of a list-typed head) is now rejected, while a list/union member of an `anySimpleType` head and a restriction of a list head remain valid. invalid-schemas-accepted 69 → 67, no false positive (valid-schemas-rejected and valid-instances-rejected held at 0), no other bucket moved. + - XSTS invalid-instances-accepted (#147): a `block` on a LOCAL element declaration is now keyed by the element's instance (qualified) name in the derivation backbone, so an xsi:type substitution blocked on an unqualified local element is barred. The namespaced backbone keyed every element by the target namespace, but a local element is in the target namespace only when qualified (its own `form`, else the schema's elementFormDefault); an unqualified local element's block went unfound (typeDef00801). invalid-instances-accepted 32 → 31, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved. - XSTS invalid-instances-accepted (#147): an instance `xsi:type` may not name an abstract type. An abstract type cannot be the type of an instance element (cvc-elt.4.3.2), so it is invalid as a substitution even when the declared type would otherwise admit it (typeDef01101); a concrete type is unaffected. This complements the existing check for an abstract DECLARED type with no `xsi:type`. invalid-instances-accepted 34 → 32, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved. diff --git a/Sources/Schema/SchemaSubstitutionType.swift b/Sources/Schema/SchemaSubstitutionType.swift index 2f5fbe6..60a5036 100644 --- a/Sources/Schema/SchemaSubstitutionType.swift +++ b/Sources/Schema/SchemaSubstitutionType.swift @@ -91,6 +91,13 @@ extension PureXML.Schema.XSDParser { memberType = headType isDerived = true } + // A member whose type is a list or union derives only from anySimpleType, so + // it may affiliate only to a head typed anySimpleType (or to the same type, or + // as a recorded restriction of the head's type, both of which `isDerived` + // covers). Affiliating it to any other head type is not a valid derivation. + if isListOrUnion(memberType, types), !isDerived, headType != "anySimpleType", headType != "anyType" { + return ["element '\(member)' may not be in the substitution group of '\(head)': its list or union type is not derived from '\(headType)'"] + } guard !isListOrUnion(memberType, types), !isListOrUnion(headType, types) else { return [] } if !isDerived { return ["element '\(member)' may not be in the substitution group of '\(head)': its type is not derived from '\(headType)'"] diff --git a/Tests/SchemaSubstitutionListUnionTests.swift b/Tests/SchemaSubstitutionListUnionTests.swift new file mode 100644 index 0000000..0ad10cf --- /dev/null +++ b/Tests/SchemaSubstitutionListUnionTests.swift @@ -0,0 +1,43 @@ +@testable import PureXML +import Testing + +/// A substitution-group member whose type is a list or union derives only from +/// `anySimpleType`, so it may affiliate only to a head typed `anySimpleType` (or +/// to the same type, or as a recorded restriction of the head's type). Affiliating +/// a list/union-typed member to an unrelated head type is an invalid schema +/// (W3C addB141 / test82919). +@Suite("substitution-group list/union member type") +struct SchemaSubstitutionListUnionTests { + @Test("a union member of a list-typed head is rejected at compile") + func test_unionMemberOfListHeadRejected() throws { + #expect(throws: (any Error).self) { + try PureXML.Schema.Document(""" + + + + + + + """) + } + } + + @Test("a list/union member of an anySimpleType head, and a restriction of a list head, compile") + func test_validListUnionAffiliationsAccepted() throws { + _ = try PureXML.Schema.Document(""" + + + + + + """) + _ = try PureXML.Schema.Document(""" + + + + + + + """) + } +} diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 49594ea..79036c0 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -148,7 +148,7 @@ struct XSTSSuiteTests { /// and the cross-document composition family (src-resolve, src-import.3, /// src-redefine.5/6.1/7.2.1, redefine cross-namespace, required schemaLocation). private let knownSchemaValidRejected = 0 - private let knownSchemaInvalidAccepted = 69 + private let knownSchemaInvalidAccepted = 67 private let knownInstanceValidRejected = 0 /// Instance bucket (133 -> 31), per-step deltas in CHANGELOG.md: xsi:type must derive from the /// declared type (complex, then atomic and list/union, resolved through the element-ref chain);