PureXML/Tests/SchemaTopLevelElementTests.swift
Mihaela Mihaljevic 44d37a1a5f feat(schema): top-level element may not have ref/form/minOccurs/maxOccurs (topLevelElement)
A top-level xs:element declaration (a direct child of schema, or of redefine) was
accepted with ref, form, minOccurs, or maxOccurs. A global element is a
declaration, never a particle or a reference, so those local-use attributes are
prohibited by the schema-for-schemas topLevelElement. The top-level-attribute
applicability scan is generalized into topLevelDeclarationErrors, driven by a
per-kind forbidden-attribute map, covering both attribute and element. A local
element particle inside a content model is unaffected.

XSTS: invalid schemas accepted 424 to 422, no other bucket raised.
2026-06-15 14:39:54 +02:00

39 lines
1.7 KiB
Swift

@testable import PureXML
import Testing
@Suite("XSD top-level element applicability (topLevelElement)")
struct SchemaTopLevelElementTests {
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
}
@Test("A top-level element with ref/form/minOccurs/maxOccurs is rejected")
func test_topLevelForbiddenRejected() {
#expect(!compiles("<xs:element name=\"g\" type=\"xs:string\"/><xs:element ref=\"g\"/>"))
#expect(!compiles("<xs:element name=\"e\" type=\"xs:string\" form=\"qualified\"/>"))
#expect(!compiles("<xs:element name=\"e\" type=\"xs:string\" minOccurs=\"0\"/>"))
#expect(!compiles("<xs:element name=\"e\" type=\"xs:string\" maxOccurs=\"3\"/>"))
}
@Test("A plain top-level element declaration compiles")
func test_plainTopLevelAccepted() {
#expect(compiles("<xs:element name=\"e\" type=\"xs:string\"/>"))
#expect(compiles("<xs:element name=\"e\" type=\"xs:string\" nillable=\"true\" abstract=\"true\"/>"))
}
/// `ref`, `minOccurs`, and `maxOccurs` are legitimate on a LOCAL element
/// particle inside a complex type; the rule applies only to top-level
/// declarations.
@Test("A local element ref with occurrence compiles")
func test_localParticleAccepted() {
#expect(compiles(
"<xs:element name=\"g\" type=\"xs:string\"/>"
+ "<xs:element name=\"e\"><xs:complexType><xs:sequence>"
+ "<xs:element ref=\"g\" minOccurs=\"0\" maxOccurs=\"3\"/>"
+ "</xs:sequence></xs:complexType></xs:element>",
))
}
}