From 9ff7ed360fbc388c087297e997b36ad8328a9830 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Sat, 13 Jun 2026 19:28:17 +0200 Subject: [PATCH] fix(schema): resolve to global attribute declarations An reference has no name attribute, so attributeUse returned nil and the referenced attribute was dropped, making it look undeclared on the instance (nested attributeGroup refs were already flattened, but the per-attribute reference was not). References now resolve through a new global attribute index to the declaration's type, with use/default/fixed taken from the reference site and the name qualified to the target namespace. XSTS: valid instances rejected 338 to 311. Baseline updated; regression tests cover a referenced attribute admitted, its type enforced, and an attribute outside the group still rejected. The attribute-parsing functions move to XSDAttributeParser.swift to keep XSDParser within the type-body length limit. Refs #147, #129. --- CHANGELOG.md | 1 + Sources/Schema/XSDAttributeParser.swift | 54 +++++++++++++++++++++++++ Sources/Schema/XSDContext.swift | 3 ++ Sources/Schema/XSDParser.swift | 39 +----------------- Tests/SchemaAttributeRefTests.swift | 44 ++++++++++++++++++++ Tests/XSTSSuiteTests.swift | 4 +- 6 files changed, 105 insertions(+), 40 deletions(-) create mode 100644 Sources/Schema/XSDAttributeParser.swift create mode 100644 Tests/SchemaAttributeRefTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index b959257..1f0d783 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `` references to global attribute declarations are now resolved (#147). A reference has no `name`, so it was dropped and the referenced attribute looked undeclared on the instance; nested attribute-group references were already flattened, but the per-attribute reference was not. The reference now resolves to the global declaration's type, taking `use`/`default`/`fixed` from the reference site. XSTS: valid instances rejected 338 to 311. - Document-scoped `xs:ID`/`xs:IDREF` validation is now enforced (#147). `xs:ID`, `xs:IDREF`, and `xs:IDREFS` were checked only lexically (as NCNames); the document-wide constraints, that every ID is unique and every IDREF/IDREFS item matches some ID, were not. The tree validator now records ID and reference values during its typed walk (elements and attributes) and reports duplicate IDs and unresolved references once the whole document is seen, so a forward reference resolves against a later ID. XSTS: invalid instances accepted 179 to 166. - The regex `\w` and `\W` escapes now follow the XSD definition (#147). `\w` was the Perl class (alphabetic, decimal, underscore); XSD defines it as `[#x0000-#x10FFFF]-[\p{P}\p{Z}\p{C}]`, so symbols and marks are word characters and the connector `_` (Pc) is not. `\W` correspondingly matched symbols it should not. XSTS: invalid instances accepted 231 to 210. (Internal regex fixtures that assumed `_` was a word character were corrected.) - `block="substitution"` on a substitution-group head is now enforced (#147). The substitution filter only dropped members whose type derived from the head's type by a blocked method (`extension`/`restriction`); the `substitution` keyword, which forbids substitution-group substitution outright regardless of the member's type, was ignored, so a same-type member still substituted under a substitution block. XSTS: invalid instances accepted 238 to 231, valid schemas rejected 75 to 74. diff --git a/Sources/Schema/XSDAttributeParser.swift b/Sources/Schema/XSDAttributeParser.swift new file mode 100644 index 0000000..4059c7d --- /dev/null +++ b/Sources/Schema/XSDAttributeParser.swift @@ -0,0 +1,54 @@ +extension PureXML.Schema.XSDParser { + /// The attribute uses declared directly under `node` and through any + /// `attributeGroup` references it nests, flattened. The `visited` set guards + /// against attribute-group reference cycles. + static func attributeUses(under node: XSDTree, _ context: PureXML.Schema.XSDContext, visited: Set = []) -> [PureXML.Schema.AttributeUse] { + var uses: [PureXML.Schema.AttributeUse] = [] + for child in PureXML.Schema.XSDNode.elementChildren(node) { + switch PureXML.Schema.XSDNode.localName(child) { + case "attribute": + if let use = attributeUse(child, context) { uses.append(use) } + case "attributeGroup": + guard let ref = PureXML.Schema.XSDNode.attribute(child, "ref") else { break } + let name = PureXML.Schema.XSDNode.stripPrefix(ref) + guard !visited.contains(name), let group = context.attributeGroups[name] else { break } + uses += attributeUses(under: group, context, visited: visited.union([name])) + default: + break + } + } + return uses + } + + static func attributeUse(_ node: XSDTree, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.AttributeUse? { + // An `` references a global attribute declaration: + // take its name (always target-namespace qualified) and type from the + // global node, but `use`/`default`/`fixed` from this reference site. + if let ref = PureXML.Schema.XSDNode.attribute(node, "ref") { + let refName = PureXML.Schema.XSDNode.stripPrefix(ref) + guard let declaration = context.globalAttributes[refName], + var use = attributeUse(declaration, context) else { return nil } + use.name = PureXML.Model.QualifiedName(localName: refName, namespaceURI: context.targetNamespace) + if PureXML.Schema.XSDNode.attribute(node, "use") == "required" { use.required = true } + if let constraint = valueConstraint(of: node) { use.valueConstraint = constraint } + return use + } + guard let name = PureXML.Schema.XSDNode.attribute(node, "name") else { return nil } + let required = PureXML.Schema.XSDNode.attribute(node, "use") == "required" + let type: PureXML.Schema.SimpleType = if let typeName = PureXML.Schema.XSDNode.attribute(node, "type") { + PureXML.Schema.XSDSimpleParser.simpleTypeReference(typeName, context) + } else if let inline = PureXML.Schema.XSDNode.firstChild(node, named: "simpleType") { + PureXML.Schema.XSDSimpleParser.simpleType(inline, context) + } else { + PureXML.Schema.SimpleType(base: .string) + } + let qualified = PureXML.Schema.XSDNode.attribute(node, "form") == "qualified" + || (PureXML.Schema.XSDNode.attribute(node, "form") == nil && context.attributeFormQualified) + return PureXML.Schema.AttributeUse( + name: PureXML.Model.QualifiedName(localName: name, namespaceURI: qualified ? context.targetNamespace : nil), + type: type, + required: required, + valueConstraint: valueConstraint(of: node), + ) + } +} diff --git a/Sources/Schema/XSDContext.swift b/Sources/Schema/XSDContext.swift index 34aac12..3caff96 100644 --- a/Sources/Schema/XSDContext.swift +++ b/Sources/Schema/XSDContext.swift @@ -132,6 +132,9 @@ extension PureXML.Schema { struct XSDContext { var simpleTypes: [String: SimpleType] var attributeGroups: [String: XSDTree] + /// Top-level (global) attribute declaration nodes by name, so an + /// `` resolves to the global declaration's type. + var globalAttributes: [String: XSDTree] = [:] var groups: [String: XSDTree] /// Named complex-type definition nodes, so a `complexContent` derivation can /// resolve and compose its base type's content model and attributes. diff --git a/Sources/Schema/XSDParser.swift b/Sources/Schema/XSDParser.swift index 43e7304..72100ad 100644 --- a/Sources/Schema/XSDParser.swift +++ b/Sources/Schema/XSDParser.swift @@ -32,6 +32,7 @@ extension PureXML.Schema { context.elementFormQualified = XSDNode.attribute(schema, "elementFormDefault") == "qualified" context.attributeFormQualified = XSDNode.attribute(schema, "attributeFormDefault") == "qualified" context.complexTypeNodes = indexByName(allChildren(containers, named: "complexType")) + context.globalAttributes = indexByName(allChildren(containers, named: "attribute")) resolveSimpleTypes(allChildren(containers, named: "simpleType"), into: &context) var types: [String: ElementType] = context.simpleTypes.mapValues(ElementType.simple) for node in allChildren(containers, named: "complexType") { @@ -167,44 +168,6 @@ extension PureXML.Schema { // MARK: Attribute uses and groups - static func attributeUses(under node: XSDTree, _ context: XSDContext, visited: Set = []) -> [AttributeUse] { - var uses: [AttributeUse] = [] - for child in XSDNode.elementChildren(node) { - switch XSDNode.localName(child) { - case "attribute": - if let use = attributeUse(child, context) { uses.append(use) } - case "attributeGroup": - guard let ref = XSDNode.attribute(child, "ref") else { break } - let name = XSDNode.stripPrefix(ref) - guard !visited.contains(name), let group = context.attributeGroups[name] else { break } - uses += attributeUses(under: group, context, visited: visited.union([name])) - default: - break - } - } - return uses - } - - private static func attributeUse(_ node: XSDTree, _ context: XSDContext) -> AttributeUse? { - guard let name = XSDNode.attribute(node, "name") else { return nil } - let required = XSDNode.attribute(node, "use") == "required" - let type: SimpleType = if let typeName = XSDNode.attribute(node, "type") { - XSDSimpleParser.simpleTypeReference(typeName, context) - } else if let inline = XSDNode.firstChild(node, named: "simpleType") { - XSDSimpleParser.simpleType(inline, context) - } else { - SimpleType(base: .string) - } - let qualified = XSDNode.attribute(node, "form") == "qualified" - || (XSDNode.attribute(node, "form") == nil && context.attributeFormQualified) - return AttributeUse( - name: PureXML.Model.QualifiedName(localName: name, namespaceURI: qualified ? context.targetNamespace : nil), - type: type, - required: required, - valueConstraint: valueConstraint(of: node), - ) - } - /// The `default` or `fixed` value constraint declared on an attribute or /// element node, if any (`fixed` takes precedence). static func valueConstraint(of node: XSDTree) -> ValueConstraint? { diff --git a/Tests/SchemaAttributeRefTests.swift b/Tests/SchemaAttributeRefTests.swift new file mode 100644 index 0000000..f06ab5f --- /dev/null +++ b/Tests/SchemaAttributeRefTests.swift @@ -0,0 +1,44 @@ +@testable import PureXML +import Testing + +/// An `` resolves to a global attribute declaration, +/// taking its type from the global node and its use/default/fixed from the +/// reference site (#147, XSTS attgD set). References were dropped (they have no +/// `name`), so a referenced attribute looked undeclared on the instance. +@Suite("Attribute references in attribute groups") +struct SchemaAttributeRefTests { + private var schema: PureXML.Schema.Document { + get throws { + try PureXML.Schema.Document(""" + + + + + + + + + + + + + """) + } + } + + @Test("A referenced global attribute is admitted on the instance") + func test_referencedAttributeAdmitted() throws { + #expect(try schema.validate(#""#).isEmpty) + } + + @Test("The referenced attribute keeps the global declaration's type") + func test_refTypeEnforced() throws { + // a2 is xs:int via inline declaration; a non-integer is rejected. + #expect(try !schema.validate(#""#).isEmpty) + } + + @Test("An attribute outside the (nested) group is still undeclared") + func test_undeclaredStillRejected() throws { + #expect(try !schema.validate(#""#).isEmpty) + } +} diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 945bd45..6daf15b 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -30,8 +30,8 @@ struct XSTSSuiteTests { /// are written to /tmp/xsts-failures.txt for the burn-down. private let knownSchemaValidRejected = 72 private let knownSchemaInvalidAccepted = 2461 - private let knownInstanceValidRejected = 338 - private let knownInstanceInvalidAccepted = 166 + private let knownInstanceValidRejected = 311 + private let knownInstanceInvalidAccepted = 165 @Test("Every XSTS case behaves: compile, reject, validate, invalidate") func test_suite() throws {