diff --git a/CHANGELOG.md b/CHANGELOG.md index e7e0698..3442721 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Sources/Schema/XSDParserHelpers.swift b/Sources/Schema/XSDParserHelpers.swift index 2035d05..87a19a6 100644 --- a/Sources/Schema/XSDParserHelpers.swift +++ b/Sources/Schema/XSDParserHelpers.swift @@ -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, diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 0b9e2b3..14a00ef 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -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 {