From 2419ab43d9ab450e16a099fbb3989ae15741ed02 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Mon, 22 Jun 2026 04:41:28 +0200 Subject: [PATCH] fix(schema): reject identity-constraint field that identifies a complex-content node Per cvc-identity-constraint.3 / c-fields-xpaths an identity-constraint (key/unique/keyref) field must evaluate to a node with a simple type: a simple-typed element, an element with simple content, or an attribute. The field-cardinality clause (at most one node) was enforced, but the simple-content clause was not, so a field selecting a complex-content element was wrongly accepted at instance-validation time. Add a structural, false-positive-free check: a field whose selected node is an element carrying element children has complex (element-only or mixed) content and can never be simple-typed, so it is flagged. A simple-typed or simple-content element (text only), an empty or text-only node, and an attribute are left alone, so no valid field is rejected. This catches W3C XSTS idK012. XSTS invalid-instances-accepted 26 -> 25, valid-instances-rejected held at 0. --- CHANGELOG.md | 1 + Sources/Schema/IdentityValidator.swift | 33 +++++++++++++++++++------- Tests/XSDIdentityTests.swift | 33 ++++++++++++++++++++++++++ Tests/XSTSSuiteTests.swift | 14 +++++------ 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36a67e9..46d6c24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- An identity-constraint (`key`/`unique`/`keyref`) field that identifies a node with complex content is now rejected during instance validation. Per cvc-identity-constraint.3 / c-fields-xpaths a field must evaluate to a node with a simple type (a simple-typed element, an element with simple content, or an attribute); the cardinality clause (at most one node) was enforced, but the simple-content clause was not. The check is structural and false-positive-free: a field whose selected node is an element carrying element children has complex (element-only or mixed) content and can never be simple-typed, so it is flagged, while a simple-typed or simple-content element (text only), an empty or text-only node, and an attribute are left alone. Catches idK012. XSTS invalid-instances-accepted 26 → 25, valid-instances-rejected held at 0. - The attribute-use uniqueness check (`ag-props-correct.2` / `ct-props-correct.4`) now flattens a redefined attribute group's self-reference and excludes prohibited uses. When an attribute group flattens its uses through `` references, a reference to a redefined group whose redefinition self-references its former self previously stopped at the cycle guard, so the original's attributes were never reached and a clash they cause was missed. The self-reference now expands to the group's pre-redefine base definition (already retained for this purpose), so the original's attributes join the set; a genuine non-redefine cycle still terminates without contributing. Separately, a `use="prohibited"` attribute is now excluded from the duplicate-name and single-ID counts, since a prohibited use is a removal marker, not a member of `{attribute uses}` (XSD §3.4.2/§3.6.2); this also keeps a self-reference that re-imports an attribute and then prohibits it from reading as a duplicate. Catches attQ011 and attQ017 (an attribute group referencing both a global attribute and a redefined self-referencing group that supplies the same-named attribute). XSTS invalid-schemas-accepted 22 → 20, valid-schemas-rejected held at 0. - A redefinition of an `attributeGroup` that SELF-references the original and also directly re-declares one of its attributes is now rejected. The self-reference re-imports all of the original's attribute uses, so a same-named direct re-declaration produces two attribute uses of the same name, a duplicate forbidden by ag-props-correct.2. The check fires only for a re-declared, non-prohibited attribute the original directly declares, and only when both the original and the re-declaration land in no namespace (a qualified attribute's target namespace depends on cross-document `form`/chameleon resolution, so it is not compared, staying lenient); a self-referencing redefinition that adds genuinely new attributes (attgC007/attgC038/groupB018) stays valid. Catches schM8 (`c-g2` self-references and re-declares `a1`). XSTS invalid-schemas-accepted 23 → 22 (schM8), valid-schemas-rejected held at 0. - A redefinition of an `attributeGroup` that WIDENS a re-declared attribute's type is now rejected. A redefinition is a restriction (src-redefine / cos-ct-restricts), so a re-declared attribute's type must derive by restriction from the original's. The check is built-in-only and therefore certain: it fires only when both the original and the re-declared attribute name a built-in type (resolved to the XSD namespace) and the re-declared built-in does not derive from the original's in the XSD derivation lattice (`int` does not derive from `boolean`). A user, inline, or absent type on either side is not compared (declines, staying lenient): an absent original type is `anySimpleType`, which any type validly restricts, a user/inline type's derivation is not resolvable here, and a user type whose local name coincides with a built-in (a `{urn:x}int`) resolves to a different namespace and is not mistaken for the built-in, so no valid restriction is rejected. Catches schM4 (`b1` re-typed `boolean` → `int`). XSTS invalid-schemas-accepted 24 → 23 (schM4), valid-schemas-rejected held at 0. diff --git a/Sources/Schema/IdentityValidator.swift b/Sources/Schema/IdentityValidator.swift index 27cd167..78b5bc3 100644 --- a/Sources/Schema/IdentityValidator.swift +++ b/Sources/Schema/IdentityValidator.swift @@ -132,7 +132,7 @@ extension PureXML.Schema { var seen: [[FieldValue?]] = [] for target in select(constraint.selector, at: node, namespaces: constraint.namespaceBindings) { let targetPath = path + steps(from: node, to: target) - if let error = fieldCardinalityError(constraint, at: target, path: targetPath) { + if let error = fieldError(constraint, at: target, path: targetPath) { issues.append(error) continue } @@ -169,7 +169,7 @@ extension PureXML.Schema { let keyTuples = scopes.reversed().compactMap { $0[refer] }.first ?? [] for target in select(constraint.selector, at: node, namespaces: constraint.namespaceBindings) { let targetPath = path + steps(from: node, to: target) - if let error = fieldCardinalityError(constraint, at: target, path: targetPath) { + if let error = fieldError(constraint, at: target, path: targetPath) { issues.append(error) continue } @@ -188,16 +188,33 @@ extension PureXML.Schema { /// `target`. A field must evaluate to a node-set with at most one member /// (Identity-constraint Definition; cvc-identity-constraint.3); selecting /// several is a violation, not a multi-valued key. - private func fieldCardinalityError( + private func fieldError( _ constraint: IdentityConstraint, at target: PureXML.Model.TreeNode, path targetPath: [PureXML.Validation.PathKey], ) -> PureXML.Validation.ValidationError? { - for field in constraint.fields where select(field, at: target, namespaces: constraint.namespaceBindings).count > 1 { - return .init( - reason: "\(label(constraint)) '\(constraint.name)': field '\(field)' must select at most one node", - at: targetPath + fieldStep(constraint.fields, at: target, namespaces: constraint.namespaceBindings), - ) + let step = fieldStep(constraint.fields, at: target, namespaces: constraint.namespaceBindings) + for field in constraint.fields { + let selected = select(field, at: target, namespaces: constraint.namespaceBindings) + // cvc-identity-constraint.3: each field must evaluate to at most one node. + if selected.count > 1 { + return .init( + reason: "\(label(constraint)) '\(constraint.name)': field '\(field)' must select at most one node", + at: targetPath + step, + ) + } + // cvc-identity-constraint.3 / c-fields-xpaths: a field must identify a node + // with a simple type (or simple content). An element with element children + // has complex content and can never be simple-typed, so it is not a valid + // field target. Checked structurally: a simple-typed or simple-content + // element carries only text, never element children, so no valid field is + // rejected; an empty or text-only node, or an attribute, is left alone. + if let node = selected.first, node.kind == .element, node.children.contains(where: { $0.kind == .element }) { + return .init( + reason: "\(label(constraint)) '\(constraint.name)': field '\(field)' must identify a node with simple content", + at: targetPath + step, + ) + } } return nil } diff --git a/Tests/XSDIdentityTests.swift b/Tests/XSDIdentityTests.swift index a685082..abc3d91 100644 --- a/Tests/XSDIdentityTests.swift +++ b/Tests/XSDIdentityTests.swift @@ -9,6 +9,39 @@ struct XSDIdentityTests { private let head = "" + @Test("a field selecting a complex-content element is rejected (cvc-identity-constraint.3)") + func test_fieldMustBeSimpleContent() throws { + // The field `pid` selects an element with element children (`gid`), which has + // complex content and can never be simple-typed: invalid (idK012/idG006). + let complexField = """ + \(head) + + + + + + + + + + """ + #expect(try !validate(complexField, "x").isEmpty) + // Guard: a simple-content field target stays valid (no false positive). + let simpleField = """ + \(head) + + + + + + + + + + """ + #expect(try validate(simpleField, "ab").isEmpty) + } + @Test("xs:unique rejects a duplicated field value") func test_unique() throws { let xsd = """ diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 03e0570..64801ee 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -150,13 +150,13 @@ struct XSTSSuiteTests { private let knownSchemaValidRejected = 0 private let knownSchemaInvalidAccepted = 20 private let knownInstanceValidRejected = 0 - /// Instance bucket (133 -> 26), per-step deltas in CHANGELOG.md: xsi:type must derive from the - /// declared type (complex/atomic/list/union via the element-ref chain); anyType cannot stand in - /// for anySimpleType; an untyped substitutionGroup member inherits its head's type; a non-nillable - /// element carries no xsi:nil (cvc-elt.3.1); block/blockDefault bar substitutions; ur-type anyType - /// wildcards are `lax`; identity fields compare in decimal value space, taking a default/fixed when - /// absent/empty; an attribute wildcard is the INTERSECTION of its sources; year 0000 is invalid. - private let knownInstanceInvalidAccepted = 26 + /// Instance bucket (133 -> 25), per-step deltas in CHANGELOG.md: xsi:type must derive from the + /// declared type (via the element-ref chain); anyType cannot stand in for anySimpleType; an untyped + /// substitutionGroup member inherits its head's type; a non-nillable element carries no xsi:nil; block/ + /// blockDefault bar substitutions; ur-type anyType wildcards are `lax`; identity fields compare in + /// decimal value space, taking a default/fixed when absent/empty; an attribute wildcard is the + /// INTERSECTION of its sources; year 0000 is invalid; an identity field must be simple-content (idK012). + private let knownInstanceInvalidAccepted = 25 @Test("Every XSTS case behaves: compile, reject, validate, invalidate") func test_suite() throws {