An <xs:element> or <xs:attribute> may not carry both a type attribute and an inline anonymous type definition (src-element.3 / src-attribute.3.1): an attribute may have only an inline simpleType, an element a simpleType or complexType. Such schemas were accepted. Only an inline type in the XSD namespace counts; a foreign-namespace child whose local name happens to be complexType/simpleType is not the component's own type definition (foreign content is not structurally checked), so it is not flagged (an adversarial critic's finding, fixed with the namespace guard and a test). The attribute-applicability function and its allowed-attributes table were extracted to SchemaAttributeApplicability.swift (a pure move) to keep SchemaStructure.swift within the file-length limit. XSTS: invalid schemas accepted 495 to 489 (addB081, attKa011, attKb014, attKc014, typedef00501m2, typedef00502m2 now rejected), no other bucket raised.
42 lines
2.4 KiB
Swift
42 lines
2.4 KiB
Swift
@testable import PureXML
|
|
import Testing
|
|
|
|
@Suite("XSD type attribute excludes an inline type")
|
|
struct SchemaTypeExclusivityTests {
|
|
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("type attribute and an inline type definition are mutually exclusive")
|
|
func test_typeAndInlineTypeRejected() {
|
|
// Element with both type= and an inline simpleType (src-element.3).
|
|
#expect(!compiles("<xs:element name=\"e\" type=\"xs:string\"><xs:simpleType>"
|
|
+ "<xs:restriction base=\"xs:string\"/></xs:simpleType></xs:element>"))
|
|
// Element with both type= and an inline complexType.
|
|
#expect(!compiles("<xs:element name=\"e\" type=\"xs:string\"><xs:complexType><xs:sequence/></xs:complexType></xs:element>"))
|
|
// Attribute with both type= and an inline simpleType (src-attribute.3.1).
|
|
#expect(!compiles("<xs:complexType name=\"T\"><xs:attribute name=\"a\" type=\"xs:string\">"
|
|
+ "<xs:simpleType><xs:restriction base=\"xs:string\"/></xs:simpleType></xs:attribute></xs:complexType>"))
|
|
}
|
|
|
|
@Test("type alone, or an inline type alone, compiles")
|
|
func test_eitherAloneAccepted() {
|
|
#expect(compiles("<xs:element name=\"e\" type=\"xs:string\"/>"))
|
|
#expect(compiles("<xs:element name=\"e\"><xs:simpleType><xs:restriction base=\"xs:string\"/></xs:simpleType></xs:element>"))
|
|
#expect(compiles("<xs:complexType name=\"T\"><xs:attribute name=\"a\" type=\"xs:string\"/></xs:complexType>"))
|
|
// type= alongside an identity constraint (not a type) is legal.
|
|
#expect(compiles("<xs:element name=\"e\" type=\"xs:string\"><xs:unique name=\"u\"><xs:selector xpath=\".\"/><xs:field xpath=\"@x\"/></xs:unique></xs:element>"))
|
|
}
|
|
|
|
@Test("A foreign-namespace child named simpleType/complexType is not the component's inline type")
|
|
func test_foreignNamespaceChildNotInlineType() {
|
|
// A foreign child whose local name happens to be complexType is not an XSD
|
|
// inline type, so type= alongside it is not the type/inline-type clash.
|
|
let schema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:foo=\"urn:foo\">"
|
|
+ "<xs:element name=\"e\" type=\"xs:string\"><foo:complexType/></xs:element></xs:schema>"
|
|
#expect((try? PureXML.Schema.Document(schema)) != nil)
|
|
}
|
|
}
|