From c6305c9d93baf87fd7c411980d3358ace7e6eaa2 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Sat, 13 Jun 2026 16:55:36 +0200 Subject: [PATCH] fix(schema): treat QName length facets as non-constraining XSD 1.0 Datatypes 4.3.1 defines the unit of length only for string, anyURI, binary, and list; for QName it is implementation-defined. We had counted characters, rejecting valid QName values. Like Xerces and the XSTS NIST set we now treat length/minLength/maxLength on QName as always satisfied. Lexical QName validity is still enforced. The suite's contributors disagree here (NIST: non-constraining; Microsoft: a character count), which the spec does not resolve, so a single behavior cannot satisfy both. Net XSTS: valid instances rejected 547 to 502; 4 Microsoft cases move to invalid instances accepted (257 to 261), recorded as a deliberate interpretation in docs/xsts-deviations.md. Refs #147, #129. --- CHANGELOG.md | 1 + Sources/Schema/SimpleType.swift | 5 +++++ Tests/SchemaQNameLengthTests.swift | 34 ++++++++++++++++++++++++++++++ Tests/XSTSSuiteTests.swift | 4 ++-- docs/xsts-deviations.md | 21 +++++++++++++++++- 5 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 Tests/SchemaQNameLengthTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index fc63a28..91898c5 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 +- Length facets on `xs:QName` no longer reject valid values (#147). XSD 1.0 Datatypes 4.3.1 does not define a unit of length for QName (only string, anyURI, binary, and list), so `length`/`minLength`/`maxLength` on QName are implementation-defined; like Xerces and the XSTS NIST set we treat them as non-constraining rather than counting characters. Net XSTS: valid instances rejected 547 to 502 (36+ NIST false rejections fixed); 4 Microsoft cases that read QName length as a character count remain in invalid instances accepted (257 to 261) and are documented as a deliberate spec-ambiguity interpretation in docs/xsts-deviations.md. - Ordering facets on `xs:duration` are now enforced (#147). `minInclusive`, `maxInclusive`, `minExclusive`, and `maxExclusive` on duration were ignored because duration has no total order in the generic comparator. A `DurationValue` now implements the XSD 1.0 Datatypes 3.2.6.2 partial order (compare by adding to the four reference dateTimes); a value incomparable to the bound (such as `P1M` against `P30D`) is outside the range and rejected. XSTS: invalid instances accepted 355 to 257. - Pattern and enumeration facets on a union type are now enforced (#147). A union admits a value valid for any member, but `pattern` and `enumeration` (the only constraining facets XSD allows on a union) had been ignored, so a member-valid value outside the enumeration or failing the pattern was wrongly accepted. Enumeration compares in the value space of the admitting member (so `01` equals an enumerated `1` for an integer member), matching the atomic path. XSTS #147: invalid instances accepted 555 to 355. - Length facets on the built-in list datatypes now count list items, not characters (#146). A restriction of `NMTOKENS`, `IDREFS`, or `ENTITIES` resolved its base through neither the `BuiltinType` table (which has only the singular `NMTOKEN`) nor the user-type table, so it collapsed to an atomic string and `length`/`minLength`/`maxLength` measured characters, rejecting valid instances. The restriction base now resolves through `simpleTypeReference`, preserving the list variety. Closes the XSTS NIST `list-NMTOKENS`/`IDREFS`/`ENTITIES` facet clusters: valid instances rejected 604 to 547, invalid instances accepted 582 to 555. diff --git a/Sources/Schema/SimpleType.swift b/Sources/Schema/SimpleType.swift index de89ad1..1906434 100644 --- a/Sources/Schema/SimpleType.swift +++ b/Sources/Schema/SimpleType.swift @@ -201,6 +201,11 @@ public extension PureXML.Schema { } private func lengthError(_ value: String, _ primitive: Primitive) -> String? { + // length/minLength/maxLength measure characters, octets, or list + // items depending on the type; for QName the spec leaves the unit + // unspecified (XSD 1.0 Datatypes 4.3.1), so like Xerces and the XSTS + // NIST oracle we do not constrain QName values by length. + if case .qName = primitive { return nil } let length = primitive.measuredLength(value) if let exact = facets.length, length != exact { return "length \(length) is not \(exact)" } if let minimum = facets.minLength, length < minimum { return "length \(length) is below \(minimum)" } diff --git a/Tests/SchemaQNameLengthTests.swift b/Tests/SchemaQNameLengthTests.swift new file mode 100644 index 0000000..7c6b111 --- /dev/null +++ b/Tests/SchemaQNameLengthTests.swift @@ -0,0 +1,34 @@ +@testable import PureXML +import Testing + +/// `length`, `minLength`, and `maxLength` on `xs:QName` do not constrain the +/// value: XSD 1.0 Datatypes 4.3.1 leaves the unit of length unspecified for +/// QName, so (like Xerces and the XSTS NIST oracle) any QName satisfies them +/// (#147). Lexical QName validity is still enforced. +@Suite("QName length facets") +struct SchemaQNameLengthTests { + private func schema(_ facet: String) throws -> PureXML.Schema.Document { + try PureXML.Schema.Document(""" + + + + \(facet) + + + + """) + } + + @Test("length, minLength, and maxLength never reject a valid QName") + func test_lengthIsNonConstraining() throws { + #expect(try schema(#""#).validate("abcdefghij").isEmpty) + #expect(try schema(#""#).validate("abcdefghij").isEmpty) + #expect(try schema(#""#).validate("abc").isEmpty) + } + + @Test("Lexical QName validity is still enforced") + func test_qnameStillValidated() throws { + // A leading digit is not a valid NCName, so not a valid QName. + #expect(try !schema(#""#).validate("1bad").isEmpty) + } +} diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 2319ea3..119942f 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 = 75 private let knownSchemaInvalidAccepted = 2467 - private let knownInstanceValidRejected = 547 - private let knownInstanceInvalidAccepted = 257 + private let knownInstanceValidRejected = 502 + private let knownInstanceInvalidAccepted = 261 @Test("Every XSTS case behaves: compile, reject, validate, invalidate") func test_suite() throws { diff --git a/docs/xsts-deviations.md b/docs/xsts-deviations.md index 44f1f59..4c9105c 100644 --- a/docs/xsts-deviations.md +++ b/docs/xsts-deviations.md @@ -86,7 +86,26 @@ own epic, not part of the instance-correctness burn-down. ## Category C: arguable / needs investigation - `test`, `elemT` (invalid instance accepted, ~86): not yet sampled; classify before fixing. -- QName length (listed in A) is spec-murky; it sits in A because NIST and Xerces agree the instance is valid, so rejecting it is the defensible bug. + +## Category D: deliberate interpretations of spec-ambiguous points + +These are resolved, not open: the spec underspecifies the point and the suite's +own contributors disagree, so a single behavior cannot satisfy all of them. We +pick the defensible reading and accept the residual deviation. + +- **QName length facets are non-constraining.** XSD 1.0 Datatypes 4.3.1 defines + the unit of length for string (characters), anyURI (characters), binary + (octets), and list (items), but not for QName, so it is implementation-defined. + NIST (36+ cases) and Xerces treat `length`/`minLength`/`maxLength` on QName as + always satisfied; Microsoft (4 cases: `QName_length001/003`, `QName_minLength003`, + `QName_maxLength001`) treats them as character counts and expects rejection. + These are directly contradictory (NIST marks a 15-character value valid under + `length=1`; MS marks `foofo` invalid under `length=4`). We chose + non-constraining, matching Xerces and the larger NIST set, because a QName's + value is an abstract (namespace, local-name) pair with no defined character + length. Cost: the 4 MS cases remain in "invalid instances accepted". Fixing + the 36+ NIST false rejections (valid documents wrongly rejected) is the more + important direction. ## How a fix lands