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.
This commit is contained in:
Mihaela Mihaljevic 2026-06-20 14:11:56 +02:00
parent 4735acbf03
commit d8794579db
6 changed files with 159 additions and 3 deletions

View File

@ -28,6 +28,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- An attribute declaration may no longer land in the XSI namespace (XSD 1.0 Schema
Component Constraint "xsi: Not Allowed", §3.2.6). A schema may target the XSI
namespace 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 now rejected (corpus
attKb018a, attKa015) while unqualified ones stay valid (attKb018, attKc018). XSTS
invalid-schemas-accepted 39 -> 37, valid-schemas-rejected held at 0, no other
bucket moved.
- A prefixed reference (`type`/`base`/`itemType`/`ref`/`substitutionGroup`/`memberTypes`)
whose namespace is never imported is now rejected on the cross-document skip path
(XSD `src-resolve`). When a schema declares imports but loads no external document,

View File

@ -245,7 +245,7 @@ extension PureXML.Validation {
nonReference: [
AnyValidation(idAttributesValid), AnyValidation(schemaStructureValid),
AnyValidation(componentNamesUnique), AnyValidation(simpleTypeFinalControlsValid),
AnyValidation(contentModelsDeterministic),
AnyValidation(contentModelsDeterministic), AnyValidation(attributeDeclarationsNotInXSI),
AnyValidation(referencedSchemasResolveToSchemas), AnyValidation(redefinitionsDeriveFromThemselves),
AnyValidation(redefineSelfReferencesAreWellFormed), AnyValidation(redefinedAttributeGroupsKeepRequired),
],
@ -341,6 +341,16 @@ extension PureXML.Validation {
}
extension PureXML.Validation.SchemaCompile {
/// xsi: Not Allowed (XSD 1.0 SCC §3.2.6): no attribute declaration lands in the
/// XSI namespace. See ``PureXML/Schema/XSDParser/xsiNamespaceAttributeErrors``.
static var attributeDeclarationsNotInXSI: PureXML.Validation.Validation<PureXML.Schema.SchemaCompileRoot, PureXML.Schema.SchemaCompileContext> {
compileRule("Attribute declarations are not in the XSI namespace") { document in
PureXML.Schema.SchemaLocatedFinding.unlocated(
PureXML.Schema.XSDParser.xsiNamespaceAttributeErrors(document.schema, document.context),
)
}
}
/// au-props-correct: an attribute's type is a simple type, never complex.
static var attributeTypesSimple: PureXML.Validation.Validation<PureXML.Schema.SchemaCompileRoot, PureXML.Schema.SchemaCompileContext> {
compileRule("An attribute's type is a simple type") { document in

View File

@ -0,0 +1,42 @@
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)
}
}
}

View File

@ -0,0 +1,94 @@
import Testing
@testable import PureXML
/// XSD 1.0 Schema Component Constraint "xsi: Not Allowed" (#183): 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 or qualified attribute would land in XSI and is forbidden. Mirrors
/// corpus attKb018a (invalid) against attKb018 / attKc018 (valid).
@Suite("XSD xsi: Not Allowed for attribute declarations (#183)")
struct SchemaXSIAttributeTests {
private let xsi = "http://www.w3.org/2001/XMLSchema-instance"
private func compiles(_ schema: String) -> Bool {
(try? PureXML.Schema.Document(schema)) != nil
}
@Test("attKb018a: attributeFormDefault=qualified forces attributes into XSI: rejected")
func test_qualifiedAttributeIntoXSIRejected() {
let schema = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="\(xsi)" xmlns:x="\(xsi)" attributeFormDefault="qualified">
<xsd:attributeGroup name="attG">
<xsd:attribute name="aga1"/>
<xsd:attribute name="aga2"/>
</xsd:attributeGroup>
<xsd:complexType name="attRef">
<xsd:attributeGroup ref="x:attG"/>
</xsd:complexType>
<xsd:element name="doc" type="x:attRef"/>
</xsd:schema>
"""
#expect(!compiles(schema), "qualified attributes land in XSI; xsi: Not Allowed")
}
@Test("attKb018: same schema with unqualified attributes (no-namespace) stays valid")
func test_unqualifiedAttributesValid() {
let schema = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="\(xsi)" xmlns:x="\(xsi)">
<xsd:attributeGroup name="attG">
<xsd:attribute name="aga1"/>
<xsd:attribute name="aga2"/>
</xsd:attributeGroup>
<xsd:complexType name="attRef">
<xsd:attributeGroup ref="x:attG"/>
</xsd:complexType>
<xsd:element name="doc" type="x:attRef"/>
</xsd:schema>
"""
#expect(compiles(schema), "unqualified attributes land in no namespace, not XSI")
}
@Test("attKc018: local unqualified attributes targeting XSI stay valid")
func test_localUnqualifiedAttributesValid() {
let schema = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="\(xsi)" xmlns:x="\(xsi)">
<xsd:complexType name="attRef">
<xsd:attribute name="ca1"/>
<xsd:attribute name="ca2"/>
</xsd:complexType>
<xsd:element name="doc" type="x:attRef"/>
</xsd:schema>
"""
#expect(compiles(schema), "local unqualified attributes are in no namespace")
}
@Test("a single local attribute with form=qualified into XSI is rejected")
func test_localFormQualifiedIntoXSIRejected() {
let schema = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="\(xsi)">
<xsd:complexType name="attRef">
<xsd:attribute name="ca1" form="qualified"/>
</xsd:complexType>
<xsd:element name="doc" type="attRef"/>
</xsd:schema>
"""
#expect(!compiles(schema), "form=qualified puts ca1 into XSI; xsi: Not Allowed")
}
@Test("the same qualified attribute targeting a non-XSI namespace is valid")
func test_qualifiedAttributeOutsideXSIValid() {
let schema = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:ok" attributeFormDefault="qualified">
<xsd:complexType name="attRef">
<xsd:attribute name="ca1"/>
</xsd:complexType>
<xsd:element name="doc" type="attRef"/>
</xsd:schema>
"""
#expect(compiles(schema), "qualifying into a non-XSI namespace is fine")
}
}

View File

@ -90,6 +90,7 @@ struct ValidationConfigurationTests {
"Global schema component names are unique within their symbol spaces",
"Simple-type final controls are declared consistently",
"Schema content models are deterministic (UPA)",
"Attribute declarations are not in the XSI namespace",
"A resolved schemaLocation reference is a valid schema",
"A redefined type restricts or extends itself",
"A redefined group or attribute group has a well-formed self-reference",

View File

@ -146,9 +146,9 @@ struct XSTSSuiteTests {
/// model-group occurrence, identity-constraint content, derivation base kind,
/// attribute/simple-type rules, composition, simpleContent inline-type checks,
/// complexContent mixedness, and substitution-group anySimpleType checks.
/// Substitution barriers and element-ref metadata moved 45 -> 43; src-resolve unresolvable foreign-ref (#185) 43 -> 39.
/// Substitution barriers and element-ref metadata 45 -> 43; src-resolve unresolvable foreign-ref (#185) 43 -> 39; xsi: Not Allowed attr decls (#183) 39 -> 37.
private let knownSchemaValidRejected = 0
private let knownSchemaInvalidAccepted = 39
private let knownSchemaInvalidAccepted = 37
private let knownInstanceValidRejected = 0
/// Instance bucket (133 -> 26), per-step deltas in CHANGELOG.md: xsi:type must derive from the
/// declared type (complex/atomic/list/union via the element-ref chain); anyType cannot stand in