PureXML/Sources/Validation/DTDSchema.swift
Mihaela Mihaljevic 89f8ccc95a refactor(validation): migrate DTD validation onto the framework
Slice 3 of #92. The DTD validator's imperative walk becomes declared Validation
values over a DTDSchema document:

- DTDSchema is now just the schema (content models + attribute declarations) and
  serves as the rules' Document context; its validate/walk methods are gone.
- contentModel and attributeDeclarations are Validation<Element, DTDSchema>, so
  every content-model and attribute violation is now located by coding path
  ("... at path: r/a"). undeclaredElement is added only in strict mode.
- identifierIntegrity is a Validation<Node, DTDSchema> gated to the root, doing
  the document-wide ID-uniqueness and IDREF-resolution checks in one pass.
- DTD.validator(strict:) assembles the rule set; validateAgainstInternalDTD runs
  it and returns [ValidationError] instead of [Issue].

A new test pins the located result. Schema (XSD) and Schematron validators still
use Issue and migrate in the following slices.
2026-06-08 12:36:56 +02:00

29 lines
1.1 KiB
Swift

public extension PureXML.Validation {
/// A DTD schema: the element content models declared by `<!ELEMENT>` and the
/// attribute declarations from `<!ATTLIST>`, against which a parsed tree can
/// be validated. Built from the internal subset surfaced by the parser.
struct DTDSchema: Sendable {
let models: [String: ContentModel]
let attributes: [String: [AttributeDeclaration]]
init(_ documentType: PureXML.Parsing.DocumentType) {
var parsedModels: [String: ContentModel] = [:]
for (name, model) in documentType.elementModels {
parsedModels[name] = ContentModelParser.parse(model)
}
models = parsedModels
var parsedAttributes: [String: [AttributeDeclaration]] = [:]
for (name, body) in documentType.attributeLists {
parsedAttributes[name] = AttributeListParser.parse(body)
}
attributes = parsedAttributes
}
/// Whether the schema declares any elements or attributes.
public var isEmpty: Bool {
models.isEmpty && attributes.isEmpty
}
}
}