PureXML/Sources/Schema/XSDWildcardParser.swift
Mihaela Mihaljevic be8a39394d fix(schema): advance XSTS valid-instances-rejected to 4 (#146)
Consolidates the in-progress #146 work; valid-instances-rejected reaches 4
on the 2006-11-06 XSTS archive (buckets now 1/262/4/147).

Regex: match Unicode scalars rather than grapheme clusters; the empty
pattern matches only the empty string; pin the category repertoire to
Unicode 3.2, the version the corpus is consistent at, so code points added
in 3.2 (Tagalog, Cyrillic Komi) are word characters while later additions
(the Tamil digit zero) stay unassigned and match \D. New XSDCategory and a
3.2 assignment table derived from DerivedAge, replacing the old Unicode31
files.

Content model: assess each child against the particle the content automaton
matched, not a by-name lookup (#180). Two same-named particles keep their
own fixed values, and an element matching a skip wildcard is no longer
validated against its global declaration. Selection is made reproducible
when an ambiguous schema slips past the UPA check.

XSTS runner: compile and merge every schemaDocument in a schemaTest, not
just the first; this also surfaced one previously-masked invalid-instance
acceptance (recorded in the baselines).

Also folds in attribute-group anyAttribute inheritance, extension wildcard
union, namespace-qualified type tables for multi-document
xsi:schemaLocation, per-container elementFormDefault, strict and lax
attribute wildcard validation, built-in type resolution, and xs:redefine
simpleType overrides.
2026-06-16 22:22:05 +02:00

62 lines
2.9 KiB
Swift

extension PureXML.Schema.XSDParser {
/// The effective `xs:anyAttribute` wildcard declared under `node`, including
/// any `anyAttribute` on nested `attributeGroup` references.
static func attributeWildcard(
under node: XSDTree,
_ context: PureXML.Schema.XSDContext,
visited: Set<String> = [],
) -> PureXML.Schema.Wildcard? {
var combined: PureXML.Schema.Wildcard?
if let direct = PureXML.Schema.XSDNode.firstChild(node, named: "anyAttribute").map({ wildcard($0, context) }) {
combined = PureXML.Schema.Wildcard.union(combined, direct)
}
for child in PureXML.Schema.XSDNode.elementChildren(node) {
guard PureXML.Schema.XSDNode.localName(child) == "attributeGroup",
let ref = PureXML.Schema.XSDNode.attribute(child, "ref")
else { continue }
let name = PureXML.Schema.XSDNode.stripPrefix(ref)
if visited.contains(name) {
guard context.redefinedAttributeGroups.contains(name),
let base = context.baseAttributeGroups[name],
let found = attributeWildcard(under: base, context, visited: visited)
else { continue }
combined = PureXML.Schema.Wildcard.union(combined, found)
continue
}
guard let group = context.attributeGroups[name] else { continue }
let scoped = context.scoped(for: PureXML.Schema.XSDNode.schemaOwner(group))
if let found = attributeWildcard(under: group, scoped, visited: visited.union([name])) {
combined = PureXML.Schema.Wildcard.union(combined, found)
}
}
return combined
}
/// Parses a wildcard's `namespace` and `processContents` constraints.
static func wildcard(_ node: XSDTree, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.Wildcard {
let processContents: PureXML.Schema.ProcessContents = switch PureXML.Schema.XSDNode.attribute(node, "processContents") {
case "skip": .skip
case "lax": .lax
default: .strict
}
let namespace = wildcardNamespace(PureXML.Schema.XSDNode.attribute(node, "namespace"), context)
return PureXML.Schema.Wildcard(namespace: namespace, processContents: processContents, targetNamespace: context.targetNamespace)
}
private static func wildcardNamespace(_ value: String?, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.WildcardNamespace {
switch value {
case nil, "##any": return .any
case "##other": return .other
default:
let uris = (value ?? "").split(whereSeparator: \.isWhitespace).map { token -> String in
switch token {
case "##targetNamespace": context.targetNamespace ?? ""
case "##local": ""
default: String(token)
}
}
return .enumerated(Set(uris))
}
}
}