fix(schema): an ID-derived attribute or element may not have a value constraint

An attribute or element whose type is or derives from xs:ID may not carry a
default or fixed value (a-props-correct.3 / e-props-correct.5). Such schemas
were accepted.

The check runs after named simple types are resolved, so a named user type
derived from xs:ID is recognized (not just the built-in and inline forms). The
type reference's namespace is resolved from its prefix: only an ID in the XSD
namespace is the built-in, and a user type is ID only when it resolves to one
whose base is xs:ID. An adversarial critic caught a namespace-blind over-
rejection (a user type named ID, or an imported foo:ID, mistaken for the
built-in); the namespace resolution fixes it, with a regression test.

XSTS: invalid schemas accepted 489 to 481 (attZ013a/b, elemZ032a/b,
valueconstraint01001m2/m3/m5/m6 now rejected), no other bucket raised.
This commit is contained in:
Mihaela Mihaljevic 2026-06-15 11:41:26 +02:00
parent 5dc06137c9
commit 4996d75879
6 changed files with 112 additions and 3 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- An attribute or element whose type is or derives from `xs:ID` may no longer carry a `default` or `fixed` value constraint (XSD 1.0 `a-props-correct.3` / `e-props-correct.5`, #170). Such schemas were accepted. The check runs after named simple types resolve, so a named user type derived from `xs:ID` is recognized; the type reference's namespace is resolved from its prefix, so a user type named `ID` or an imported `foo:ID` is not mistaken for the built-in. XSTS: invalid schemas accepted 489 to 481, no other bucket raised.
- A `type` attribute and an inline anonymous type definition are now mutually exclusive on an element or attribute (XSD 1.0 `src-element.3` / `src-attribute.3.1`, #170). An `<xs:element>` or `<xs:attribute>` carrying both a `type=` and an inline `<simpleType>` (or `<complexType>`, for an element) is rejected; previously it was accepted. Only an inline type in the XSD namespace counts, so a foreign-namespace child that happens to be named `complexType`/`simpleType` is not mistaken for the component's own type. XSTS: invalid schemas accepted 495 to 489, no other bucket raised.
- Attribute-use uniqueness and the single-ID rule are now enforced (XSD 1.0 `ct-props-correct.4` / `ag-props-correct.2` / `ct-props-correct.5`, #162). A complex type's or attribute group's attribute-use set (flattened through `attributeGroup` references) may not contain two distinct declarations with the same name and namespace, nor more than one attribute whose type is or derives from `xs:ID`. Declarations are deduplicated by the identity of their declaring node, so the same declaration reached by two paths (a diamond of attribute-group references) is one use, not a clash; names carry their resolved namespace, so a local attribute and a reference to a same-local-name attribute in an imported namespace do not collide. XSTS: invalid schemas accepted 505 to 495, no other bucket raised. Disclosed under-rejections: the set inherited through extension is not composed in, and a duplicate that requires an unprefixed reference in a schema with no default namespace is not detected.
- Circular references among model groups, attribute groups, and substitution-group affiliations are now rejected (XSD 1.0 `mg-props-correct.2`, `ag-props-correct.3`, `e-props-correct.6`, #168). A model group may not contain itself, an attribute group may not reference itself transitively, and a substitution chain may not loop. The model-group check follows references only within a definition's own compositor tree, stopping at element and type boundaries: a group referenced inside an element's content is a recursive data structure (permitted), not the group containing itself. A reference is followed only when it resolves to the schema's own target namespace, and a group or attribute group redefined in `xs:redefine` (whose single self-reference is the legal redefinition) ends the chain. XSTS: invalid schemas accepted 516 to 505, no other bucket raised. A genuine cycle introduced across two redefined definitions is a disclosed under-rejection.

View File

@ -0,0 +1,65 @@
private typealias IDVNode = PureXML.Schema.XSDNode
extension PureXML.Schema.XSDParser {
/// XSD 1.0 `a-props-correct.3` / `e-props-correct.5`: an attribute or element
/// declaration whose type is or derives from `xs:ID` must not carry a `default`
/// or `fixed` value constraint. Runs after named simple types are resolved, so a
/// named user type derived from `xs:ID` is recognized, not only the built-in and
/// inline forms. Only XSD-namespace declarations are checked; foreign content is
/// skipped.
static func idValueConstraintErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [String] {
var errors: [String] = []
collectIDValueConstraintErrors(schema, context, into: &errors)
return errors
}
private static func collectIDValueConstraintErrors(_ node: XSDTree, _ context: PureXML.Schema.XSDContext, into errors: inout [String]) {
let local = IDVNode.localName(node)
if local == "appinfo" || local == "documentation" { return }
if let error = idValueConstraintError(node, context) {
errors.append(error)
}
for child in IDVNode.elementChildren(node) {
collectIDValueConstraintErrors(child, context, into: &errors)
}
}
private static func idValueConstraintError(_ node: XSDTree, _ context: PureXML.Schema.XSDContext) -> String? {
guard node.name?.namespaceURI == xsdNamespace, let kind = IDVNode.localName(node),
kind == "element" || kind == "attribute",
let name = IDVNode.attribute(node, "name"),
IDVNode.attribute(node, "default") != nil || IDVNode.attribute(node, "fixed") != nil,
isIDDerived(node, context)
else { return nil }
return "'\(kind)' '\(name)' must not have a default or fixed value because its type is derived from ID"
}
/// Whether the declaration's type is or derives from `xs:ID`. A `type` attribute
/// is resolved by reference; an inline `simpleType` is ID only when it restricts
/// an ID-derived base (a list or union is not derived from ID).
private static func isIDDerived(_ node: XSDTree, _ context: PureXML.Schema.XSDContext) -> Bool {
if let typeName = IDVNode.attribute(node, "type") {
return typeReferenceIsID(typeName, context)
}
guard let inline = IDVNode.firstChild(node, named: "simpleType"),
let restriction = IDVNode.firstChild(inline, named: "restriction"),
let base = IDVNode.attribute(restriction, "base")
else {
return false
}
return typeReferenceIsID(base, context)
}
/// Whether a type reference resolves to, or derives from, the built-in `xs:ID`.
/// The reference's namespace is resolved from its prefix: only an `ID` in the
/// XSD namespace is the built-in; a user type is ID only if it resolves to one
/// whose base is `xs:ID`; a foreign or unresolved reference is not ID. (Matching
/// by local name alone would mistake a user type named `ID`, or an imported
/// `foo:ID`, for the built-in.)
private static func typeReferenceIsID(_ typeName: String, _ context: PureXML.Schema.XSDContext) -> Bool {
if IDVNode.referenceNamespace(typeName, context.namespaceBindings) == xsdNamespace {
return IDVNode.stripPrefix(typeName) == "ID"
}
return context.simpleTypes[IDVNode.stripPrefix(typeName)]?.base == .id
}
}

View File

@ -41,7 +41,7 @@ extension PureXML.Schema {
let elements = globalElements(containers, context, into: &types)
// Attribute-use uniqueness / single-ID run here, after `namedTypes`
// populated `simpleTypes`, so a named user type derived from `xs:ID` counts.
for error in referenceErrors(schema, in: context, elements: elements) + attributeUseErrors(containers, context) {
for error in referenceErrors(schema, in: context, elements: elements) + attributeUseErrors(containers, context) + idValueConstraintErrors(schema, context) {
context.diagnostics.report(error)
}
let (nillable, elementConstraints) = elementMetadata(containers)

View File

@ -0,0 +1,43 @@
@testable import PureXML
import Testing
@Suite("XSD ID-typed declarations have no value constraint")
struct SchemaIDValueConstraintTests {
private func compiles(_ body: String) -> Bool {
(try? PureXML.Schema.Document("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\(body)</xs:schema>")) != nil
}
@Test("An ID-typed attribute or element with a default or fixed value is rejected")
func test_idWithValueConstraintRejected() {
// Attribute of type xs:ID with a fixed value (a-props-correct.3).
#expect(!compiles("<xs:complexType name=\"T\"><xs:attribute name=\"a\" type=\"xs:ID\" fixed=\"x\"/></xs:complexType>"))
// Element of type xs:ID with a default value (e-props-correct.5).
#expect(!compiles("<xs:element name=\"e\" type=\"xs:ID\" default=\"x\"/>"))
// A named user type derived from xs:ID is recognized (the check runs after
// named types resolve).
#expect(!compiles("<xs:simpleType name=\"myID\"><xs:restriction base=\"xs:ID\"/></xs:simpleType>"
+ "<xs:complexType name=\"T\"><xs:attribute name=\"a\" type=\"myID\" fixed=\"x\"/></xs:complexType>"))
}
@Test("An ID-typed declaration without a value constraint, or a value constraint on a non-ID type, compiles")
func test_acceptedCases() {
// ID type, no value constraint.
#expect(compiles("<xs:complexType name=\"T\"><xs:attribute name=\"a\" type=\"xs:ID\"/></xs:complexType>"))
#expect(compiles("<xs:element name=\"e\" type=\"xs:ID\"/>"))
// A value constraint on a non-ID type is fine.
#expect(compiles("<xs:element name=\"e\" type=\"xs:string\" default=\"x\"/>"))
#expect(compiles("<xs:complexType name=\"T\"><xs:attribute name=\"a\" type=\"xs:string\" fixed=\"x\"/></xs:complexType>"))
}
@Test("A user type named ID (not xs:ID) with a value constraint is allowed")
func test_userTypeNamedIDNotBuiltin() {
// A user simpleType named ID in the target namespace, derived from string, is
// not the built-in xs:ID: a value constraint on it is legal. Resolving by
// local name alone would wrongly reject it.
let schema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
+ " targetNamespace=\"urn:t\" xmlns:t=\"urn:t\">"
+ "<xs:simpleType name=\"ID\"><xs:restriction base=\"xs:string\"/></xs:simpleType>"
+ "<xs:element name=\"e\" type=\"t:ID\" default=\"x\"/></xs:schema>"
#expect((try? PureXML.Schema.Document(schema)) != nil)
}
}

View File

@ -29,7 +29,7 @@ struct XSTSSuiteTests {
/// XSTS` (debug is far slower and the corpus is large). Per-case deviations
/// are written to /tmp/xsts-failures.txt for the burn-down.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 489
private let knownSchemaInvalidAccepted = 481
private let knownInstanceValidRejected = 180
private let knownInstanceInvalidAccepted = 160

View File

@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open.
| Bucket | Count | Reading |
|---|---|---|
| valid-schemas-rejected | **72 -> 4** | production target is 0: rejecting a correct schema is the most user-hostile defect (#148). Particle-restriction over-rejection fixed: pointless `maxOccurs=0` particles removed, content-free restriction of an EMPTY base, empty-derived emptiable-base, and (#163) the pairwise check over the normalized content model with occurrence subsumption applied only to same-kind pairings (cross-kind element/group, group/element, group/wildcard match occurrence inside, by effective total range / member). #164 resolved element-ref namespaces by prefix binding (the `particlesJj/Jk/Jm/Jn/Js`, `particlesQ/R` cluster, -28). Content-free (`maxOccurs=0`) derived groups and substitution-group declaration order closed `particlesW006`/`elemZ027_a`; the W3C MapAndSum rule (Sequence:Choice, #165) closed `particlesV003`. Remaining 1 (`particlesZ001`): a documented spec ambiguity (RecurseAsIfGroup; the reference rejects it), not a defect. |
| **invalid-schemas-accepted** | **2461 -> 489** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type landed). |
| **invalid-schemas-accepted** | **2461 -> 481** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion landed). |
| valid-instances-rejected | **233 -> 180** | (instance side; #164's element-ref namespace fix is shared with instance validation, -34) |
| invalid-instances-accepted | 160 | |