fix(schema): resolve identity attribute-field types for value-space keys (#147)

An identity-constraint attribute field's type was resolved only among the
constraint host's descendant elements, so when the selector targets a global
element referenced from the host (e.g. unique over `.//uid` with field
`@val`), the field type was unknown and values compared lexically. Two keys
equal in value space but not lexically (3.0 and 3 for xsd:decimal) were then
treated as distinct and the duplicate was accepted.

The attribute type now resolves through the selector's target global element,
so values compare in their value space. XSTS invalid-instances-accepted
133 to 132 (idF010), no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 10:31:32 +02:00
parent 39a91c258e
commit fe8a6e8d6e
3 changed files with 36 additions and 4 deletions

View File

@ -22,7 +22,7 @@ Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-s
### Fixed
- XSTS invalid-instances-accepted (#147): identity constraints reject more invalid instances. Fields now enforce cardinality (a `field` must select at most one node; cvc-identity-constraint.3), so an instance whose `unique`/`key`/`keyref` field matches several nodes is rejected. Selector and field XPath prefixes now resolve against the schema's namespace context (where the constraint is declared) rather than the instance document, so a constraint like `.//v:vehicle` works whatever prefix the instance uses; a QName field value still compares using the instance's bindings. Together these caught fourteen cases (idF005, idG005, the idc004/5/6 cluster, idZ010, and kin): invalid-instances-accepted 147 → 133, no other bucket moved.
- XSTS invalid-instances-accepted (#147): identity constraints reject more invalid instances. Fields now enforce cardinality (a `field` must select at most one node; cvc-identity-constraint.3), so an instance whose `unique`/`key`/`keyref` field matches several nodes is rejected. Selector and field XPath prefixes now resolve against the schema's namespace context (where the constraint is declared) rather than the instance document, so a constraint like `.//v:vehicle` works whatever prefix the instance uses; a QName field value still compares using the instance's bindings. An attribute field's type also resolves through the selector's target global element, so values compare in their value space (3.0 and 3 are the same xsd:decimal key). Together these caught fifteen cases (idF005, idG005, idF010, the idc004/5/6 cluster, idZ010, and kin): invalid-instances-accepted 147 → 132, no other bucket moved.
- XSTS valid-instances-rejected (#146): **173 → 3** on the 2006-11-06 archive (settled-status count; the four buckets now read 1/262/3/143). Regex patterns match Unicode scalars (not Swift grapheme clusters), fixing `\p{Is…}` block tests and combining-mark cases; empty patterns match only the empty string. The regex repertoire is pinned to Unicode 3.2, the version the corpus is consistent at: code points added in 3.2 (Tagalog, Cyrillic Komi) are word characters, while later additions (the Tamil digit zero) stay unassigned and match `\D` (`XSDCategory`, `Unicode32Assigned`). Per-child content assessment is driven by the content-model particle the automaton matched, not a by-name lookup (#180), so two same-named particles keep their own `fixed` values and an element matching a `skip` wildcard is not validated against its global declaration. Schema documents that form an import chain are compiled as a union rather than merged as separately compiled documents, so cross-document substitution groups resolve (#161, `Document.init(composing:)`). Plus attribute-group `anyAttribute` inheritance, extension wildcard union, namespace-qualified type tables for multi-document `xsi:schemaLocation`, per-container `elementFormDefault`, strict/lax attribute wildcard validation, built-in type resolution, and `xs:redefine` simpleType overrides.

View File

@ -46,7 +46,7 @@ extension PureXML.Schema.XSDParser {
}
continue
}
if let type = identityFieldType(field, host: element, constraint: constraint, context) {
if let type = identityFieldType(field, host: element, constraint: constraint, containers: containers, context) {
types[identityFieldKey(constraint: constraint, field: field)] = type
}
}
@ -76,11 +76,25 @@ extension PureXML.Schema.XSDParser {
_ field: String,
host: XSDTree,
constraint: PureXML.Schema.IdentityConstraint,
containers: [XSDTree],
_ context: PureXML.Schema.XSDContext,
) -> PureXML.Schema.SimpleType? {
if field.hasPrefix("@") {
let attributeName = identityFieldAttributeName(field)
return attributeType(named: attributeName, under: host, context)
if let type = attributeType(named: attributeName, under: host, context) { return type }
// The selector's target may be a global element referenced from the
// host rather than nested under it; the attribute is then on that
// element's own complex type, so field values compare in its value
// space (e.g. 3.0 and 3 are the same xsd:decimal key).
guard let target = selectorTargetLocalName(constraint.selector),
let element = globalElementNode(named: target, containers),
let complex = PureXML.Schema.XSDNode.firstChild(element, named: "complexType")
else { return nil }
for attribute in descendants(complex, named: "attribute") {
guard PureXML.Schema.XSDNode.attribute(attribute, "name") == attributeName else { continue }
if let use = PureXML.Schema.XSDParser.attributeUse(attribute, context) { return use.type }
}
return nil
}
if field == "." {
let local = selectorTargetLocalName(constraint.selector)
@ -100,6 +114,19 @@ extension PureXML.Schema.XSDParser {
return PureXML.Schema.XSDNode.stripPrefix(token)
}
/// The top-level (global) element declaration node of the given local name.
private static func globalElementNode(named local: String, _ containers: [XSDTree]) -> XSDTree? {
for container in containers {
for child in PureXML.Schema.XSDNode.elementChildren(container) {
guard PureXML.Schema.XSDNode.localName(child) == "element",
PureXML.Schema.XSDNode.attribute(child, "name") == local
else { continue }
return child
}
}
return nil
}
private static func declaredElementSimpleType(
named local: String?,
under host: XSDTree,

View File

@ -63,10 +63,15 @@ struct XSTSSuiteTests {
/// the instance document then caught ten more (the idc004/5/6 cluster and
/// namespace-resolution cases): invalid accepted 143 → 133, no other bucket
/// moved.
///
/// Resolving an identity-constraint attribute field's type through the
/// selector's target global element (not just the host's descendants) lets a
/// `unique`/`key` compare values in their value space (3.0 and 3 are the same
/// xsd:decimal key): invalid accepted 133 → 132, no other bucket moved.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 262
private let knownInstanceValidRejected = 3
private let knownInstanceInvalidAccepted = 133
private let knownInstanceInvalidAccepted = 132
@Test("Every XSTS case behaves: compile, reject, validate, invalidate")
func test_suite() throws {