The instance-validity derivation and substitution tables were keyed by bare
local name, so two same-local-name types or elements in different namespaces
collided. In multi-document validation this let one schema's block/derivation
facts shadow another's, and it blocked honouring blockDefault (whose re-land
widened that collision into an over-rejection).
Key the instance subsystem by namespaced identity {ns}local instead. The shared
DerivationTables keeps its bare-keyed fields for the schema-validity machinery
(Particle-Valid-Restriction and the consistency rules, which resolve QNames to
bare names and walk the same backbone) and gains parallel ns* fields built in
the same pass, resolving each container's components through its target
namespace and prefix bindings. Only the instance consumers
(blockedSubstitutionError, notDerivedSubstitutionError, abstractTypeError, the
substitution-group closure, and referenceParticle) read the ns* tables, via a
derivationKey(fromReference:) that reduces a type:{ns}local reference, a bare
name, or an element ref to a {ns}local backbone key. A key miss stays permissive
(no constraint, substitution allowed), so the re-keying cannot over-reject.
With the namespaced backbone in place, blockDefault is re-landed FP-free: a
schema's blockDefault supplies the {disallowed substitutions} of every element
and complex type without an explicit block (own block overrides; block="" clears;
intersected with the methods each component admits), so blockDefault="extension"
bars an extension substitution and "#all" bars all, without one namespace's
defaults shadowing another's.
The pure re-keying preserves single-document behaviour exactly (the corpus is
overwhelmingly single-namespace, where {ns}local and bare coincide): all four
XSTS buckets held at vsr=0 / isa=69 / vir=0 / ivi=89 after re-keying alone.
blockDefault then drove invalid-instances-accepted 89 to 81, no false positive
(valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket
moved. An adversarial review exercised same- and cross-namespace substitutions,
no-targetNamespace keys, simple-type restriction, transitive substitution groups,
and blockDefault override/over-block cases, and the prior multi-document
cross-namespace blockDefault over-rejection is now accepted while a same-namespace
blocked substitution is still rejected (precise, not merely permissive).
51 lines
2.5 KiB
Swift
51 lines
2.5 KiB
Swift
extension PureXML.Schema.XSDNode {
|
|
/// The transitive substitution-group membership across all definition
|
|
/// containers: each head element maps to every element that may substitute for
|
|
/// it, directly or through a chain of heads. Keyed by namespaced identity
|
|
/// (`{ns}local`), so a head and a member with the same local name in different
|
|
/// namespaces stay distinct. `namespaceMap` gives each container's resolved
|
|
/// target namespace by index.
|
|
static func substitutionMembers(
|
|
_ containers: [XSDTree],
|
|
namespaceMap: [Int: String?],
|
|
mainTargetNamespace: String?,
|
|
) -> [String: [String]] {
|
|
var direct: [String: [String]] = [:]
|
|
for index in containers.indices {
|
|
let container = containers[index]
|
|
let containerNamespace = namespaceMap[index] ?? mainTargetNamespace
|
|
for element in children(container, named: "element") {
|
|
guard let name = attribute(element, "name"), let head = attribute(element, "substitutionGroup") else {
|
|
continue
|
|
}
|
|
let memberKey = PureXML.Schema.XSDParser.derivationKey(name, in: containerNamespace)
|
|
let bindings = PureXML.Schema.XSDParser.namespaceBindingsInScope(of: element, defaultBindings: [:])
|
|
let headNamespace = prefix(head) != nil ? referenceNamespace(head, bindings) : (bindings[""] ?? containerNamespace)
|
|
let headKey = PureXML.Schema.XSDParser.derivationKey(stripPrefix(head), in: headNamespace)
|
|
direct[headKey, default: []].append(memberKey)
|
|
}
|
|
}
|
|
var closure: [String: [String]] = [:]
|
|
for head in direct.keys {
|
|
// Breadth-first in document order: a `popLast()` stack would reverse the
|
|
// members, but the substitution-group expansion must stay in declaration
|
|
// order. The order-preserving RecurseLax restriction check relies on it
|
|
// (a base `choice(head, m1, m2)` must list `m1` before `m2` so a derived
|
|
// `choice(m1, m2)` maps in order).
|
|
var members: [String] = []
|
|
var queue = direct[head] ?? []
|
|
var seen: Set<String> = []
|
|
var index = 0
|
|
while index < queue.count {
|
|
let member = queue[index]
|
|
index += 1
|
|
guard seen.insert(member).inserted else { continue }
|
|
members.append(member)
|
|
queue += direct[member] ?? []
|
|
}
|
|
closure[head] = members
|
|
}
|
|
return closure
|
|
}
|
|
}
|