feat(schema): element ref excludes declaration-only attributes and inline type (src-element.2.2)

An element ref carrying nillable/default/fixed/form/block, or an element or
attribute ref carrying an inline type definition, was accepted. A ref is a use
of a global declaration, not a declaration, so beyond ref it admits only
minOccurs, maxOccurs, and id (src-element.2.2). The applicability check already
excluded name and type from a ref; this extends the exclusion to the remaining
declaration-only properties and to an inline simpleType/complexType child.
minOccurs/maxOccurs, a group/attributeGroup ref, and an attribute ref's
use/default/fixed (which override on the use) are unaffected.

XSTS: invalid schemas accepted 439 to 424, no other bucket raised. Disclosed
under-detection: a key/unique/keyref child under a ref is not yet flagged.
This commit is contained in:
Mihaela Mihaljevic 2026-06-15 14:29:13 +02:00
parent be5d260b52
commit 7ed7059134
5 changed files with 53 additions and 2 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- An element `ref` may no longer carry `nillable`, `default`, `fixed`, `form`, or `block`, and an element or attribute `ref` may no longer have an inline type definition (XSD 1.0 `src-element.2.2`, #170). A reference is a use of a global declaration, not a declaration, so beyond `ref` it carries only `minOccurs`, `maxOccurs`, and `id`; the applicability check already excluded `name` and `type` from a `ref`, and this extends the exclusion to the remaining declaration-only properties. A `group`/`attributeGroup` ref and an attribute ref's `use`/`default`/`fixed` (which override on the use) are unaffected. XSTS: invalid schemas accepted 439 to 424, no other bucket raised. Disclosed under-detection: a `key`/`unique`/`keyref` child under a ref is not yet flagged.
- A top-level `xs:attribute` declaration may no longer carry `use`, `form`, or `ref` (XSD 1.0 schema-for-schemas `topLevelAttribute`, #170). Those belong to a local attribute use, not a global declaration; the per-component applicability table is the permissive global/local union and so could not draw the distinction, leaving such schemas accepted. A focused scan of the direct `attribute` children of `schema` (and of `redefine`) supplies the global-only constraint; a local attribute use inside a complex type or attribute group is unaffected. XSTS: invalid schemas accepted 454 to 439, no other bucket raised.
- A `default`/`fixed` value on an element or attribute whose `type` names a user-declared type must now be a valid value of that type (XSD 1.0 `e-props-correct.2` / `a-props-correct.2`, #170). This runs after the named types are compiled, complementing the existing built-in-only check: a `default` failing the type's `pattern`, a malformed value for a type restricting a built-in, or a value invalid against a complex type's simple content is now rejected. The type name is resolved namespace-aware; only a type in the schema's own target namespace is checked (built-ins are handled at structure time; imported, foreign, or inline anonymous types are left alone), and the compiled `SimpleType` validator (the one instance validation uses) performs the check, so whitespace and facet handling match. XSTS: invalid schemas accepted 462 to 454, no other bucket raised.
- A simple-type `restriction` whose base is an inline anonymous `<simpleType>` (no `base` attribute) now resolves that inline child as its base, preserving its list or union variety. The base was read only from the `base` attribute, so an inline list/union base was dropped and the type collapsed to an atomic string: a `length` facet then counted characters instead of list items, and a list-valued `default` of the correct length was wrongly rejected once value constraints are checked. Resolving the inline base fixes both schema-time value checking and instance validation. XSTS: invalid instances accepted 160 to 158, no other bucket raised.

View File

@ -68,6 +68,20 @@ extension PureXML.Schema.XSDParser {
if present.contains("ref"), present.contains("type") {
errors.append("'\(local)' may not have both 'ref' and 'type'")
}
// src-element.2.2: an element `ref` is a use of a global declaration, not a
// declaration, so beyond name/type it may not carry the other
// declaration-only properties (`minOccurs`/`maxOccurs`, the particle's own,
// stay allowed).
if local == "element", present.contains("ref") {
for excluded in ["nillable", "default", "fixed", "form", "block"] where present.contains(excluded) {
errors.append("'element' with a 'ref' may not also specify '\(excluded)'")
}
}
// A `ref` likewise excludes an inline type definition: the type comes from the
// referenced declaration.
if present.contains("ref"), local == "element" || local == "attribute", hasInlineType(node, local) {
errors.append("'\(local)' with a 'ref' may not have an inline type definition")
}
// src-element.3 / src-attribute.3-4: a `type` attribute and an inline
// anonymous type definition are mutually exclusive. An attribute may carry
// only an inline `simpleType`; an element may carry a `simpleType` or

View File

@ -0,0 +1,36 @@
@testable import PureXML
import Testing
@Suite("XSD element-ref applicability (src-element.2.2)")
struct SchemaElementRefApplicabilityTests {
private let xsd = "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
private func compiles(_ body: String) -> Bool {
(try? PureXML.Schema.Document("<xs:schema \(xsd)>\(body)</xs:schema>")) != nil
}
private func refParticle(_ refAttrs: String, inlineType: String = "") -> String {
"<xs:element name=\"Main\" type=\"xs:string\"/>"
+ "<xs:element name=\"e\"><xs:complexType><xs:sequence>"
+ "<xs:element ref=\"Main\" \(refAttrs)>\(inlineType)</xs:element>"
+ "</xs:sequence></xs:complexType></xs:element>"
}
@Test("An element ref carrying a declaration-only attribute is rejected")
func test_refDeclarationAttributesRejected() {
for attr in ["nillable=\"true\"", "default=\"x\"", "fixed=\"x\"", "form=\"qualified\"", "block=\"#all\""] {
#expect(!compiles(refParticle(attr)), "ref with \(attr) should be rejected")
}
}
@Test("An element ref with an inline type is rejected")
func test_refInlineTypeRejected() {
#expect(!compiles(refParticle("", inlineType: "<xs:complexType><xs:sequence/></xs:complexType>")))
}
@Test("An element ref with only occurrence attributes compiles")
func test_refOccurrenceAccepted() {
#expect(compiles(refParticle("minOccurs=\"0\" maxOccurs=\"3\"")))
#expect(compiles(refParticle("")))
}
}

View File

@ -29,7 +29,7 @@ struct XSTSSuiteTests {
/// XSTS` (debug is far slower and the corpus is large). Per-case deviations
/// are written to /tmp/xsts-failures.txt for the burn-down.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 439
private let knownSchemaInvalidAccepted = 424
private let knownInstanceValidRejected = 180
private let knownInstanceInvalidAccepted = 158

View File

@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open.
| Bucket | Count | Reading |
|---|---|---|
| valid-schemas-rejected | **72 -> 4** | production target is 0: rejecting a correct schema is the most user-hostile defect (#148). Particle-restriction over-rejection fixed: pointless `maxOccurs=0` particles removed, content-free restriction of an EMPTY base, empty-derived emptiable-base, and (#163) the pairwise check over the normalized content model with occurrence subsumption applied only to same-kind pairings (cross-kind element/group, group/element, group/wildcard match occurrence inside, by effective total range / member). #164 resolved element-ref namespaces by prefix binding (the `particlesJj/Jk/Jm/Jn/Js`, `particlesQ/R` cluster, -28). Content-free (`maxOccurs=0`) derived groups and substitution-group declaration order closed `particlesW006`/`elemZ027_a`; the W3C MapAndSum rule (Sequence:Choice, #165) closed `particlesV003`. Remaining 1 (`particlesZ001`): a documented spec ambiguity (RecurseAsIfGroup; the reference rejects it), not a defect. |
| **invalid-schemas-accepted** | **2461 -> 439** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion landed). |
| **invalid-schemas-accepted** | **2461 -> 424** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion landed). |
| valid-instances-rejected | **233 -> 180** | (instance side; #164's element-ref namespace fix is shared with instance validation, -34) |
| invalid-instances-accepted | **160 -> 158** | (instance side; resolving an inline `<simpleType>` restriction base preserves list/union variety, so a `length` facet counts items and two more invalid instances are caught) |