PureXML/Tests/SchemaTopLevelAttributeTests.swift
Mihaela Mihaljevic be5d260b52 feat(schema): top-level attribute may not have use/form/ref (topLevelAttribute)
A top-level xs:attribute declaration (a direct child of schema, or of redefine)
was accepted with use, form, or ref. Those belong to a local attribute use, not
a global declaration: the schema-for-schemas topLevelAttribute marks ref, form,
and use as prohibited, allowing only default, fixed, id, name, type and an inline
simpleType. The per-component applicability table is the permissive global/local
union, so it could not draw the distinction; a focused scan of the direct
attribute children of schema and redefine supplies the global-only constraint. A
local attribute use inside a complex type or attribute group is unaffected.

The existing test_validAttributeValues asserted a top-level attribute with use
compiles, which is itself invalid; corrected to a local attribute use.

XSTS: invalid schemas accepted 454 to 439, no other bucket raised.
2026-06-15 14:18:25 +02:00

48 lines
2.0 KiB
Swift

@testable import PureXML
import Testing
@Suite("XSD top-level attribute applicability (topLevelAttribute)")
struct SchemaTopLevelAttributeTests {
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 attribute with use is rejected")
func test_topLevelUseRejected() {
#expect(!compiles("<xs:attribute name=\"a\" type=\"xs:string\" use=\"required\"/>"))
#expect(!compiles("<xs:attribute name=\"a\" type=\"xs:string\" use=\"optional\"/>"))
#expect(!compiles("<xs:attribute name=\"a\" type=\"xs:string\" use=\"prohibited\"/>"))
}
@Test("A top-level attribute with form is rejected")
func test_topLevelFormRejected() {
#expect(!compiles("<xs:attribute name=\"a\" type=\"xs:string\" form=\"qualified\"/>"))
}
@Test("A plain top-level attribute declaration compiles")
func test_plainTopLevelAccepted() {
#expect(compiles("<xs:attribute name=\"a\" type=\"xs:string\"/>"))
#expect(compiles("<xs:attribute name=\"a\" type=\"xs:string\" default=\"x\"/>"))
}
/// `use`, `form`, and a `ref` are legitimate on a LOCAL attribute use inside a
/// complex type; the rule applies only to top-level declarations.
@Test("A local attribute use with use/form compiles")
func test_localUseAccepted() {
#expect(compiles(
"<xs:element name=\"e\"><xs:complexType>"
+ "<xs:attribute name=\"a\" type=\"xs:string\" use=\"required\" form=\"qualified\"/>"
+ "</xs:complexType></xs:element>",
))
// A local ref with use is fine.
#expect(compiles(
"<xs:attribute name=\"g\" type=\"xs:string\"/>"
+ "<xs:element name=\"e\"><xs:complexType>"
+ "<xs:attribute ref=\"g\" use=\"required\"/>"
+ "</xs:complexType></xs:element>",
))
}
}