fix(schema): an import namespace must match the imported targetNamespace (src-import.3); invalid-schemas-accepted 87 to 84 (#161)

src-import.3.1/3.2: an xs:import's namespace attribute must equal the
targetNamespace of the schema document it resolves to (both present and equal,
or both absent). Importing a no-namespace document under a declared namespace,
or one whose target namespace differs, is now rejected.

importNamespaceErrors (new SchemaImportNamespace.swift) builds a resolved
schemaLocation -> targetNamespace map from the loaded containers and compares
each import node's namespace to it. Checked only when the schemaLocation
resolved to a loaded schema (an unresolved import is the processor's choice,
not an error) -- the false-positive boundary; imports keep their own target
namespace so no chameleon adjustment applies.

XSTS: invalid-schemas-accepted 87 to 84 (schF6/schG10/schG13),
valid-schemas-rejected held at 0, no other bucket moved. Differential:
agree-valid unchanged at 3161, PureXML-stricter unchanged at 87 over 9618
schemas. Second step of the cross-document composition campaign (#161).
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 22:47:24 +02:00
parent 7f2943cf5c
commit c418147d30
5 changed files with 63 additions and 3 deletions

View File

@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- XSTS invalid-schemas-accepted (#161): an `import`'s `namespace` attribute must equal the `targetNamespace` of the schema document it resolves to (`src-import.3.1`/`3.2`): both present and equal, or both absent. Importing a no-namespace document under a declared namespace, or a document whose target namespace differs from the declared one, is now rejected. Checked only when the `schemaLocation` resolves to a loaded schema (an unresolved import is the processor's choice and not an error); imports keep their own target namespace, so no chameleon adjustment applies. invalid-schemas-accepted 87 → 84, no false positive (valid-schemas-rejected held at 0; differential PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved.
- XSTS invalid-schemas-accepted (#145, #161): a resolved `include`/`import`/`redefine` `schemaLocation` must be a well-formed schema document (`src-resolve`). When the schema loader returns content for a referenced location, that content is now required to parse as an `xs:schema`; not-well-formed XML, or well-formed XML whose root is not `xs:schema`, makes the composing schema invalid. A location the loader does not resolve (returns nil) is not an error, since resolution is the processor's choice, so a self-contained schema is unaffected. invalid-schemas-accepted 95 → 87, no false positive (valid-schemas-rejected held at 0; differential PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved.
- XSTS invalid-schemas-accepted (#145): an attribute's type must be a simple type (`au-props-correct` / `src-attribute`). An `attribute` whose `type` resolves to a complex type, or to the complex ur-type `xs:anyType`, is rejected; a built-in or user simple type, an inline `simpleType`, or no type (`anySimpleType`) stays valid. A locally-declared type takes precedence over the built-in reading. invalid-schemas-accepted 96 → 95, no false positive (valid-schemas-rejected held at 0; differential PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved.

View File

@ -0,0 +1,32 @@
private typealias ImportNode = PureXML.Schema.XSDNode
extension PureXML.Schema.XSDParser {
/// XSD 1.0 `src-import.3.1`/`3.2`: an `import`'s `namespace` attribute must equal
/// the `targetNamespace` of the schema it resolves to (both present and equal, or
/// both absent). Runs only past the cross-document skip guard, so the imported
/// schema is loaded. Checked only when the `schemaLocation` resolved to a loaded
/// container (an unresolved import is the processor's choice and not an error),
/// which is the false-positive boundary. Imports keep their own target namespace
/// (no chameleon), so the imported document's literal `targetNamespace` is used.
static func importNamespaceErrors(_ context: PureXML.Schema.XSDContext, _ containers: [XSDTree]) -> [String] {
var locationTargetNS: [String: String?] = [:]
for container in containers where ImportNode.localName(container) == "schema" {
guard let location = context.containerLocations[ObjectIdentifier(container)] ?? nil else { continue }
locationTargetNS[location] = ImportNode.attribute(container, "targetNamespace")
}
var errors: [String] = []
for container in containers {
for node in descendants(container, named: "import") where node.name?.namespaceURI == xsdNamespace {
guard let location = ImportNode.attribute(node, "schemaLocation"),
locationTargetNS.keys.contains(location)
else { continue }
let imported = locationTargetNS[location] ?? nil
let declared = ImportNode.attribute(node, "namespace")
if declared != imported {
errors.append("import of '\(location)' declares namespace '\(declared ?? "(none)")', not the imported target namespace '\(imported ?? "(none)")'")
}
}
}
return errors
}
}

View File

@ -99,6 +99,7 @@ extension PureXML.Schema.XSDParser {
collectReferenceErrors(source, in: resolutionContext, inheritedBindings: sourceBindings, into: &errors)
}
return xsdErrors + simpleContentErrors + errors + redefineComponentExistenceErrors(containers)
+ importNamespaceErrors(context, containers)
}
/// XSD 1.0 `src-redefine.6`/`7.2.1`/`7.2.2`: each component a `redefine` names

View File

@ -47,6 +47,29 @@ struct SchemaIncludeTests {
#expect((try? PureXML.Schema.Document(main, schemaLoader: { _ in nil })) != nil)
}
/// src-import.3.1/3.2: an import's namespace attribute must equal the imported
/// schema's targetNamespace (both equal, or both absent). Checked only when the
/// schemaLocation resolves to a loaded schema.
@Test("an import's namespace must match the imported targetNamespace")
func test_importNamespaceMustMatchImportedTarget() {
let mainNS = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:a" xmlns:b="urn:b">
<xs:import namespace="urn:b" schemaLocation="b.xsd"/>
</xs:schema>
"""
// Imported doc's targetNamespace does NOT match the declared import namespace.
let mismatched = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"urn:WRONG\"/>"
#expect((try? PureXML.Schema.Document(mainNS, schemaLoader: { $0 == "b.xsd" ? mismatched : nil })) == nil)
// Imported doc has NO targetNamespace but the import declares one.
let noTarget = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"/>"
#expect((try? PureXML.Schema.Document(mainNS, schemaLoader: { $0 == "b.xsd" ? noTarget : nil })) == nil)
// Matching targetNamespace compiles.
let matched = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"urn:b\"/>"
#expect((try? PureXML.Schema.Document(mainNS, schemaLoader: { $0 == "b.xsd" ? matched : nil })) != nil)
// An unresolved import (loader returns nil) is not an error.
#expect((try? PureXML.Schema.Document(mainNS, schemaLoader: { _ in nil })) != nil)
}
@Test("xs:include with a mismatched targetNamespace is rejected")
func test_invalidIncludeDifferentTargetNamespace() {
let included = """

View File

@ -146,10 +146,12 @@ struct XSTSSuiteTests {
/// 130->110; identity-constraint content model 110->104; derivation base-type kind
/// (src-ct.1/.2, local type beats the built-in reading) 104->99; element value
/// constraint needs simple/mixed content (cos-valid-default) 99->96; an attribute's
/// type is a simple type (au-props-correct) 96->95; and a resolved include/import/
/// redefine schemaLocation must be a well-formed schema (src-resolve) 95->87.
/// type is a simple type (au-props-correct) 96->95; a resolved include/import/
/// redefine schemaLocation must be a well-formed schema (src-resolve) 95->87; and
/// an import's namespace must equal the imported targetNamespace (src-import.3)
/// 87->84.
private let knownSchemaValidRejected = 0
private let knownSchemaInvalidAccepted = 87
private let knownSchemaInvalidAccepted = 84
private let knownInstanceValidRejected = 0
private let knownInstanceInvalidAccepted = 133