PureXML/Sources/Schema/SchemaXSIAttribute.swift
Mihaela Mihaljevic d8794579db fix(schema): forbid attribute declarations in the XSI namespace
XSD 1.0 Schema Component Constraint "xsi: Not Allowed" (§3.2.6): an attribute
declaration's {target namespace} must not be the XSI namespace. A schema may
target XSI and declare unqualified attributes (they land in no namespace),
but a top-level attribute, or a local one made qualified by form="qualified"
or attributeFormDefault="qualified", lands in XSI and is forbidden.

Add xsiNamespaceAttributeErrors, scoped to schemas whose target namespace is
XSI, registered as the pre-compile rule attributeDeclarationsNotInXSI. It
rejects corpus attKb018a (qualified attribute-group attributes) and attKa015
(top-level attributes), while attKb018 and attKc018 (local unqualified) stay
valid.

XSTS invalid-schemas-accepted 39 -> 37; valid-schemas-rejected held at 0; no
other bucket moved.
2026-06-20 14:33:14 +02:00

43 lines
2.5 KiB
Swift

extension PureXML.Schema.XSDParser {
private static let xsiInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"
/// XSD 1.0 Schema Component Constraint "xsi: Not Allowed" (§3.2.6): an attribute
/// declaration's {target namespace} must not be the XSI namespace. A schema may
/// target XSI and declare attributes there as long as they stay unqualified, so
/// they land in no namespace (corpus attKb018, attKc018: valid). A top-level
/// attribute, or a local one made qualified by `form="qualified"` or
/// `attributeFormDefault="qualified"`, lands in the target namespace; when that is
/// XSI it is forbidden (corpus attKb018a: `attributeFormDefault="qualified"` puts
/// its attribute-group attributes into XSI). The check is scoped to schemas whose
/// target namespace is XSI, the only documents that can place an attribute there.
static func xsiNamespaceAttributeErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [String] {
guard context.targetNamespace == xsiInstanceNamespace else { return [] }
let attributeFormDefault = PureXML.Schema.XSDNode.attribute(schema, "attributeFormDefault")
var errors: [String] = []
collectXSIAttributeErrors(schema, parentIsSchema: false, attributeFormDefault: attributeFormDefault, into: &errors)
return errors
}
private static func collectXSIAttributeErrors(
_ node: XSDTree,
parentIsSchema: Bool,
attributeFormDefault: String?,
into errors: inout [String],
) {
let local = PureXML.Schema.XSDNode.localName(node)
if local == "appinfo" || local == "documentation" { return }
let isSchema = node.name?.namespaceURI == xsdNamespace && local == "schema"
let isAttribute = node.name?.namespaceURI == xsdNamespace && local == "attribute"
if isAttribute, PureXML.Schema.XSDNode.attribute(node, "ref") == nil, let name = PureXML.Schema.XSDNode.attribute(node, "name") {
let form = PureXML.Schema.XSDNode.attribute(node, "form")
let qualified = parentIsSchema || form == "qualified" || (form == nil && attributeFormDefault == "qualified")
if qualified {
errors.append("attribute declaration '\(name)' is in the XSI namespace, which is not allowed (xsi: Not Allowed)")
}
}
for child in PureXML.Schema.XSDNode.elementChildren(node) {
collectXSIAttributeErrors(child, parentIsSchema: isSchema, attributeFormDefault: attributeFormDefault, into: &errors)
}
}
}