fix(schema): a local element must have a name or a ref; invalid-schemas-accepted 80 to 79 (#145)

A local element declaration (one not a child of schema) must identify itself
with a name or a ref; an xs:element with neither is invalid. localElementErrors
now flags it, alongside the existing local-element forbidden-attribute checks.

XSTS: invalid-schemas-accepted 80 to 79 (elemP005), valid-schemas-rejected held
at 0, no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-18 00:34:50 +02:00
parent 75dd044d93
commit c45e1eb856
4 changed files with 26 additions and 26 deletions

View File

@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- XSTS invalid-schemas-accepted (#145): a local `element` declaration must identify itself with a `name` or a `ref`; an element with neither is rejected. invalid-schemas-accepted 80 → 79, no false positive (valid-schemas-rejected held at 0), no other bucket moved.
- XSTS invalid-schemas-accepted (#145): a `complexType` nested in an `element` is a local, anonymous type and may not carry a `name` attribute; only a top-level `complexType` (or a `redefine` redefinition) is named. invalid-schemas-accepted 81 → 80, no false positive (valid-schemas-rejected held at 0), no other bucket moved.
- XSTS invalid-schemas-accepted (#161): a type inside `xs:redefine` must restrict or extend the type it redefines, which lives in the redefining schema's own target namespace (`src-redefine.5`). A derivation base bound to a different namespace, such as an imported type that happens to share the redefined type's local name, is not a self-redefinition and is now rejected; the previous check compared local names only and accepted it. A base with no resolvable namespace is left alone, so an unprefixed or own-namespace self-reference is unaffected. invalid-schemas-accepted 84 → 81, no false positive (valid-schemas-rejected held at 0; differential PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved.

View File

@ -169,6 +169,9 @@ extension PureXML.Schema.XSDParser {
PureXML.Schema.XSDNode.localName(parent) != "schema"
else { return [] }
var errors: [String] = []
if PureXML.Schema.XSDNode.attribute(node, "name") == nil, PureXML.Schema.XSDNode.attribute(node, "ref") == nil {
errors.append("a local element declaration must have a 'name' or a 'ref'")
}
for forbidden in ["abstract", "final", "substitutionGroup"] where hasUnprefixed(node, forbidden) {
errors.append("a local element declaration may not specify '\(forbidden)'")
}

View File

@ -372,23 +372,21 @@ struct SchemaStructureOrderTests {
"""#))
}
/// A local element declaration must identify itself with a name or a ref; an
/// element with neither is invalid, while a named one is valid.
@Test("a local element must have a name or a ref")
func test_localElementNameOrRef() {
#expect(rejects(#"<xs:complexType name="t"><xs:all><xs:element/></xs:all></xs:complexType>"#))
#expect(!rejects(#"<xs:complexType name="t"><xs:all><xs:element name="a" type="xs:string"/></xs:all></xs:complexType>"#))
}
/// A complexType nested in an element is a local (anonymous) type and must not
/// carry a name; only a top-level complexType is named.
@Test("a local complexType may not have a name")
func test_localComplexTypeMustBeAnonymous() {
#expect(rejects(#"""
<xs:element name="e">
<xs:complexType name="fooType">
<xs:sequence><xs:element name="c" type="xs:string"/></xs:sequence>
</xs:complexType>
</xs:element>
"""#))
// A top-level named complexType, and an anonymous local one, are both valid.
#expect(rejects(#"<xs:element name="e"><xs:complexType name="fooType"><xs:sequence/></xs:complexType></xs:element>"#))
// A named top-level complexType and an anonymous local one are both valid.
try? compile(#"<xs:complexType name="t"><xs:sequence/></xs:complexType>"#)
#expect(!rejects(#"""
<xs:element name="e">
<xs:complexType><xs:sequence><xs:element name="c" type="xs:string"/></xs:sequence></xs:complexType>
</xs:element>
"""#))
#expect(!rejects(#"<xs:element name="e"><xs:complexType><xs:sequence/></xs:complexType></xs:element>"#))
}
}

View File

@ -141,20 +141,17 @@ struct XSTSSuiteTests {
/// gap) is accepted, 132 → 133. Per the production standard over-rejection is the
/// non-starter and under-rejection is recoverable; this debt is recovered by the
/// full effective-total-range Particle-Valid-Restriction (tracked).
/// Six further rules, each with valid-schemas-rejected held at 0 (see CHANGELOG
/// for detail): model-group occurrence ranges (p-props-correct.1, cos-all-limited.2)
/// 130->110; identity-constraint content model 110->104; derivation base-type kind
/// (src-ct.1/.2, local type beats the built-in reading) 104->99; element value
/// constraint needs simple/mixed content (cos-valid-default) 99->96; an attribute's
/// type is a simple type (au-props-correct) 96->95; a resolved include/import/
/// redefine schemaLocation must be a well-formed schema (src-resolve) 95->87; and
/// an import's namespace must equal the imported targetNamespace (src-import.3)
/// 87->84; and a redefined type must derive from the type it redefines in the
/// redefining schema's own namespace, not a same-named foreign one (src-redefine.5)
/// 84->81; and a complexType nested in an element must be anonymous (no name)
/// 81->80.
/// Further rules (each with valid-schemas-rejected held at 0; see CHANGELOG for
/// detail) drove invalid-schemas-accepted 130 -> 79: model-group occurrence ranges
/// (p-props-correct.1, cos-all-limited.2), identity-constraint content model,
/// derivation base-type kind (src-ct.1/.2), element value-constraint content type
/// (cos-valid-default), attribute type must be simple (au-props-correct), resolved
/// schemaLocation must be a schema (src-resolve), import namespace matches the
/// imported target (src-import.3), redefined type derives from itself
/// (src-redefine.5), local complexType is anonymous, and a local element has a
/// name or ref.
private let knownSchemaValidRejected = 0
private let knownSchemaInvalidAccepted = 80
private let knownSchemaInvalidAccepted = 79
private let knownInstanceValidRejected = 0
private let knownInstanceInvalidAccepted = 133