From a96e5ce3ac30d55b36032fb0e7f30a9abaf59c86 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 17 Jun 2026 13:23:26 +0200 Subject: [PATCH] fix(schema): reject extending a non-empty all-group base with element content (#145) Extending a complexType whose whole content is a non-empty xs:all group, via complexContent extension that adds element content, nests that all in the sequence joining base and extension content, which cos-all-limited forbids (an all group must be the whole content model). Such schemas were accepted. This is the symmetric companion to the existing rule (an all group in the extension over a base with content); both now live in one pass. An empty-all base contributes no content, and an attribute-only extension adds none, so both stay valid. XSTS invalid-schemas-accepted 233 to 232, no other bucket moved. --- CHANGELOG.md | 2 ++ Sources/Schema/SchemaExtensionAllGroup.swift | 31 +++++++++++++++++--- Tests/XSTSSuiteTests.swift | 6 +++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5f3d04..2d7520b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-s ### Fixed +- XSTS invalid-schemas-accepted (#145): a `complexContent` extension that adds element content may not extend a base whose whole content is a non-empty `all` group; the base's `all` would be nested in the sequence joining base and extension content (`cos-all-limited`). An empty-`all` base, or an attribute-only extension, stays valid. Caught one invalid schema: invalid-schemas-accepted 233 → 232, no other bucket moved. + - XSTS invalid-schemas-accepted (#145): a `complexContent` extension that adds element content (a non-empty model group) may not extend a base whose content is simple (`cos-ct-extends.1.4.2.2`); a simple-content type is extended through `simpleContent`. An attribute-only `complexContent` extension of a simpleContent base stays valid (the `complexContent`-around-`simpleContent` idiom). Caught two invalid schemas: invalid-schemas-accepted 235 → 233, no other bucket moved. - XSTS invalid-schemas-accepted (#145): two more schema-for-schemas structural rules. A named model group's compositor (`all`/`choice`/`sequence`) may not carry `minOccurs`/`maxOccurs` (those belong on a group reference), and an attribute in the XML Schema namespace (such as `xsd:type`) is rejected since the schema vocabulary defines no namespaced attributes (a foreign attribute in any other namespace stays the author's own). Caught nine invalid schemas: invalid-schemas-accepted 244 → 235, no other bucket moved. diff --git a/Sources/Schema/SchemaExtensionAllGroup.swift b/Sources/Schema/SchemaExtensionAllGroup.swift index 48a5b64..78b8d6e 100644 --- a/Sources/Schema/SchemaExtensionAllGroup.swift +++ b/Sources/Schema/SchemaExtensionAllGroup.swift @@ -18,17 +18,40 @@ extension PureXML.Schema.XSDParser { var errors: [String] = [] for content in descendants(schema, named: "complexContent") { guard let ext = ExtAllNode.firstChild(content, named: "extension"), - extensionHasAllGroup(ext), let base = ExtAllNode.attribute(ext, "base"), ExtAllNode.referenceNamespace(base, bindings) == context.targetNamespace, - case let .complex(complex)? = types[ExtAllNode.stripPrefix(base)], - baseHasElementContent(complex.content) + case let .complex(complex)? = types[ExtAllNode.stripPrefix(base)] else { continue } - errors.append("an 'all' group may not extend the type '\(ExtAllNode.stripPrefix(base))', which has its own content; an all group must be the whole content model") + let baseName = ExtAllNode.stripPrefix(base) + // The extension itself adds an `all` group over a base that already has + // element content: the all would be nested in the extension sequence. + if extensionHasAllGroup(ext), baseHasElementContent(complex.content) { + errors.append("an 'all' group may not extend the type '\(baseName)', which has its own content; an all group must be the whole content model") + continue + } + // The base's own content IS an `all` group and the extension adds + // element content: the base's all is then nested in the sequence that + // joins base and extension content, which is forbidden (cos-all-limited). + if contentIsAllGroup(complex.content), extensionAddsElementContent(ext) { + errors.append("the type '\(baseName)' has an 'all' group as its whole content and may not be extended with element content") + } } return errors } + /// Whether a content type's whole content model is a NON-EMPTY `all` group. An + /// empty `` contributes no content (the sibling rule treats extending + /// empty content as valid), so it is not flagged. + private static func contentIsAllGroup(_ content: PureXML.Schema.ContentType) -> Bool { + switch content { + case let .elementOnly(particle), let .mixed(particle): + guard case let .group(group) = particle.term, group.compositor == .all else { return false } + return !PureXML.Schema.ParticleRestriction.contentFree(particle) + case .empty, .simpleContent: + return false + } + } + /// XSD 1.0 `cos-ct-extends.1.4.2.2`: a `complexContent` extension that adds /// element content (a model group) produces an element-only/mixed content type, /// so its base must have complex or empty content. A `simpleContent` base has a diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 9e8fc90..fdbe55d 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -83,8 +83,12 @@ struct XSTSSuiteTests { /// two more invalid schemas; an attribute-only complexContent extension of a /// simpleContent base stays valid. Invalid-schemas accepted 235 → 233, no other /// bucket moved. + /// Rejecting a complexContent extension that adds element content on top of a + /// base whose whole content is an `all` group (the all would be nested in the + /// joining sequence; cos-all-limited) then caught one more invalid schema: + /// invalid-schemas accepted 233 → 232, no other bucket moved. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 233 + private let knownSchemaInvalidAccepted = 232 private let knownInstanceValidRejected = 3 private let knownInstanceInvalidAccepted = 132