A complex or simple type may not derive, transitively through its base, from itself. Such schemas were accepted. The check walks the base chain built from each type's complexContent/simpleContent extension/restriction base (and a simpleType's restriction base), following an edge only when the base resolves to the schema's own target namespace. Three exceptions keep it from over-rejecting: element content that recurses (an element of the type's own type) is not a derivation and is not followed; a type redefined in xs:redefine legitimately names itself as its base, so redefined types end the chain; and the ur-types (anyType/anySimpleType/anyAtomicType, which the schema-for-schemas bootstraps with self-referential definitions) end the chain. The target-namespace gate prevents a local type whose base is a same-local-name type in another namespace from being mistaken for a self-cycle (found by an adversarial critic; the engine resolves by local name). XSTS: invalid schemas accepted 519 to 516 (addB101, elemM003, xsd007.e now rejected), no other bucket raised. Union/list and genuine cross-namespace derivation cycles are a disclosed under-rejection.
54 lines
3.0 KiB
Swift
54 lines
3.0 KiB
Swift
extension PureXML.Schema.XSDParser {
|
|
/// Schema-validity findings for the `id` attributes in a schema document. The
|
|
/// `id` attribute on any XSD component is of type `xs:ID`, so each value must
|
|
/// be a valid NCName and all values must be unique within the document
|
|
/// (XSD Structures: `xs:ID` and the ID/IDREF constraints). The values were
|
|
/// never checked, so a malformed (`id=""`, `id="123"`) or duplicated `id` left
|
|
/// the schema wrongly accepted.
|
|
///
|
|
/// Walks the one document rooted at `schema`; an included or imported document
|
|
/// keeps its own `id` scope, so cross-document collisions are not flagged here.
|
|
static func idAttributeErrors(_ schema: XSDTree) -> [String] {
|
|
var errors: [String] = []
|
|
var seen: Set<String> = []
|
|
collectIDErrors(schema, into: &errors, seen: &seen)
|
|
return errors
|
|
}
|
|
|
|
private static func collectIDErrors(_ node: XSDTree, into errors: inout [String], seen: inout Set<String>) {
|
|
// `appinfo` and `documentation` hold arbitrary foreign content whose own
|
|
// `id` attributes are not xs:ID; do not descend into them.
|
|
let local = PureXML.Schema.XSDNode.localName(node)
|
|
if local == "appinfo" || local == "documentation" { return }
|
|
if let id = unprefixedID(node) {
|
|
if !PureXML.Schema.Lexical.isNCName(id) {
|
|
errors.append("id attribute value '\(id)' is not a valid NCName")
|
|
} else if !seen.insert(id).inserted {
|
|
errors.append("duplicate id attribute value '\(id)' in the schema document")
|
|
}
|
|
}
|
|
for child in PureXML.Schema.XSDNode.elementChildren(node) {
|
|
collectIDErrors(child, into: &errors, seen: &seen)
|
|
}
|
|
}
|
|
|
|
/// The value of the unprefixed, no-namespace `id` attribute (the xs:ID one),
|
|
/// or nil. A prefixed attribute such as `xml:id` or a foreign `pre:id` is a
|
|
/// different attribute and is not the schema-component identifier.
|
|
private static func unprefixedID(_ node: XSDTree) -> String? {
|
|
node.attributes.first { $0.name.prefix == nil && $0.name.localName == "id" }?.value
|
|
}
|
|
|
|
/// All compile-time schema-consistency findings, collected together so they are
|
|
/// reported in one pass: `id` validity, schema-for-schemas structure, global
|
|
/// component-name uniqueness, content-model determinism (UPA), and circular
|
|
/// type derivation. The explicit return type keeps the type-checker from
|
|
/// inferring a long concatenation.
|
|
static func consistencyErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext, _ containers: [XSDTree]) -> [String] {
|
|
let structural = idAttributeErrors(schema) + structureErrors(schema) + componentNameErrors(schema)
|
|
let determinism = PureXML.Schema.ContentModelDeterminism.violations(in: schema, context: context)
|
|
let cycles = derivationCycleErrors(containers, context.namespaceBindings, context.targetNamespace)
|
|
return structural + determinism + cycles
|
|
}
|
|
}
|