From 872e6ea7d0eb191dc8b82ff2de6aa9a19420d7c7 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Thu, 18 Jun 2026 17:22:26 +0200 Subject: [PATCH] fix(schema): preserve list/union variety through a simpleContent derivation (#147) A complex type whose simpleContent derives from a list or union simple type now keeps that variety, so its element content is validated as a list or union rather than collapsing to an atomic value space. simpleContentType built the content type with only the resolved base and facets, dropping the variety, so a list-typed simpleContent accepted any whitespace-separated text and an invalid item (e.g. a non-int in a list of int) was not caught. A valid list/union value is unaffected; the change only tightens validation of content that was wrongly accepted. The W3C XSTS corpus does not exercise this case, so all four bucket counts are unchanged (vsr=0, isa=67, vir=0, ivi=31); the gap was surfaced by the libxml2 differential and is covered by a unit test. --- CHANGELOG.md | 2 ++ Sources/Schema/XSDParser.swift | 2 +- Tests/SchemaSimpleContentListTests.swift | 31 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Tests/SchemaSimpleContentListTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 86d36e7..6a9e883 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 +- A complex type whose `simpleContent` derives from a list or union simple type now keeps that variety, so its element content is validated as a list or union rather than collapsing to an atomic value space. Previously `simpleContentType` dropped the base's variety, so an invalid item in a list-typed simpleContent (e.g. a non-int in a list-of-int) was accepted. A valid list/union value is unaffected. (Under-rejection fix surfaced by the libxml2 differential; the W3C suite does not exercise it, so the conformance counts are unchanged.) + - 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. diff --git a/Sources/Schema/XSDParser.swift b/Sources/Schema/XSDParser.swift index 21d0cfe..5399026 100644 --- a/Sources/Schema/XSDParser.swift +++ b/Sources/Schema/XSDParser.swift @@ -377,7 +377,7 @@ extension PureXML.Schema.XSDParser { let baseType = simpleContentBaseType(baseName, uri: uri, context, visited: visited) var facets = baseType.facets XSDSimpleParser.applyFacets(inner, into: &facets) - return SimpleType(base: baseType.base, facets: facets) + return SimpleType(base: baseType.base, facets: facets, variety: baseType.variety, isBuiltinList: baseType.isBuiltinList) } /// The simple type a `simpleContent` derivation derives from: an XSD built-in, a diff --git a/Tests/SchemaSimpleContentListTests.swift b/Tests/SchemaSimpleContentListTests.swift new file mode 100644 index 0000000..ad36bff --- /dev/null +++ b/Tests/SchemaSimpleContentListTests.swift @@ -0,0 +1,31 @@ +@testable import PureXML +import Testing + +/// A complex type whose `simpleContent` derives from a list or union simple type +/// keeps that variety, so its element content is validated as a list/union rather +/// than collapsing to an atomic value space. A `simpleContent` extension of a +/// list-of-int validates its text as a list of ints: a non-int item is rejected. +@Suite("simpleContent list/union variety") +struct SchemaSimpleContentListTests { + private func schema() throws -> PureXML.Schema.Document { + try PureXML.Schema.Document(""" + + + + + + + + """) + } + + @Test("a valid int-list simpleContent is accepted") + func test_validListAccepted() throws { + #expect(try schema().validate(#"1 2 3"#).isEmpty) + } + + @Test("a non-int item in a list simpleContent is rejected") + func test_invalidListItemRejected() throws { + #expect(try !schema().validate(#"1 z 3"#).isEmpty) + } +}