diff --git a/Sources/Validation/Schematron.swift b/Sources/Validation/Schematron.swift index 1f0d56a..d70b299 100644 --- a/Sources/Validation/Schematron.swift +++ b/Sources/Validation/Schematron.swift @@ -28,7 +28,7 @@ public extension PureXML.Validation { /// `#ALL` runs every pattern. public func validate(_ xml: String, phase: String? = nil) throws -> [ValidationError] { let node = try PureXML.parse(xml) - let validation = Self.rule(activePatterns(phase: phase), diagnostics: schema.diagnostics) + let validation = Self.rule(activePatterns(phase: phase), schemaLets: schema.lets, diagnostics: schema.diagnostics) return Validator.blank.validating(validation).errors(for: node, in: ()) } @@ -45,10 +45,18 @@ public extension PureXML.Validation { /// The schema as a single ``Validation`` over the document root, so it /// composes with the rest of the framework. - static func rule(_ patterns: [SchematronPattern], diagnostics: [String: [SchematronMessagePart]]) -> Validation { + static func rule( + _ patterns: [SchematronPattern], + schemaLets: [SchematronLet], + diagnostics: [String: [SchematronMessagePart]], + ) -> Validation { .init( description: "Document satisfies the Schematron schema", - check: { context in evaluate(patterns, over: PureXML.Model.TreeNode(context.subject), diagnostics: diagnostics) }, + check: { context in + let root = PureXML.Model.TreeNode(context.subject) + let base = bindings(schemaLets, at: root, base: [:]) + return evaluate(patterns, over: root, base: base, diagnostics: diagnostics) + }, when: { $0.codingPath.isEmpty }, ) } @@ -56,30 +64,36 @@ public extension PureXML.Validation { private static func evaluate( _ patterns: [SchematronPattern], over root: PureXML.Model.TreeNode, + base: [String: PureXML.XPath.Value], diagnostics: [String: [SchematronMessagePart]], ) -> [ValidationError] { - patterns.flatMap { evaluate($0, over: root, diagnostics: diagnostics) } + patterns.flatMap { evaluate($0, over: root, base: base, diagnostics: diagnostics) } } private static func evaluate( _ pattern: SchematronPattern, over root: PureXML.Model.TreeNode, + base: [String: PureXML.XPath.Value], diagnostics: [String: [SchematronMessagePart]], ) -> [ValidationError] { + // Pattern-level lets extend the schema-level base, evaluated at the root. + let patternBase = bindings(pattern.lets, at: root, base: base) var errors: [ValidationError] = [] var claimed: Set = [] for rule in pattern.rules { for node in rule.context.nodes(over: root) where claimed.insert(ObjectIdentifier(node)).inserted { - errors += apply(rule.assertions, at: node, variables: bindings(rule.lets, at: node), diagnostics: diagnostics) + let variables = bindings(rule.lets, at: node, base: patternBase) + errors += apply(rule.assertions, at: node, variables: variables, diagnostics: diagnostics) } } return errors } - /// Evaluates a rule's `` bindings at the context node, in order, so a - /// later binding can reference an earlier one through `$name`. - private static func bindings(_ lets: [SchematronLet], at node: PureXML.Model.TreeNode) -> [String: PureXML.XPath.Value] { - var variables: [String: PureXML.XPath.Value] = [:] + /// Evaluates `` bindings over `base` at `node`, in order, so a later + /// binding can reference an earlier one (or a wider-scope one) through + /// `$name`. + private static func bindings(_ lets: [SchematronLet], at node: PureXML.Model.TreeNode, base: [String: PureXML.XPath.Value]) -> [String: PureXML.XPath.Value] { + var variables = base for binding in lets { if let value = try? binding.value.value(at: node, variables: variables) { variables[binding.name] = value diff --git a/Sources/Validation/SchematronModel.swift b/Sources/Validation/SchematronModel.swift index bd17a9f..10a8bba 100644 --- a/Sources/Validation/SchematronModel.swift +++ b/Sources/Validation/SchematronModel.swift @@ -40,6 +40,7 @@ extension PureXML.Validation { /// context selects it (the ISO Schematron firing rule). struct SchematronPattern { let id: String? + let lets: [SchematronLet] let rules: [SchematronRule] } @@ -49,6 +50,9 @@ extension PureXML.Validation { let patterns: [SchematronPattern] let phases: [String: [String]] let defaultPhase: String? + /// Schema-level `` bindings, evaluated once at the document root and + /// available to every rule's tests. + let lets: [SchematronLet] /// `` message templates, keyed by id, referenced from an /// assertion's `diagnostics=` attribute. let diagnostics: [String: [SchematronMessagePart]] diff --git a/Sources/Validation/SchematronParser.swift b/Sources/Validation/SchematronParser.swift index 0b19d59..f9d9555 100644 --- a/Sources/Validation/SchematronParser.swift +++ b/Sources/Validation/SchematronParser.swift @@ -19,6 +19,7 @@ extension PureXML.Validation { patterns: patterns, phases: phases(root), defaultPhase: attribute(schema, "defaultPhase"), + lets: ruleLets(schema), diagnostics: diagnostics(root), ) } @@ -43,7 +44,7 @@ extension PureXML.Validation { let rules = try ruleNodes .filter { attribute($0, "abstract") != "true" } .compactMap { try rule($0, abstracts) } - return SchematronPattern(id: attribute(node, "id"), rules: rules) + return try SchematronPattern(id: attribute(node, "id"), lets: ruleLets(node), rules: rules) } private static func rule(_ node: PureXML.Model.TreeNode, _ abstracts: [String: AbstractRule]) throws -> SchematronRule? { diff --git a/Tests/SchematronScopedLetTests.swift b/Tests/SchematronScopedLetTests.swift new file mode 100644 index 0000000..940613f --- /dev/null +++ b/Tests/SchematronScopedLetTests.swift @@ -0,0 +1,58 @@ +@testable import PureXML +import Testing + +@Suite("Schematron schema- and pattern-level let") +struct SchematronScopedLetTests { + private func validate(_ schema: String, _ xml: String) throws -> [PureXML.Validation.ValidationError] { + try PureXML.Validation.Schematron(schema: schema).validate(xml) + } + + @Test("A schema-level let is available to every rule") + func test_schemaLet() throws { + let schema = """ + + + + + over the schema limit + + + + """ + #expect(try validate(schema, "5").isEmpty) + #expect(try !validate(schema, "50").isEmpty) + } + + @Test("A pattern-level let is available to the pattern's rules") + func test_patternLet() throws { + let schema = """ + + + + + flag must be yes + + + + """ + #expect(try validate(schema, "yes").isEmpty) + #expect(try !validate(schema, "no").isEmpty) + } + + @Test("A rule-level let overrides a wider-scope let of the same name") + func test_ruleOverridesSchema() throws { + let schema = """ + + + + + + over the rule limit + + + + """ + // The rule's limit (10) wins over the schema's (100). + #expect(try !validate(schema, "50").isEmpty) + } +}