fix(schema): resolve element-ref namespaces by prefix binding, not target namespace
An `<xs:element ref="p:foo">` had its prefix stripped and the element assumed to be in the schema's target namespace. That mis-qualified every reference to an imported element: a restriction of an `##other` wildcard by such a reference was rejected (the element looked like the target namespace, which `##other` forbids), and the same mis-qualification rejected valid instances whose elements were correctly in the imported namespace. The reference's QName is now resolved by standard XML namespace rules against the schema root's xmlns bindings: a bound prefix gives its namespace (an imported element lands in the imported namespace), an unprefixed name the default namespace, or no namespace when none is declared. Substitution-group members stay in the target namespace (this schema's own globals). The content model is shared between schema-restriction checks and instance validation, so both improve. XSTS: valid schemas rejected 32 to 4, invalid schemas accepted 526 to 520, valid instances rejected 214 to 180, invalid instances accepted held at 160. Disclosed limitations: a prefix re-bound on an inner element is not honored, cross-namespace substitution-group members are not modeled, and a dangling reference is not yet flagged.
This commit is contained in:
parent
aba91a168f
commit
828ed195f0
@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- An `<xs:element ref="p:foo">` now resolves to the namespace its prefix is bound to, not the schema's target namespace (#164). The reference's QName is resolved by standard XML namespace rules against the schema root's `xmlns:` bindings: a bound prefix gives its namespace (so a reference to an imported element lands in the imported namespace), an unprefixed name resolves through the default namespace, and a name with no applicable binding is in no namespace. Previously the prefix was stripped and the element was assumed to be in the target namespace, which mis-qualified every reference to an imported element: a restriction of an `##other` wildcard by such a reference was wrongly rejected (the element looked like it was in the target namespace, which `##other` forbids), and the same mis-qualification rejected valid instance documents whose elements were correctly in the imported namespace. Because the content model is shared between schema-restriction checks and instance validation, the fix improves both. XSTS: valid schemas rejected 32 to 4, invalid schemas accepted 526 to 520, valid instances rejected 214 to 180, invalid instances accepted held at 160. Disclosed limitations, unchanged by this fix: a prefix re-bound on an inner element (rather than the schema root) is not honored, substitution-group members are still resolved to the target namespace (cross-namespace substitution is not modeled), and a dangling reference is not yet flagged as a schema error.
|
||||
- Particle Valid (Restriction) now derives its pairwise check over the normalized content model, and applies occurrence subsumption only where the two particles' own ranges are comparable (#163). The W3C restriction algorithm is defined over a content model with pointless particles eliminated, so both the derived and base particles are first normalized (`maxOccurs="0"` members dropped, a `{1,1}` single-member group unwrapped to its member, a nested same-compositor `{1,1}` group spliced into its parent), all language-preserving. The outer occurrence-range check (`rangeSubsumed`) was being applied to every pairing, including the cross-kind ones where it compares mismatched ranges: a derived element's range against a base group's, or a derived group's against a base wildcard's. It is now applied only to the same-kind pairings (element/element, wildcard/wildcard, group/group) and element/wildcard; for element/group (RecurseAsIfGroup), group/element, and group/wildcard the occurrence is matched inside the case, against a member or as an effective total range. This unblocks a single-member derived sequence narrowing an occurrence (`sequence(e1{2,3})` restricting `sequence(e1{1,3}, ...)`, the `e1{2,3}` no longer compared to the base sequence's own `{1,1}`). A group restricting a wildcard (NSRecurseCheckCardinality) is now valid when every leaf it can contain is admitted by the wildcard and the group's effective total occurrence range lies within the wildcard's; a group restricting a single element is bounded the same way, by that element's name, type derivation, and occurrence range. XSTS: valid schemas rejected 45 to 32 (13 correct schemas no longer wrongly rejected, the `particlesHa` family of element-restricts-wildcard cardinality among them), invalid schemas accepted 528 to 526 (the cardinality bound also catches two over-wide restrictions), no other bucket raised.
|
||||
- Particle Valid (Restriction) no longer over-rejects valid restrictions (#148). Three corrections, all language-preserving: a particle that can never occur (`maxOccurs="0"`, a "pointless particle") is removed from both the derived and base content before mapping, so it no longer consumes a base particle and starves the ones after it (e.g. `(e1 maxOccurs="0", e2)` restricting `(any)` is now valid); a content-free restriction (an empty `<sequence/>`, or a group whose members all cannot occur) is a valid restriction of an EMPTY base, since both accept only the empty sequence; and a derived group that reduces to empty after that removal is a valid restriction of its base only when the base is itself emptiable (by structure or its own occurrence), closing a vacuous-acceptance gap the removal would otherwise open. XSTS: valid schemas rejected 72 to 45 (27 correct schemas no longer wrongly rejected), with one schema (`addB32`) moving to invalid-accepted because its only invalidity is a malformed imported document PureXML does not yet load (tracked by #161), not the restriction.
|
||||
- A `simpleType` must contain exactly one of `restriction`, `list`, or `union` (#158). Per XSD 1.0 Datatypes its content is `(annotation?, (restriction | list | union))`, but the membership table admitted each name without the cardinality, so an empty `simpleType` or one combining two derivations (two `union`s, a `union` and a `list`, two `restriction`s) was accepted (the stB family). The exactly-one rule is now enforced. XSTS: invalid schemas accepted 537 to 527, no other bucket raised (valid schemas rejected held at 72).
|
||||
|
||||
@ -29,6 +29,36 @@ extension PureXML.Schema {
|
||||
qualified.split(separator: ":").last.map(String.init) ?? qualified
|
||||
}
|
||||
|
||||
/// The prefix part of a QName (`p` in `p:local`), or nil when unprefixed.
|
||||
static func prefix(_ qualified: String) -> String? {
|
||||
let parts = qualified.split(separator: ":", maxSplits: 1)
|
||||
return parts.count == 2 ? String(parts[0]) : nil
|
||||
}
|
||||
|
||||
/// The namespace prefix bindings declared on a schema-root element:
|
||||
/// `xmlns:p="..."` keyed by `p`, the default `xmlns="..."` under the empty key.
|
||||
static func namespaceBindings(of schema: XSDTree) -> [String: String] {
|
||||
var bindings: [String: String] = [:]
|
||||
for attribute in schema.attributes {
|
||||
if attribute.name.prefix == "xmlns" {
|
||||
bindings[attribute.name.localName] = attribute.value
|
||||
} else if attribute.name.prefix == nil, attribute.name.localName == "xmlns" {
|
||||
bindings[""] = attribute.value
|
||||
}
|
||||
}
|
||||
return bindings
|
||||
}
|
||||
|
||||
/// The namespace URI a reference QName resolves to, given the schema's prefix
|
||||
/// bindings: a prefixed name uses its prefix's binding, an unprefixed name the
|
||||
/// default-namespace binding (nil when none is declared).
|
||||
static func referenceNamespace(_ qualified: String, _ bindings: [String: String]) -> String? {
|
||||
if let prefix = prefix(qualified) {
|
||||
return bindings[prefix]
|
||||
}
|
||||
return bindings[""]
|
||||
}
|
||||
|
||||
static func occurrence(_ node: XSDTree) -> (min: Int, max: Int?) {
|
||||
let minimum = attribute(node, "minOccurs").flatMap(Int.init) ?? 1
|
||||
let maximumText = attribute(node, "maxOccurs")
|
||||
@ -167,6 +197,11 @@ extension PureXML.Schema {
|
||||
/// in wildcard constraints and qualifying global and qualified-form local
|
||||
/// declarations.
|
||||
var targetNamespace: String?
|
||||
/// Namespace prefix bindings declared on the schema root (`xmlns:p="..."`,
|
||||
/// the default namespace under the empty key), so an `xs:element ref="p:foo"`
|
||||
/// resolves to the namespace `p` is bound to rather than assuming the target
|
||||
/// namespace. An imported element reference is in the imported namespace.
|
||||
var namespaceBindings: [String: String] = [:]
|
||||
/// Whether `elementFormDefault="qualified"`: local element declarations are
|
||||
/// in the target namespace unless their own `form` overrides it.
|
||||
var elementFormQualified: Bool = false
|
||||
|
||||
@ -31,6 +31,7 @@ extension PureXML.Schema {
|
||||
)
|
||||
context.elementFormQualified = XSDNode.attribute(schema, "elementFormDefault") == "qualified"
|
||||
context.attributeFormQualified = XSDNode.attribute(schema, "attributeFormDefault") == "qualified"
|
||||
context.namespaceBindings = XSDNode.namespaceBindings(of: schema)
|
||||
context.complexTypeNodes = indexByName(allChildren(containers, named: "complexType"))
|
||||
context.globalAttributes = indexByName(allChildren(containers, named: "attribute"))
|
||||
let consistencyErrors = idAttributeErrors(schema) + structureErrors(schema)
|
||||
@ -275,19 +276,7 @@ extension PureXML.Schema {
|
||||
|
||||
private static func elementParticle(_ node: XSDTree, _ minimum: Int, _ maximum: Int?, _ context: XSDContext) -> Particle {
|
||||
if let ref = XSDNode.attribute(node, "ref") {
|
||||
let name = XSDNode.stripPrefix(ref)
|
||||
// An abstract head may not appear itself, only its members; a
|
||||
// concrete head appears alongside them.
|
||||
let head = context.abstractElements.contains(name) ? [] : [name]
|
||||
let alternatives = head + (context.substitutions[name] ?? [])
|
||||
if alternatives.count == 1 {
|
||||
return Particle(minOccurs: minimum, maxOccurs: maximum, term: elementReferenceTerm(alternatives[0], context))
|
||||
}
|
||||
// The reference admits its substitution-group members, so expand it
|
||||
// to a choice over the head and every member, each carrying its own
|
||||
// declared type.
|
||||
let members = alternatives.map { Particle(term: elementReferenceTerm($0, context)) }
|
||||
return Particle(minOccurs: minimum, maxOccurs: maximum, term: .group(Group(compositor: .choice, particles: members)))
|
||||
return referenceParticle(ref, minimum, maximum, context)
|
||||
}
|
||||
let name = XSDNode.attribute(node, "name") ?? ""
|
||||
return Particle(
|
||||
@ -315,14 +304,34 @@ extension PureXML.Schema {
|
||||
}
|
||||
|
||||
extension PureXML.Schema.XSDParser {
|
||||
/// The qualified name of a reference to a global element: globals are always in
|
||||
/// the schema's target namespace.
|
||||
static func elementReferenceTerm(_ name: String, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.Term {
|
||||
/// The particle for an `<xs:element ref="...">`. An abstract head may not appear
|
||||
/// itself, only its substitution-group members; a concrete head appears alongside
|
||||
/// them, the reference expanding to a choice over the head and every member.
|
||||
static func referenceParticle(_ ref: String, _ minimum: Int, _ maximum: Int?, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.Particle {
|
||||
let name = PureXML.Schema.XSDNode.stripPrefix(ref)
|
||||
// The reference's QName is resolved by standard XML namespace rules (a bound
|
||||
// prefix gives its namespace, so an imported element lands in the imported
|
||||
// namespace; an unprefixed name the default namespace, or none), as the W3C
|
||||
// XSTS suite expects: a bare `ref="foo"` with no default namespace is in no
|
||||
// namespace, which `##other` admits; forcing it to the target over-rejects
|
||||
// those and masks a dangling reference. Members are this schema's globals.
|
||||
let refNamespace = PureXML.Schema.XSDNode.referenceNamespace(ref, context.namespaceBindings)
|
||||
let head: [(String, String?)] = context.abstractElements.contains(name) ? [] : [(name, refNamespace)]
|
||||
let alternatives = head + (context.substitutions[name] ?? []).map { ($0, context.targetNamespace) }
|
||||
if alternatives.count == 1 {
|
||||
return PureXML.Schema.Particle(minOccurs: minimum, maxOccurs: maximum, term: elementReferenceTerm(alternatives[0].0, alternatives[0].1))
|
||||
}
|
||||
let members = alternatives.map { PureXML.Schema.Particle(term: elementReferenceTerm($0.0, $0.1)) }
|
||||
return PureXML.Schema.Particle(minOccurs: minimum, maxOccurs: maximum, term: .group(.init(compositor: .choice, particles: members)))
|
||||
}
|
||||
|
||||
/// The term for a reference to a global element, in the resolved `namespace`.
|
||||
static func elementReferenceTerm(_ name: String, _ namespace: String?) -> PureXML.Schema.Term {
|
||||
// A reference's type is resolved indirectly through the element key, so its
|
||||
// derivation identity is not a plain type name; NameAndTypeOK over a ref
|
||||
// falls back to the structural check (typeName nil).
|
||||
.element(
|
||||
name: PureXML.Model.QualifiedName(localName: name, namespaceURI: context.targetNamespace),
|
||||
name: PureXML.Model.QualifiedName(localName: name, namespaceURI: namespace),
|
||||
type: .typeReference(elementKey(name)),
|
||||
typeName: nil,
|
||||
)
|
||||
|
||||
@ -302,3 +302,54 @@ struct XSDParticleRestrictionTests {
|
||||
#expect(doc("xs:string", base: "u", types: userTypes) != nil)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite("XSD element-reference namespace resolution")
|
||||
struct XSDElementRefNamespaceTests {
|
||||
@Test("An element reference resolves to its prefix's namespace, not the target namespace")
|
||||
func test_elementRefNamespaceResolution() {
|
||||
// particlesJj001: a ref to an imported element (imp -> http://importedXSD)
|
||||
// restricts a `##other` wildcard. `##other` admits any namespace but the
|
||||
// target, so an imported element is admitted and the restriction is valid;
|
||||
// resolving the ref into the target namespace would make `##other` reject it.
|
||||
let imported = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:x=\"http://xsdtesting\""
|
||||
+ " targetNamespace=\"http://xsdtesting\" xmlns:imp=\"http://importedXSD\">"
|
||||
+ "<xsd:import namespace=\"http://importedXSD\"/>"
|
||||
+ "<xsd:complexType name=\"B\"><xsd:sequence><xsd:any namespace=\"##other\" minOccurs=\"0\"/></xsd:sequence></xsd:complexType>"
|
||||
+ "<xsd:complexType name=\"R\"><xsd:complexContent><xsd:restriction base=\"x:B\">"
|
||||
+ "<xsd:sequence><xsd:element ref=\"imp:impElem1\" minOccurs=\"0\"/></xsd:sequence>"
|
||||
+ "</xsd:restriction></xsd:complexContent></xsd:complexType></xsd:schema>"
|
||||
#expect((try? PureXML.Schema.Document(imported)) != nil)
|
||||
// The same shape with a ref into the TARGET namespace (a declared global) is
|
||||
// invalid: `##other` forbids the target namespace, so the element is not
|
||||
// admitted. This proves the resolved namespace is actually checked.
|
||||
let targetNs = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:x=\"http://xsdtesting\""
|
||||
+ " targetNamespace=\"http://xsdtesting\" elementFormDefault=\"qualified\">"
|
||||
+ "<xsd:element name=\"tElem\" type=\"xsd:string\"/>"
|
||||
+ "<xsd:complexType name=\"B\"><xsd:sequence><xsd:any namespace=\"##other\" minOccurs=\"0\"/></xsd:sequence></xsd:complexType>"
|
||||
+ "<xsd:complexType name=\"R\"><xsd:complexContent><xsd:restriction base=\"x:B\">"
|
||||
+ "<xsd:sequence><xsd:element ref=\"x:tElem\" minOccurs=\"0\"/></xsd:sequence>"
|
||||
+ "</xsd:restriction></xsd:complexContent></xsd:complexType></xsd:schema>"
|
||||
#expect((try? PureXML.Schema.Document(targetNs)) == nil)
|
||||
}
|
||||
|
||||
@Test("An unprefixed ref resolves through the default namespace, qualifying an instance element")
|
||||
func test_unprefixedRefUsesDefaultNamespace() {
|
||||
// The spec-faithful common idiom: the target namespace is also the default
|
||||
// xmlns, so a bare ref="child" resolves to the target namespace and a
|
||||
// target-namespace instance element validates against it.
|
||||
let schema = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"urn:T\""
|
||||
+ " targetNamespace=\"urn:T\" elementFormDefault=\"qualified\">"
|
||||
+ "<xsd:element name=\"child\" type=\"xsd:string\"/>"
|
||||
+ "<xsd:element name=\"root\"><xsd:complexType><xsd:sequence>"
|
||||
+ "<xsd:element ref=\"child\"/></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>"
|
||||
guard let document = try? PureXML.Schema.Document(schema) else {
|
||||
Issue.record("schema should compile")
|
||||
return
|
||||
}
|
||||
#expect((try? document.validate("<root xmlns=\"urn:T\"><child>x</child></root>"))?.isEmpty == true)
|
||||
// A child in the wrong namespace is rejected (the resolution is real, not a
|
||||
// blanket accept).
|
||||
let wrong = "<root xmlns=\"urn:T\"><child xmlns=\"urn:OTHER\">x</child></root>"
|
||||
#expect((try? document.validate(wrong))?.isEmpty != true)
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,10 +9,10 @@ irreducible tail. Do not deviate to new features while this is open.
|
||||
|
||||
| Bucket | Count | Reading |
|
||||
|---|---|---|
|
||||
| valid-schemas-rejected | **72 -> 32** | production target is 0: rejecting a correct schema is the most user-hostile defect (#148). Particle-restriction over-rejection fixed: pointless `maxOccurs=0` particles removed, content-free restriction of an EMPTY base, empty-derived emptiable-base, and (#163) the pairwise check over the normalized content model with occurrence subsumption applied only to same-kind pairings (cross-kind element/group, group/element, group/wildcard match occurrence inside, by effective total range / member). Remaining 32 are mostly element-ref namespace resolution (`particlesJj/Jk/Jm`, #164). |
|
||||
| **invalid-schemas-accepted** | **2461 -> 526** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality landed). |
|
||||
| valid-instances-rejected | 233 | (instance side, separately tracked) |
|
||||
| invalid-instances-accepted | 165 | |
|
||||
| valid-schemas-rejected | **72 -> 4** | production target is 0: rejecting a correct schema is the most user-hostile defect (#148). Particle-restriction over-rejection fixed: pointless `maxOccurs=0` particles removed, content-free restriction of an EMPTY base, empty-derived emptiable-base, and (#163) the pairwise check over the normalized content model with occurrence subsumption applied only to same-kind pairings (cross-kind element/group, group/element, group/wildcard match occurrence inside, by effective total range / member). #164 resolved element-ref namespaces by prefix binding (the `particlesJj/Jk/Jm/Jn/Js`, `particlesQ/R` cluster, -28). Remaining 4 (`elemZ027_a`, `particlesV003`, `particlesW006`, `particlesZ001`). |
|
||||
| **invalid-schemas-accepted** | **2461 -> 520** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace landed). |
|
||||
| valid-instances-rejected | **233 -> 180** | (instance side; #164's element-ref namespace fix is shared with instance validation, -34) |
|
||||
| invalid-instances-accepted | 160 | |
|
||||
|
||||
One-sentence diagnosis: *the validator runs its instance-validity rules but not
|
||||
its schema-validity rules.* It compiles what it understands and ignores the
|
||||
|
||||
Loading…
Reference in New Issue
Block a user