PureXML/Sources/Schema/SchemaImportNamespace.swift
Mihaela Mihaljevic c418147d30 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).
2026-06-17 22:47:24 +02:00

33 lines
1.9 KiB
Swift

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
}
}