diff --git a/CHANGELOG.md b/CHANGELOG.md index e9af6b2..76fecb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- An attribute declaration may no longer land in the XSI namespace (XSD 1.0 Schema + Component Constraint "xsi: Not Allowed", §3.2.6). A schema may target the XSI + namespace and declare unqualified attributes (they land in no namespace), but a + top-level attribute, or a local one made qualified by `form="qualified"` or + `attributeFormDefault="qualified"`, lands in XSI and is now rejected (corpus + attKb018a, attKa015) while unqualified ones stay valid (attKb018, attKc018). XSTS + invalid-schemas-accepted 39 -> 37, valid-schemas-rejected held at 0, no other + bucket moved. + - A prefixed reference (`type`/`base`/`itemType`/`ref`/`substitutionGroup`/`memberTypes`) whose namespace is never imported is now rejected on the cross-document skip path (XSD `src-resolve`). When a schema declares imports but loads no external document, diff --git a/Sources/Schema/SchemaCompileValidation.swift b/Sources/Schema/SchemaCompileValidation.swift index 404a995..2d96e41 100644 --- a/Sources/Schema/SchemaCompileValidation.swift +++ b/Sources/Schema/SchemaCompileValidation.swift @@ -245,7 +245,7 @@ extension PureXML.Validation { nonReference: [ AnyValidation(idAttributesValid), AnyValidation(schemaStructureValid), AnyValidation(componentNamesUnique), AnyValidation(simpleTypeFinalControlsValid), - AnyValidation(contentModelsDeterministic), + AnyValidation(contentModelsDeterministic), AnyValidation(attributeDeclarationsNotInXSI), AnyValidation(referencedSchemasResolveToSchemas), AnyValidation(redefinitionsDeriveFromThemselves), AnyValidation(redefineSelfReferencesAreWellFormed), AnyValidation(redefinedAttributeGroupsKeepRequired), ], @@ -341,6 +341,16 @@ extension PureXML.Validation { } extension PureXML.Validation.SchemaCompile { + /// xsi: Not Allowed (XSD 1.0 SCC §3.2.6): no attribute declaration lands in the + /// XSI namespace. See ``PureXML/Schema/XSDParser/xsiNamespaceAttributeErrors``. + static var attributeDeclarationsNotInXSI: PureXML.Validation.Validation { + compileRule("Attribute declarations are not in the XSI namespace") { document in + PureXML.Schema.SchemaLocatedFinding.unlocated( + PureXML.Schema.XSDParser.xsiNamespaceAttributeErrors(document.schema, document.context), + ) + } + } + /// au-props-correct: an attribute's type is a simple type, never complex. static var attributeTypesSimple: PureXML.Validation.Validation { compileRule("An attribute's type is a simple type") { document in diff --git a/Sources/Schema/SchemaXSIAttribute.swift b/Sources/Schema/SchemaXSIAttribute.swift new file mode 100644 index 0000000..76c082c --- /dev/null +++ b/Sources/Schema/SchemaXSIAttribute.swift @@ -0,0 +1,42 @@ +extension PureXML.Schema.XSDParser { + private static let xsiInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance" + + /// XSD 1.0 Schema Component Constraint "xsi: Not Allowed" (§3.2.6): an attribute + /// declaration's {target namespace} must not be the XSI namespace. A schema may + /// target XSI and declare attributes there as long as they stay unqualified, so + /// they land in no namespace (corpus attKb018, attKc018: valid). A top-level + /// attribute, or a local one made qualified by `form="qualified"` or + /// `attributeFormDefault="qualified"`, lands in the target namespace; when that is + /// XSI it is forbidden (corpus attKb018a: `attributeFormDefault="qualified"` puts + /// its attribute-group attributes into XSI). The check is scoped to schemas whose + /// target namespace is XSI, the only documents that can place an attribute there. + static func xsiNamespaceAttributeErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [String] { + guard context.targetNamespace == xsiInstanceNamespace else { return [] } + let attributeFormDefault = PureXML.Schema.XSDNode.attribute(schema, "attributeFormDefault") + var errors: [String] = [] + collectXSIAttributeErrors(schema, parentIsSchema: false, attributeFormDefault: attributeFormDefault, into: &errors) + return errors + } + + private static func collectXSIAttributeErrors( + _ node: XSDTree, + parentIsSchema: Bool, + attributeFormDefault: String?, + into errors: inout [String], + ) { + let local = PureXML.Schema.XSDNode.localName(node) + if local == "appinfo" || local == "documentation" { return } + let isSchema = node.name?.namespaceURI == xsdNamespace && local == "schema" + let isAttribute = node.name?.namespaceURI == xsdNamespace && local == "attribute" + if isAttribute, PureXML.Schema.XSDNode.attribute(node, "ref") == nil, let name = PureXML.Schema.XSDNode.attribute(node, "name") { + let form = PureXML.Schema.XSDNode.attribute(node, "form") + let qualified = parentIsSchema || form == "qualified" || (form == nil && attributeFormDefault == "qualified") + if qualified { + errors.append("attribute declaration '\(name)' is in the XSI namespace, which is not allowed (xsi: Not Allowed)") + } + } + for child in PureXML.Schema.XSDNode.elementChildren(node) { + collectXSIAttributeErrors(child, parentIsSchema: isSchema, attributeFormDefault: attributeFormDefault, into: &errors) + } + } +} diff --git a/Tests/SchemaXSIAttributeTests.swift b/Tests/SchemaXSIAttributeTests.swift new file mode 100644 index 0000000..90c8402 --- /dev/null +++ b/Tests/SchemaXSIAttributeTests.swift @@ -0,0 +1,94 @@ +import Testing +@testable import PureXML + +/// XSD 1.0 Schema Component Constraint "xsi: Not Allowed" (#183): an attribute +/// declaration's {target namespace} must not be the XSI namespace. A schema may +/// target XSI and declare unqualified attributes (they land in no namespace), but a +/// top-level or qualified attribute would land in XSI and is forbidden. Mirrors +/// corpus attKb018a (invalid) against attKb018 / attKc018 (valid). +@Suite("XSD xsi: Not Allowed for attribute declarations (#183)") +struct SchemaXSIAttributeTests { + private let xsi = "http://www.w3.org/2001/XMLSchema-instance" + + private func compiles(_ schema: String) -> Bool { + (try? PureXML.Schema.Document(schema)) != nil + } + + @Test("attKb018a: attributeFormDefault=qualified forces attributes into XSI: rejected") + func test_qualifiedAttributeIntoXSIRejected() { + let schema = """ + + + + + + + + + + + """ + #expect(!compiles(schema), "qualified attributes land in XSI; xsi: Not Allowed") + } + + @Test("attKb018: same schema with unqualified attributes (no-namespace) stays valid") + func test_unqualifiedAttributesValid() { + let schema = """ + + + + + + + + + + + """ + #expect(compiles(schema), "unqualified attributes land in no namespace, not XSI") + } + + @Test("attKc018: local unqualified attributes targeting XSI stay valid") + func test_localUnqualifiedAttributesValid() { + let schema = """ + + + + + + + + """ + #expect(compiles(schema), "local unqualified attributes are in no namespace") + } + + @Test("a single local attribute with form=qualified into XSI is rejected") + func test_localFormQualifiedIntoXSIRejected() { + let schema = """ + + + + + + + """ + #expect(!compiles(schema), "form=qualified puts ca1 into XSI; xsi: Not Allowed") + } + + @Test("the same qualified attribute targeting a non-XSI namespace is valid") + func test_qualifiedAttributeOutsideXSIValid() { + let schema = """ + + + + + + + """ + #expect(compiles(schema), "qualifying into a non-XSI namespace is fine") + } +} diff --git a/Tests/ValidationConfigurationTests.swift b/Tests/ValidationConfigurationTests.swift index 87177b2..63f2fcf 100644 --- a/Tests/ValidationConfigurationTests.swift +++ b/Tests/ValidationConfigurationTests.swift @@ -90,6 +90,7 @@ struct ValidationConfigurationTests { "Global schema component names are unique within their symbol spaces", "Simple-type final controls are declared consistently", "Schema content models are deterministic (UPA)", + "Attribute declarations are not in the XSI namespace", "A resolved schemaLocation reference is a valid schema", "A redefined type restricts or extends itself", "A redefined group or attribute group has a well-formed self-reference", diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index b14fb25..b4b40f9 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -146,9 +146,9 @@ struct XSTSSuiteTests { /// model-group occurrence, identity-constraint content, derivation base kind, /// attribute/simple-type rules, composition, simpleContent inline-type checks, /// complexContent mixedness, and substitution-group anySimpleType checks. - /// Substitution barriers and element-ref metadata moved 45 -> 43; src-resolve unresolvable foreign-ref (#185) 43 -> 39. + /// Substitution barriers and element-ref metadata 45 -> 43; src-resolve unresolvable foreign-ref (#185) 43 -> 39; xsi: Not Allowed attr decls (#183) 39 -> 37. private let knownSchemaValidRejected = 0 - private let knownSchemaInvalidAccepted = 39 + private let knownSchemaInvalidAccepted = 37 private let knownInstanceValidRejected = 0 /// Instance bucket (133 -> 26), per-step deltas in CHANGELOG.md: xsi:type must derive from the /// declared type (complex/atomic/list/union via the element-ref chain); anyType cannot stand in