fix(schema): resolve <xs:attribute ref> to global attribute declarations

An <xs:attribute ref="..."> 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.
This commit is contained in:
Mihaela Mihaljevic 2026-06-13 19:28:17 +02:00
parent 77db89397b
commit 9ff7ed360f
6 changed files with 105 additions and 40 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- `<xs:attribute ref="...">` 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.

View File

@ -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<String> = []) -> [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 `<attribute ref="...">` 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),
)
}
}

View File

@ -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
/// `<attribute ref="...">` 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.

View File

@ -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<String> = []) -> [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? {

View File

@ -0,0 +1,44 @@
@testable import PureXML
import Testing
/// An `<xs:attribute ref="...">` 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("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="a1" type="xs:string"/>
<xs:element name="doc">
<xs:complexType>
<xs:attributeGroup ref="g"/>
</xs:complexType>
</xs:element>
<xs:attributeGroup name="g">
<xs:attribute ref="a1"/>
<xs:attribute name="a2" type="xs:int"/>
</xs:attributeGroup>
</xs:schema>
""")
}
}
@Test("A referenced global attribute is admitted on the instance")
func test_referencedAttributeAdmitted() throws {
#expect(try schema.validate(#"<doc a1="x" a2="5"/>"#).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(#"<doc a1="x" a2="notint"/>"#).isEmpty)
}
@Test("An attribute outside the (nested) group is still undeclared")
func test_undeclaredStillRejected() throws {
#expect(try !schema.validate(#"<doc a1="x" a2="5" a3="y"/>"#).isEmpty)
}
}

View File

@ -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 {