diff --git a/Sources/Schema/RelaxNGCompactParser.swift b/Sources/Schema/RelaxNGCompactParser.swift index 04efb7c..0bd1f61 100644 --- a/Sources/Schema/RelaxNGCompactParser.swift +++ b/Sources/Schema/RelaxNGCompactParser.swift @@ -249,7 +249,8 @@ extension RNCParser { private func parseNonWordPrimary() -> Pattern { switch peek() { case let .string(value): advance() - return .value(value) + // A bare string literal is shorthand for a `token`-typed value. + return .value(PureXML.Schema.SimpleType(base: .token), value) case .symbol("("): advance() let pattern = parsePattern() expectSymbol(")") @@ -268,7 +269,7 @@ extension RNCParser { var facets = PureXML.Schema.Facets() if peek() == .symbol("{") { parseParams(into: &facets) } if case let .string(value) = peek() { advance() - return .value(value) + return .value(PureXML.Schema.SimpleType(base: type), value) } return .data(PureXML.Schema.SimpleType(base: type, facets: facets)) } diff --git a/Sources/Schema/RelaxNGDerivative.swift b/Sources/Schema/RelaxNGDerivative.swift index 1a3ca92..b51271f 100644 --- a/Sources/Schema/RelaxNGDerivative.swift +++ b/Sources/Schema/RelaxNGDerivative.swift @@ -83,9 +83,8 @@ extension PureXML.Schema { private func valueDeriv(_ pattern: PureXML.Schema.Pattern, _ string: String) -> PureXML.Schema.Pattern { switch pattern { - case let .value(literal): - SimpleType(base: .token).validate(string) == nil - && SimpleType.process(string, whiteSpace: .collapse) == literal ? .empty : .notAllowed + case let .value(type, literal): + type.valueMatches(string, literal: literal) ? .empty : .notAllowed case let .data(type): type.validate(string) == nil ? .empty : .notAllowed case let .list(inner): listMatches(inner, string) ? .empty : .notAllowed default: .notAllowed diff --git a/Sources/Schema/RelaxNGParser.swift b/Sources/Schema/RelaxNGParser.swift index ebbf205..f67d3e7 100644 --- a/Sources/Schema/RelaxNGParser.swift +++ b/Sources/Schema/RelaxNGParser.swift @@ -134,19 +134,25 @@ private final class RNGCompiler { return .data(PureXML.Schema.SimpleType(base: base, facets: facets)) } - /// Builds a `` pattern. An explicit `type` that the in-scope - /// `datatypeLibrary` does not define makes the value unmatchable; otherwise - /// the literal is compared lexically. + /// Builds a `` pattern carrying its datatype, so the value compares in + /// that type's value space. An untyped `` uses the built-in `token` + /// type; an explicit `type` the in-scope `datatypeLibrary` does not define is + /// an unknown datatype, so the value matches nothing. private func valuePattern(_ node: Tree) -> Pattern { - if hasUnknownType(node) { return .notAllowed } - return .value(RNGNode.text(node)) + guard let type = valueType(node) else { return .notAllowed } + return .value(type, RNGNode.text(node)) } - /// Whether `node` declares a `type` the in-scope `datatypeLibrary` does not - /// define (an untyped `` is always known: it compares lexically). - private func hasUnknownType(_ node: Tree) -> Bool { - guard let declared = RNGNode.attribute(node, "type") else { return false } - return resolvedBuiltin(RNGNode.strip(declared), library: datatypeLibrary(of: node)) == nil + /// The datatype of a ``: `token` when untyped, the library-resolved + /// built-in when typed, or nil when the declared type is unknown. + private func valueType(_ node: Tree) -> PureXML.Schema.SimpleType? { + guard let declared = RNGNode.attribute(node, "type") else { + return PureXML.Schema.SimpleType(base: .token) + } + guard let base = resolvedBuiltin(RNGNode.strip(declared), library: datatypeLibrary(of: node)) else { + return nil + } + return PureXML.Schema.SimpleType(base: base) } /// Resolves a datatype name within a library. The default (empty) library diff --git a/Sources/Schema/RelaxNGPattern.swift b/Sources/Schema/RelaxNGPattern.swift index 0552eaa..2a07ce7 100644 --- a/Sources/Schema/RelaxNGPattern.swift +++ b/Sources/Schema/RelaxNGPattern.swift @@ -36,7 +36,7 @@ public extension PureXML.Schema { case element(NameClass, Pattern) case attribute(NameClass, Pattern) case data(SimpleType) - case value(String) + case value(SimpleType, String) case list(Pattern) case ref(String) case after(Pattern, Pattern) diff --git a/Sources/Schema/SimpleType.swift b/Sources/Schema/SimpleType.swift index 464f4d3..92198b9 100644 --- a/Sources/Schema/SimpleType.swift +++ b/Sources/Schema/SimpleType.swift @@ -109,6 +109,37 @@ public extension PureXML.Schema { return nil } + /// Whether `instance` and `literal` denote the same value in this type's + /// value space, the comparison a RELAX NG `` performs. Ordered + /// types (numeric, date/time) compare in value space, so `1` equals `01` + /// and `1.0` equals `1.00`; booleans treat `1`/`true` and `0`/`false` as + /// equal. Every other type, including `string`/`token`, compares by its + /// whitespace-normalized lexical form. (Durations and binary types fall + /// under the lexical rule, so only their normalized forms compare equal.) + public func valueMatches(_ instance: String, literal: String) -> Bool { + let whiteSpace = facets.whiteSpace ?? base.whiteSpace + let left = Self.process(instance, whiteSpace: whiteSpace) + let right = Self.process(literal, whiteSpace: whiteSpace) + switch base.primitive { + case .decimal, .integer, .double, .float, .dateKind: + guard let leftValue = base.primitive.ordered(left), let rightValue = base.primitive.ordered(right) else { return false } + return leftValue == rightValue + case .boolean: + guard let leftValue = Self.booleanValue(left) else { return false } + return leftValue == Self.booleanValue(right) + default: + return left == right + } + } + + private static func booleanValue(_ value: String) -> Bool? { + switch value { + case "true", "1": true + case "false", "0": false + default: nil + } + } + private func validateUnion(_ lexical: String, members: [SimpleType]) -> String? { for member in members where member.validate(lexical) == nil { return nil diff --git a/Tests/RelaxNGValueSpaceTests.swift b/Tests/RelaxNGValueSpaceTests.swift new file mode 100644 index 0000000..35b38f1 --- /dev/null +++ b/Tests/RelaxNGValueSpaceTests.swift @@ -0,0 +1,100 @@ +@testable import PureXML +import Testing + +@Suite("RELAX NG typed value-space comparison") +struct RelaxNGValueSpaceTests { + private let xsd = "http://www.w3.org/2001/XMLSchema-datatypes" + + private func validXML(_ rng: String, _ xml: String) throws -> Bool { + try PureXML.Schema.RelaxNG(rng).validate(xml) + } + + private func validRNC(_ rnc: String, _ xml: String) throws -> Bool { + try PureXML.Schema.RelaxNG(compact: rnc).validate(xml) + } + + private func valueElement(_ type: String, _ literal: String) -> String { + """ + + \(literal) + + """ + } + + @Test("An integer value matches in value space, not lexically") + func test_integerValueSpace() throws { + let rng = valueElement("integer", "1") + #expect(try validXML(rng, "1")) + #expect(try validXML(rng, "01")) + #expect(try validXML(rng, "+1")) + #expect(try !validXML(rng, "2")) + #expect(try !validXML(rng, "1x")) + } + + @Test("A decimal value matches regardless of trailing zeros") + func test_decimalValueSpace() throws { + let rng = valueElement("decimal", "1.5") + #expect(try validXML(rng, "1.5")) + #expect(try validXML(rng, "1.50")) + #expect(try validXML(rng, "01.500")) + #expect(try !validXML(rng, "1.6")) + } + + @Test("A boolean value treats 1/true and 0/false as equal") + func test_booleanValueSpace() throws { + let trueRNG = valueElement("boolean", "true") + #expect(try validXML(trueRNG, "true")) + #expect(try validXML(trueRNG, "1")) + #expect(try !validXML(trueRNG, "false")) + #expect(try !validXML(trueRNG, "0")) + } + + @Test("A token value still compares by normalized lexical form") + func test_tokenLexical() throws { + // Untyped value defaults to token: whitespace collapses, then compares. + let rng = """ + + yes please + + """ + #expect(try validXML(rng, "yes please")) + #expect(try validXML(rng, " yes please ")) + #expect(try !validXML(rng, "no")) + } + + @Test("A string value preserves and compares exactly after the type's whitespace rule") + func test_stringPreserve() throws { + let rng = valueElement("string", "abc") + #expect(try validXML(rng, "abc")) + #expect(try !validXML(rng, "abcd")) + } + + @Test("Compact syntax typed value compares in value space") + func test_compactIntegerValue() throws { + let rnc = """ + namespace x = "urn:x" + element n { xsd:integer "10" } + """ + #expect(try validRNC(rnc, "10")) + #expect(try validRNC(rnc, "010")) + #expect(try !validRNC(rnc, "11")) + } + + @Test("Compact bare string literal is a token value") + func test_compactBareLiteral() throws { + let rnc = "element flag { \"on\" }" + #expect(try validRNC(rnc, "on")) + #expect(try validRNC(rnc, " on ")) + #expect(try !validRNC(rnc, "off")) + } + + @Test("SimpleType.valueMatches compares numeric value space directly") + func test_valueMatchesUnit() { + let integer = PureXML.Schema.SimpleType(base: .integer) + #expect(integer.valueMatches("007", literal: "7")) + #expect(!integer.valueMatches("7", literal: "8")) + let token = PureXML.Schema.SimpleType(base: .token) + #expect(token.valueMatches(" a b ", literal: "a b")) + #expect(!token.valueMatches("a b", literal: "ab")) + } +}