From fd97faf2916d5f8c2f3e46a4f7009b8fd532f4ca Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 17 Jun 2026 15:11:13 +0200 Subject: [PATCH] fix(schema): require complexContent extension mixed to agree with its base (#145) A complexContent extension that explicitly states mixed must match its base type's mixedness (cos-ct-extends.1.4.3.2.2.1): a mixed extension extends only a mixed base, an element-only extension only an element-only base. An extension that omits mixed inherits the base's setting and stays valid, so only an explicit conflict is rejected (ctZ012b, which inherits, is not flagged). XSTS invalid-schemas-accepted 197 to 194, no other bucket moved. --- CHANGELOG.md | 2 + Sources/Schema/SchemaCompileValidation.swift | 5 ++- Sources/Schema/SchemaExtensionAllGroup.swift | 42 ++++++++++++++++++++ Tests/ValidationConfigurationTests.swift | 2 +- Tests/XSTSSuiteTests.swift | 7 +++- 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebd5565..8bd0e62 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 explicitly states `mixed` must agree with its base type's mixedness (`cos-ct-extends.1.4.3.2.2.1`): a mixed extension may extend only a mixed base, and an element-only extension only an element-only base. An extension that omits `mixed` inherits the base's setting and stays valid, so only an explicit conflict is rejected. Caught three invalid schemas: invalid-schemas-accepted 197 → 194, no other bucket moved. + - XSTS invalid-schemas-accepted (#145): a `complexContent` or `simpleContent` must contain exactly one `restriction` or `extension`. An empty `` (or ``), which declares no derivation, is now rejected as a structural error. Caught three invalid schemas: invalid-schemas-accepted 200 → 197, no other bucket moved. - XSTS invalid-schemas-accepted (#145): a `pattern` facet whose `\p{...}` or `\P{...}` property name can be no XSD `charProp` (`IsCategory | IsBlock`) under any repertoire is now rejected as a schema-validity error: an empty name, the bare prefix `Is`, a non-block name carrying a non-letter (`\p{\L}`), or `Is` followed by a non-block character. A well-formed but unrecognised category or block name (such as an unknown `Is`-block) stays a lenient engine limitation rather than an error, so only the unambiguously malformed forms reject. Caught three invalid schemas: invalid-schemas-accepted 203 → 200, no other bucket moved. diff --git a/Sources/Schema/SchemaCompileValidation.swift b/Sources/Schema/SchemaCompileValidation.swift index 0202098..5f410dd 100644 --- a/Sources/Schema/SchemaCompileValidation.swift +++ b/Sources/Schema/SchemaCompileValidation.swift @@ -342,10 +342,11 @@ extension PureXML.Validation.SchemaCompile { /// cos-ct-extends.1.4.2.2: a complexContent extension adding element content /// must have a base with complex (or empty) content, not simpleContent. static var complexExtensionBaseValid: PureXML.Validation.Validation { - compileRule("A complexContent extension's base has complex content") { document in + compileRule("A complexContent extension is consistent with its base type") { document in guard let namedTypes = document.namedTypes else { return [] } return PureXML.Schema.SchemaLocatedFinding.unlocated( - PureXML.Schema.XSDParser.simpleContentExtensionBaseErrors(document.schema, document.context, namedTypes), + PureXML.Schema.XSDParser.simpleContentExtensionBaseErrors(document.schema, document.context, namedTypes) + + PureXML.Schema.XSDParser.extensionMixedAgreementErrors(document.schema, document.context, namedTypes), ) } } diff --git a/Sources/Schema/SchemaExtensionAllGroup.swift b/Sources/Schema/SchemaExtensionAllGroup.swift index 78b8d6e..66cbb6c 100644 --- a/Sources/Schema/SchemaExtensionAllGroup.swift +++ b/Sources/Schema/SchemaExtensionAllGroup.swift @@ -76,6 +76,48 @@ extension PureXML.Schema.XSDParser { return errors } + /// XSD 1.0 `cos-ct-extends.1.4.3.2.2.1`: a `complexContent` extension's mixedness + /// must match its base type's. The effective content joins the base content and + /// the extension's, so a mixed extension may extend only a mixed base and an + /// element-only extension only an element-only base. Checked for a self-contained + /// schema, and only when the base content is definitely element-only or mixed (an + /// empty or simple-content base is governed by other rules and is left alone). + static func extensionMixedAgreementErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext, _ types: [String: PureXML.Schema.ElementType]) -> [String] { + guard !skipsCrossDocumentRules(schema, compositionLoaded: context.compositionLoaded) else { return [] } + let bindings = context.namespaceBindings + var errors: [String] = [] + for content in descendants(schema, named: "complexContent") { + guard let ext = ExtAllNode.firstChild(content, named: "extension"), + let base = ExtAllNode.attribute(ext, "base"), + ExtAllNode.referenceNamespace(base, bindings) == context.targetNamespace, + case let .complex(complex)? = types[ExtAllNode.stripPrefix(base)] + else { continue } + let baseIsMixed: Bool + switch complex.content { + case .mixed: baseIsMixed = true + case .elementOnly: baseIsMixed = false + case .empty, .simpleContent: continue + } + // An extension that does not state `mixed` inherits the base's, so there + // is no conflict; only an EXPLICIT `mixed` that disagrees is invalid. + guard let derivedMixed = explicitMixed(content) else { continue } + if baseIsMixed != derivedMixed { + errors.append("a complexContent extension of '\(ExtAllNode.stripPrefix(base))' must have the same mixed setting as its base") + } + } + return errors + } + + /// The EXPLICIT `mixed` of a `complexContent` derivation: its own `mixed` + /// attribute when present, otherwise the enclosing `complexType`'s, or nil when + /// neither states it (an extension then inherits its base's mixedness). `true` + /// and `1` are mixed. + private static func explicitMixed(_ complexContent: XSDTree) -> Bool? { + if let own = ExtAllNode.attribute(complexContent, "mixed") { return own == "true" || own == "1" } + if let parent = complexContent.parent, let outer = ExtAllNode.attribute(parent, "mixed") { return outer == "true" || outer == "1" } + return nil + } + /// Whether a `complexContent` extension adds element content: an `element` or /// a wildcard `any` somewhere in its model group. An empty group (``) /// or an attribute-only extension adds no element content, so it keeps the diff --git a/Tests/ValidationConfigurationTests.swift b/Tests/ValidationConfigurationTests.swift index 5c2fc51..4d978ff 100644 --- a/Tests/ValidationConfigurationTests.swift +++ b/Tests/ValidationConfigurationTests.swift @@ -100,7 +100,7 @@ struct ValidationConfigurationTests { "ID-typed value constraints are valid", "Element value constraints are valid against their declared types", "Complex types extending xs:all groups satisfy XSD placement rules", - "A complexContent extension's base has complex content", + "A complexContent extension is consistent with its base type", "Attribute restrictions are faithful to their bases", "Simple types do not derive from complex types", "Simple-type varieties are declared consistently", diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 59bf6f4..a0cd9b3 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -129,8 +129,13 @@ struct XSTSSuiteTests { /// `restriction` or `extension` (an empty `` has none) then /// caught three more invalid schemas: invalid-schemas accepted 200 → 197, no /// other bucket moved. + /// Requiring a `complexContent` extension that explicitly states `mixed` to agree + /// with its base's mixedness (cos-ct-extends.1.4.3.2.2.1; an extension that omits + /// `mixed` inherits the base's, so only an explicit conflict is invalid) then + /// caught three more invalid schemas: invalid-schemas accepted 197 → 194, no + /// other bucket moved. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 197 + private let knownSchemaInvalidAccepted = 194 private let knownInstanceValidRejected = 3 private let knownInstanceInvalidAccepted = 132