fix(schema): allow character data in a mixed type with no content model

A complexType with mixed="true" and no sequence/choice/all (only attributes)
was compiled to the empty content type, which forbids text, so a mixed element
carrying only character data was rejected. It is now mixed content over an empty
particle: text is permitted, child elements are still disallowed. Non-mixed
empty types are unchanged.

XSTS: valid instances rejected 311 to 308. Baseline updated; regression tests
cover text accepted, an empty element, a child element rejected, and the
non-mixed control still rejecting text.

Refs #147, #129.
This commit is contained in:
Mihaela Mihaljevic 2026-06-13 19:36:37 +02:00
parent 9ff7ed360f
commit b83948b09b
4 changed files with 44 additions and 2 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- A `mixed="true"` complex type with no content model now permits character data (#147). Such a type (only attributes, no `sequence`/`choice`/`all`) was compiled to the empty content type, which forbids text, so a mixed element carrying only text was rejected. It is now mixed content over an empty particle: text is allowed, child elements are still not. XSTS: valid instances rejected 311 to 308.
- `<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.)

View File

@ -148,7 +148,14 @@ extension PureXML.Schema {
}
let wildcard = attributeWildcard(under: node, context)
guard let particle = modelGroup(in: node, context) else {
return ComplexType(attributes: attributes, attributeWildcard: wildcard, content: .empty)
// A mixed type with no content model still permits character data
// (just no child elements): mixed content over an empty particle,
// not the empty content type, which forbids text.
guard mixed else {
return ComplexType(attributes: attributes, attributeWildcard: wildcard, content: .empty)
}
let emptyParticle = Particle(term: .group(.init(compositor: .sequence, particles: [])))
return ComplexType(attributes: attributes, attributeWildcard: wildcard, content: .mixed(emptyParticle))
}
return ComplexType(attributes: attributes, attributeWildcard: wildcard, content: mixed ? .mixed(particle) : .elementOnly(particle))
}

View File

@ -0,0 +1,34 @@
@testable import PureXML
import Testing
/// A `mixed="true"` complex type with no content model (only attributes) still
/// permits character data: it is mixed content over an empty particle, not the
/// empty content type, which forbids text (#147, XSTS attgD set). Child elements
/// are still not allowed, and a non-mixed empty type still rejects text.
@Suite("Mixed content with no element model")
struct SchemaMixedEmptyTests {
private func schema(mixed: Bool) throws -> PureXML.Schema.Document {
try PureXML.Schema.Document("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doc">
<xs:complexType\(mixed ? " mixed=\"true\"" : "")>
<xs:attribute name="a" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
""")
}
@Test("A mixed empty type permits text and stays empty of elements")
func test_mixedAllowsText() throws {
let doc = try schema(mixed: true)
#expect(try doc.validate(#"<doc a="x">hello</doc>"#).isEmpty) // text allowed
#expect(try doc.validate(#"<doc a="x"/>"#).isEmpty) // empty allowed
#expect(try !doc.validate(#"<doc a="x"><child/></doc>"#).isEmpty) // no element content
}
@Test("A non-mixed empty type still rejects text")
func test_nonMixedRejectsText() throws {
#expect(try !schema(mixed: false).validate(#"<doc a="x">hello</doc>"#).isEmpty)
}
}

View File

@ -30,7 +30,7 @@ struct XSTSSuiteTests {
/// are written to /tmp/xsts-failures.txt for the burn-down.
private let knownSchemaValidRejected = 72
private let knownSchemaInvalidAccepted = 2461
private let knownInstanceValidRejected = 311
private let knownInstanceValidRejected = 308
private let knownInstanceInvalidAccepted = 165
@Test("Every XSTS case behaves: compile, reject, validate, invalidate")