fix(schema): reject an xsi:type that names an abstract type; invalid-instances-accepted 34 to 32 (#147)

An abstract type cannot be the type of an instance element (cvc-elt.4.3.2), so an
instance xsi:type naming an abstract type is invalid as a substitution even when
the element's declared type would otherwise admit it (typeDef01101, xsi:type on an
anyType-declared element). The validator only checked for an abstract DECLARED
type with no xsi:type; abstractXsiTypeError now also rejects an abstract xsi:type
substitute (matched against the namespaced abstract-type set). A concrete type is
unaffected.

invalid-instances-accepted 34 to 32, no false positive (valid-instances-rejected
and valid-schemas-rejected held at 0; a valid instance never names an abstract
type via xsi:type), no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-18 15:58:08 +02:00
parent f432254f5c
commit 7e765a3bc1
4 changed files with 57 additions and 2 deletions

View File

@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- XSTS invalid-instances-accepted (#147): an instance `xsi:type` may not name an abstract type. An abstract type cannot be the type of an instance element (cvc-elt.4.3.2), so it is invalid as a substitution even when the declared type would otherwise admit it (typeDef01101); a concrete type is unaffected. This complements the existing check for an abstract DECLARED type with no `xsi:type`. invalid-instances-accepted 34 → 32, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved.
- XSTS invalid-instances-accepted (#147): an element with a `fixed` value constraint and a mixed content type must carry its fixed character value with no element children (cvc-elt.5.2.2.2.1). The fixed check validated only the concatenated text, so a mixed `fixed` element whose text matched but which also contained child elements (ctZ013) was accepted; element children are now rejected. An empty element (which takes the fixed value) and text equal to the fixed value remain valid. invalid-instances-accepted 37 → 34, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved.
- XSTS invalid-instances-accepted (#147): a `simpleContent` derivation whose base is another complex type with simpleContent now resolves through the chain to the underlying simple type, so the element's text is validated against it. A type restricting or extending a complex type that ultimately derives from a built-in (e.g. `Test` restricts `Test2` which extends `xs:int`) validates its content as that built-in rather than as unconstrained string; `baseTD00101`'s non-int text is now rejected. The base resolution recurses through `complexTypeNodes`, accumulating facets, with a cycle guard. invalid-instances-accepted 39 → 37, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved.

View File

@ -17,10 +17,30 @@ extension PureXML.Schema.ComplexValidator {
if let blocked = blockedSubstitutionError(declared: declared, child: child, at: path, namespaceBindings: namespaceBindings) { return blocked }
if let notDerived = notDerivedSubstitutionError(declared: declared, child: child, at: path, namespaceBindings: namespaceBindings) { return notDerived }
if let listUnion = listUnionSubstitutionError(declared: declared, child: child, at: path, namespaceBindings: namespaceBindings) { return listUnion }
if let abstractType = abstractXsiTypeError(child: child, at: path, namespaceBindings: namespaceBindings) { return abstractType }
if case let .typeReference(name) = declared { return abstractTypeError(named: name, child: child, at: path) }
return nil
}
/// The error when an instance `xsi:type` names an ABSTRACT type: an abstract
/// type cannot be the type of an instance element (cvc-elt.4.3.2), so it may not
/// be used as a substitution. Distinct from ``abstractTypeError``, which covers
/// an abstract DECLARED type with no `xsi:type`.
func abstractXsiTypeError(
child: PureXML.Model.Element,
at path: [PureXML.Validation.PathKey],
namespaceBindings: [String: String],
) -> PureXML.Validation.ValidationError? {
guard let label = Self.xsiTypeName(child),
let reference = Self.xsiTypeReference(child, namespaceBindings: namespaceBindings),
abstractTypes.contains(Self.derivationKey(fromReference: reference))
else { return nil }
return PureXML.Validation.ValidationError(
reason: "xsi:type '\(label)' names an abstract type and may not be used as a substitution",
at: path,
)
}
/// cvc-elt.4.3.2.1 for a list or union `xsi:type`: a list or union simple type
/// is derived only from `anySimpleType`, so it can validly stand in only for an
/// element whose declared type is a ur-type (`anySimpleType`/`anyType`). Naming a

View File

@ -0,0 +1,33 @@
@testable import PureXML
import Testing
/// An `xsi:type` may not name an abstract type: an abstract type cannot be the
/// type of an instance element (cvc-elt.4.3.2), so it is invalid as a substitution
/// even when the declared type would otherwise admit it. A concrete derived type
/// is accepted.
@Suite("abstract xsi:type")
struct SchemaAbstractXsiTypeTests {
private func schema() throws -> PureXML.Schema.Document {
try PureXML.Schema.Document("""
<xs:schema xmlns="u" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="u">
<xs:element name="root"/>
<xs:complexType name="Real"/>
<xs:complexType name="Virtual" abstract="true"/>
</xs:schema>
""")
}
private func doc(_ type: String) -> String {
#"<root xmlns="u" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="\#(type)"/>"#
}
@Test("an abstract xsi:type is rejected")
func test_abstractRejected() throws {
#expect(try !schema().validate(doc("Virtual")).isEmpty)
}
@Test("a concrete xsi:type is accepted")
func test_concreteAccepted() throws {
#expect(try schema().validate(doc("Real")).isEmpty)
}
}

View File

@ -150,13 +150,13 @@ struct XSTSSuiteTests {
private let knownSchemaValidRejected = 0
private let knownSchemaInvalidAccepted = 69
private let knownInstanceValidRejected = 0
/// Instance bucket (133 -> 34), per-step deltas in CHANGELOG.md: xsi:type must derive from the
/// Instance bucket (133 -> 32), per-step deltas in CHANGELOG.md: xsi:type must derive from the
/// declared type (complex, then atomic and list/union, resolved through the element-ref chain);
/// block/blockDefault (incl. "substitution" and an untyped head) bar substitutions after the
/// derivation backbone was re-keyed by namespace; ur-type `xsd:anyType` wildcards are `lax`;
/// identity fields compare in the decimal value space and take a declared default/fixed when
/// absent or empty; a complex type's attribute wildcard is the INTERSECTION of its sources.
private let knownInstanceInvalidAccepted = 34
private let knownInstanceInvalidAccepted = 32
@Test("Every XSTS case behaves: compile, reject, validate, invalidate")
func test_suite() throws {