Merge epic/77-xsd-wildcards: XSD wildcards and list built-ins

Closes #77
This commit is contained in:
Mihaela Mihaljevic 2026-06-08 13:53:27 +02:00
commit 76d7ccfedc
8 changed files with 368 additions and 112 deletions

View File

@ -9,10 +9,57 @@ public extension PureXML.Schema {
case all
}
/// An element wildcard (`xs:any`). The minimal namespace constraint: any
/// element matches.
struct Wildcard: Sendable {
public init() {}
/// The namespace constraint of a wildcard (`xs:any`/`xs:anyAttribute`).
enum WildcardNamespace: Sendable, Equatable {
/// `##any`: a name in any namespace, or none.
case any
/// `##other`: a name in any namespace other than the target, and not in no
/// namespace.
case other
/// A specific set of namespace URIs; the empty string stands for no
/// namespace (`##local`).
case enumerated(Set<String>)
}
/// How a wildcard-matched item is validated: `skip` (no validation), `lax`
/// (validate if a declaration is found), or `strict` (a declaration is
/// required).
enum ProcessContents: Sendable, Equatable {
case skip
case lax
case strict
}
/// An element or attribute wildcard (`xs:any`/`xs:anyAttribute`): which
/// namespaces it admits, and how matched items are validated.
struct Wildcard: Sendable, Equatable {
public var namespace: WildcardNamespace
public var processContents: ProcessContents
/// The schema's target namespace, needed to resolve `##other`.
public var targetNamespace: String?
public init(
namespace: WildcardNamespace = .any,
processContents: ProcessContents = .strict,
targetNamespace: String? = nil,
) {
self.namespace = namespace
self.processContents = processContents
self.targetNamespace = targetNamespace
}
/// Whether `name` is admitted by this wildcard's namespace constraint.
public func admits(_ name: PureXML.Model.QualifiedName) -> Bool {
let namespaceURI = name.namespaceURI ?? ""
switch namespace {
case .any:
return true
case .other:
return !namespaceURI.isEmpty && namespaceURI != (targetNamespace ?? "")
case let .enumerated(uris):
return uris.contains(namespaceURI)
}
}
}
/// The term of a particle: an element declaration, a nested model group, or a
@ -102,20 +149,20 @@ public extension PureXML.Schema {
case mixed(Particle)
}
/// A complex type: its attribute uses, whether unknown attributes are allowed,
/// and its content model.
/// A complex type: its attribute uses, an optional attribute wildcard
/// (`xs:anyAttribute`) admitting further attributes, and its content model.
struct ComplexType: Sendable {
public var attributes: [AttributeUse]
public var allowsOtherAttributes: Bool
public var attributeWildcard: Wildcard?
public var content: ContentType
public init(
attributes: [AttributeUse] = [],
allowsOtherAttributes: Bool = false,
attributeWildcard: Wildcard? = nil,
content: ContentType = .empty,
) {
self.attributes = attributes
self.allowsOtherAttributes = allowsOtherAttributes
self.attributeWildcard = attributeWildcard
self.content = content
}
}

View File

@ -2,6 +2,12 @@
/// namespace to avoid nesting a type two levels deep.
private typealias XSDFailure = PureXML.Validation.ValidationError
private typealias XSDPath = [PureXML.Validation.PathKey]
private typealias XSDGroup = PureXML.Schema.Group
private typealias XSDTerm = PureXML.Schema.Term
private typealias XSDTermLabel = PureXML.Schema.TermLabel
private typealias XSDProcessContents = PureXML.Schema.ProcessContents
private typealias XSDElementType = PureXML.Schema.ElementType
private typealias XSDWildcard = PureXML.Schema.Wildcard
public extension PureXML.Schema {
/// Validates an element against a ``ComplexType``: its attribute uses, its
@ -101,8 +107,10 @@ public extension PureXML.Schema {
errors.append(XSDFailure(reason: "missing required attribute '\(use.name.localName)'", at: path))
}
}
guard !type.allowsOtherAttributes else { return }
// An undeclared attribute is allowed only if an xs:anyAttribute wildcard
// admits its namespace.
for attribute in present where !type.attributes.contains(where: { $0.name.localName == attribute.name.localName }) {
if type.attributeWildcard?.admits(attribute.name) == true { continue }
errors.append(XSDFailure(reason: "undeclared attribute '\(attribute.name.localName)'", at: path))
}
}
@ -156,19 +164,56 @@ public extension PureXML.Schema {
errors.append(XSDFailure(reason: "content does not match the content model", at: path))
return
}
validateChildren(children, childTypes: Self.elementTypes(in: particle.term), at: path, into: &errors)
validateChildren(
children,
childTypes: Self.elementTypes(in: particle.term),
wildcard: Self.wildcard(in: particle.term),
at: path,
into: &errors,
)
}
private func validateChildren(
_ children: [PureXML.Model.Element],
childTypes: [String: ElementType],
wildcard: ProcessContents?,
at path: XSDPath,
into errors: inout [XSDFailure],
) {
let steps = Self.childSteps(children)
for (child, step) in zip(children, steps) {
guard let declared = childTypes[Self.key(child.name)] else { continue }
validateChild(child, against: declared, at: path + [step], into: &errors)
if let declared = childTypes[Self.key(child.name)] {
validateChild(child, against: declared, at: path + [step], into: &errors)
} else if let wildcard {
// The structure already matched, so an undeclared child here was
// admitted by a wildcard; process its content per the wildcard.
validateWildcardChild(child, processContents: wildcard, at: path + [step], into: &errors)
}
}
}
/// Validates a wildcard-matched child by its `processContents`: skip does
/// nothing, lax validates against a global declaration when one exists, and
/// strict requires one.
private func validateWildcardChild(
_ child: PureXML.Model.Element,
processContents: ProcessContents,
at path: XSDPath,
into errors: inout [XSDFailure],
) {
switch processContents {
case .skip:
return
case .lax:
if let declaration = types["element:\(child.name.localName)"] {
validateChild(child, against: declaration, at: path, into: &errors)
}
case .strict:
guard let declaration = types["element:\(child.name.localName)"] else {
errors.append(XSDFailure(reason: "no declaration for wildcard-matched element '\(child.name.localName)'", at: path))
return
}
validateChild(child, against: declaration, at: path, into: &errors)
}
}
@ -208,103 +253,124 @@ public extension PureXML.Schema {
validateChild(child, against: resolved, at: path, into: &errors)
}
}
}
}
// MARK: Helpers
extension PureXML.Schema.ComplexValidator {
// MARK: Helpers
/// The coding-path step for each child: its name, with a sibling index only
/// when more than one child shares that name.
private static func childSteps(_ children: [PureXML.Model.Element]) -> [PureXML.Validation.PathKey] {
var totals: [String: Int] = [:]
for child in children {
totals[child.name.description, default: 0] += 1
/// The coding-path step for each child: its name, with a sibling index only
/// when more than one child shares that name.
static func childSteps(_ children: [PureXML.Model.Element]) -> [PureXML.Validation.PathKey] {
var totals: [String: Int] = [:]
for child in children {
totals[child.name.description, default: 0] += 1
}
var seen: [String: Int] = [:]
return children.map { child in
let name = child.name.description
let index = (seen[name] ?? 0) + 1
seen[name] = index
return (totals[name] ?? 0) > 1 ? .element(name, index: index) : .element(name)
}
}
/// Validates an `all` group order-independently: every child matches a
/// member within its occurrence bounds, and each member meets its minimum.
fileprivate static func matchesAll(_ group: XSDGroup, names: [PureXML.Model.QualifiedName]) -> Bool {
var counts = [Int](repeating: 0, count: group.particles.count)
for name in names {
guard let index = group.particles.indices.first(where: { position in
let member = group.particles[position]
let room = member.maxOccurs.map { counts[position] < $0 } ?? true
return room && label(of: member.term).matches(name)
}) else { return false }
counts[index] += 1
}
for (index, member) in group.particles.enumerated() where counts[index] < member.minOccurs {
return false
}
return true
}
fileprivate static func label(of term: XSDTerm) -> XSDTermLabel {
switch term {
case let .element(name, _): .name(name)
case let .wildcard(wildcard): .wildcard(wildcard)
case .group: .wildcard(XSDWildcard())
}
}
/// The `processContents` of a wildcard reachable in `term`, if the content
/// model contains one.
fileprivate static func wildcard(in term: XSDTerm) -> XSDProcessContents? {
switch term {
case let .wildcard(wildcard):
return wildcard.processContents
case let .group(group):
for member in group.particles {
if let found = wildcard(in: member.term) { return found }
}
var seen: [String: Int] = [:]
return children.map { child in
let name = child.name.description
let index = (seen[name] ?? 0) + 1
seen[name] = index
return (totals[name] ?? 0) > 1 ? .element(name, index: index) : .element(name)
return nil
case .element:
return nil
}
}
fileprivate static func elementTypes(in term: XSDTerm) -> [String: XSDElementType] {
var result: [String: XSDElementType] = [:]
collectTypes(term, into: &result)
return result
}
fileprivate static func collectTypes(_ term: XSDTerm, into result: inout [String: XSDElementType]) {
switch term {
case let .element(name, type):
if let type { result[key(name)] = type }
case let .group(group):
for member in group.particles {
collectTypes(member.term, into: &result)
}
case .wildcard:
break
}
}
static func key(_ name: PureXML.Model.QualifiedName) -> String {
"{\(name.namespaceURI ?? "")}\(name.localName)"
}
static func textContent(_ element: PureXML.Model.Element) -> String {
let text = element.children.reduce(into: "") { result, child in
switch child {
case let .text(value), let .cdata(value): result += value
default: break
}
}
return text.trimmingXMLWhitespace()
}
/// Validates an `all` group order-independently: every child matches a
/// member within its occurrence bounds, and each member meets its minimum.
private static func matchesAll(_ group: Group, names: [PureXML.Model.QualifiedName]) -> Bool {
var counts = [Int](repeating: 0, count: group.particles.count)
for name in names {
guard let index = group.particles.indices.first(where: { position in
let member = group.particles[position]
let room = member.maxOccurs.map { counts[position] < $0 } ?? true
return room && label(of: member.term).matches(name)
}) else { return false }
counts[index] += 1
}
for (index, member) in group.particles.enumerated() where counts[index] < member.minOccurs {
return false
}
return true
}
static func isNamespaceDeclaration(_ attribute: PureXML.Model.Attribute) -> Bool {
attribute.name.prefix == "xmlns" || (attribute.name.prefix == nil && attribute.name.localName == "xmlns")
}
private static func label(of term: Term) -> TermLabel {
if case let .element(name, _) = term { return .name(name) }
return .any
}
/// Whether an attribute belongs to the XML Schema instance namespace
/// (`xsi:type`, `xsi:nil`, and the schema-location hints), which are
/// processing directives rather than declared attributes.
static func isSchemaInstanceAttribute(_ attribute: PureXML.Model.Attribute) -> Bool {
attribute.name.namespaceURI == "http://www.w3.org/2001/XMLSchema-instance" || attribute.name.prefix == "xsi"
}
private static func elementTypes(in term: Term) -> [String: ElementType] {
var result: [String: ElementType] = [:]
collectTypes(term, into: &result)
return result
}
private static func collectTypes(_ term: Term, into result: inout [String: ElementType]) {
switch term {
case let .element(name, type):
if let type { result[key(name)] = type }
case let .group(group):
for member in group.particles {
collectTypes(member.term, into: &result)
}
case .wildcard:
break
}
}
private static func key(_ name: PureXML.Model.QualifiedName) -> String {
"{\(name.namespaceURI ?? "")}\(name.localName)"
}
private static func textContent(_ element: PureXML.Model.Element) -> String {
let text = element.children.reduce(into: "") { result, child in
switch child {
case let .text(value), let .cdata(value): result += value
default: break
}
}
return text.trimmingXMLWhitespace()
}
private static func isNamespaceDeclaration(_ attribute: PureXML.Model.Attribute) -> Bool {
attribute.name.prefix == "xmlns" || (attribute.name.prefix == nil && attribute.name.localName == "xmlns")
}
/// Whether an attribute belongs to the XML Schema instance namespace
/// (`xsi:type`, `xsi:nil`, and the schema-location hints), which are
/// processing directives rather than declared attributes.
private static func isSchemaInstanceAttribute(_ attribute: PureXML.Model.Attribute) -> Bool {
attribute.name.namespaceURI == "http://www.w3.org/2001/XMLSchema-instance" || attribute.name.prefix == "xsi"
}
/// The local name of an element's `xsi:type` attribute value, or nil when it
/// carries none. Recognizes the attribute by the XML Schema instance
/// namespace or, failing namespace resolution, the conventional `xsi` prefix.
private static func xsiTypeName(_ element: PureXML.Model.Element) -> String? {
let schemaInstance = "http://www.w3.org/2001/XMLSchema-instance"
let match = element.attributes.first { attribute in
attribute.name.localName == "type"
&& (attribute.name.namespaceURI == schemaInstance || attribute.name.prefix == "xsi")
}
return match.map { $0.value.split(separator: ":").last.map(String.init) ?? $0.value }
/// The local name of an element's `xsi:type` attribute value, or nil when it
/// carries none. Recognizes the attribute by the XML Schema instance
/// namespace or, failing namespace resolution, the conventional `xsi` prefix.
static func xsiTypeName(_ element: PureXML.Model.Element) -> String? {
let schemaInstance = "http://www.w3.org/2001/XMLSchema-instance"
let match = element.attributes.first { attribute in
attribute.name.localName == "type"
&& (attribute.name.namespaceURI == schemaInstance || attribute.name.prefix == "xsi")
}
return match.map { $0.value.split(separator: ":").last.map(String.init) ?? $0.value }
}
}

View File

@ -3,11 +3,11 @@ extension PureXML.Schema {
/// element name, or a wildcard.
enum TermLabel: Sendable {
case name(PureXML.Model.QualifiedName)
case any
case wildcard(Wildcard)
func matches(_ actual: PureXML.Model.QualifiedName) -> Bool {
switch self {
case .any: true
case let .wildcard(wildcard): wildcard.admits(actual)
case let .name(declared): declared.localName == actual.localName
&& declared.namespaceURI == actual.namespaceURI
}
@ -134,8 +134,8 @@ extension PureXML.Schema {
switch term {
case let .element(name, _):
labeled(.name(name))
case .wildcard:
labeled(.any)
case let .wildcard(wildcard):
labeled(.wildcard(wildcard))
case let .group(group):
self.group(group)
}

View File

@ -59,7 +59,7 @@ public extension PureXML.Schema {
case let .elementOnly(particle), let .mixed(particle):
let (labels, complete) = ContentNFABuilder.build(particle).follow(after: childNames)
let names = labels.compactMap { if case let .name(name) = $0 { name.description } else { nil } }
let anyElement = labels.contains { if case .any = $0 { true } else { false } }
let anyElement = labels.contains { if case .wildcard = $0 { true } else { false } }
return Completions(elements: names, complete: complete, allowsAnyElement: anyElement, attributes: attributes)
case .empty, .simpleContent:
return Completions(elements: [], complete: true, allowsAnyElement: false, attributes: attributes)

View File

@ -114,6 +114,9 @@ extension PureXML.Schema {
var simpleTypes: [String: SimpleType]
var attributeGroups: [String: XSDTree]
var groups: [String: XSDTree]
/// The schema's target namespace, for resolving `##other`/`##targetNamespace`
/// in wildcard constraints.
var targetNamespace: String?
/// Each substitution-group head maps to its transitive member element
/// names, so an `xs:element ref` to a head also admits its members.
var substitutions: [String: [String]] = [:]

View File

@ -18,6 +18,7 @@ extension PureXML.Schema {
simpleTypes: [:],
attributeGroups: indexByName(allChildren(containers, named: "attributeGroup")),
groups: indexByName(allChildren(containers, named: "group")),
targetNamespace: XSDNode.attribute(schema, "targetNamespace"),
substitutions: XSDNode.substitutionMembers(containers),
)
// Named simple types may restrict or list one another regardless of
@ -96,13 +97,16 @@ extension PureXML.Schema {
if let builtin = BuiltinType(rawValue: local) {
return .simple(SimpleType(base: builtin))
}
if let item = XSDSimpleParser.listBuiltinItem(local) {
return .simple(.list(item: SimpleType(base: item)))
}
return .typeReference(local)
}
private static var anyType: ComplexType {
ComplexType(
allowsOtherAttributes: true,
content: .mixed(Particle(minOccurs: 0, maxOccurs: nil, term: .wildcard(Wildcard()))),
attributeWildcard: Wildcard(processContents: .skip),
content: .mixed(Particle(minOccurs: 0, maxOccurs: nil, term: .wildcard(Wildcard(processContents: .skip)))),
)
}
@ -114,14 +118,16 @@ extension PureXML.Schema {
if let simpleContent = XSDNode.firstChild(node, named: "simpleContent") {
let inner = derivation(simpleContent)
attributes += inner.map { attributeUses(under: $0, context) } ?? []
return ComplexType(attributes: attributes, content: .simpleContent(simpleContentType(simpleContent, context)))
let wildcard = inner.flatMap { attributeWildcard(under: $0, context) }
return ComplexType(attributes: attributes, attributeWildcard: wildcard, content: .simpleContent(simpleContentType(simpleContent, context)))
}
let container = XSDNode.firstChild(node, named: "complexContent").flatMap(derivation) ?? node
attributes += container === node ? [] : attributeUses(under: container, context)
let wildcard = attributeWildcard(under: container, context)
guard let particle = modelGroup(in: container, context) else {
return ComplexType(attributes: attributes, content: .empty)
return ComplexType(attributes: attributes, attributeWildcard: wildcard, content: .empty)
}
return ComplexType(attributes: attributes, content: mixed ? .mixed(particle) : .elementOnly(particle))
return ComplexType(attributes: attributes, attributeWildcard: wildcard, content: mixed ? .mixed(particle) : .elementOnly(particle))
}
private static func derivation(_ node: XSDTree) -> XSDTree? {
@ -228,7 +234,7 @@ extension PureXML.Schema {
case "group":
return groupReferenceParticle(node, minimum, maximum, context)
case "any":
return Particle(minOccurs: minimum, maxOccurs: maximum, term: .wildcard(Wildcard()))
return Particle(minOccurs: minimum, maxOccurs: maximum, term: .wildcard(wildcard(node, context)))
default:
return nil
}
@ -324,3 +330,37 @@ extension PureXML.Schema.XSDParser {
return result
}
}
extension PureXML.Schema.XSDParser {
/// The `xs:anyAttribute` wildcard declared directly under `node`, if any.
static func attributeWildcard(under node: XSDTree, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.Wildcard? {
PureXML.Schema.XSDNode.firstChild(node, named: "anyAttribute").map { wildcard($0, context) }
}
/// Parses a wildcard's `namespace` and `processContents` constraints.
static func wildcard(_ node: XSDTree, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.Wildcard {
let processContents: PureXML.Schema.ProcessContents = switch PureXML.Schema.XSDNode.attribute(node, "processContents") {
case "skip": .skip
case "lax": .lax
default: .strict
}
let namespace = wildcardNamespace(PureXML.Schema.XSDNode.attribute(node, "namespace"), context)
return PureXML.Schema.Wildcard(namespace: namespace, processContents: processContents, targetNamespace: context.targetNamespace)
}
private static func wildcardNamespace(_ value: String?, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.WildcardNamespace {
switch value {
case nil, "##any": return .any
case "##other": return .other
default:
let uris = (value ?? "").split(whereSeparator: \.isWhitespace).map { token -> String in
switch token {
case "##targetNamespace": context.targetNamespace ?? ""
case "##local": ""
default: String(token)
}
}
return .enumerated(Set(uris))
}
}
}

View File

@ -31,9 +31,21 @@ extension PureXML.Schema {
static func simpleTypeReference(_ typeName: String, _ context: XSDContext) -> SimpleType {
let local = XSDNode.stripPrefix(typeName)
if let builtin = BuiltinType(rawValue: local) { return SimpleType(base: builtin) }
if let item = listBuiltinItem(local) { return .list(item: SimpleType(base: item)) }
return context.simpleTypes[local] ?? SimpleType(base: .string)
}
/// The item type of a built-in list datatype (`IDREFS`, `ENTITIES`,
/// `NMTOKENS`), each a whitespace-separated list of its singular form.
static func listBuiltinItem(_ name: String) -> BuiltinType? {
switch name {
case "IDREFS": .idref
case "ENTITIES": .entity
case "NMTOKENS": .nmtoken
default: nil
}
}
static func applyFacets(_ restriction: XSDTree, into facets: inout Facets) {
for facet in XSDNode.elementChildren(restriction) {
let value = XSDNode.attribute(facet, "value")

View File

@ -0,0 +1,88 @@
@testable import PureXML
import Testing
@Suite("XSD wildcards and list builtins")
struct XSDWildcardTests {
private func validate(_ xsd: String, _ xml: String) throws -> [PureXML.Validation.ValidationError] {
try PureXML.Schema.Document(xsd).validate(xml)
}
@Test("xs:any with processContents=skip admits any element without validating it")
func test_anySkip() throws {
let xsd = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="box">
<xs:complexType>
<xs:sequence>
<xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
"""
#expect(try validate(xsd, "<box><anything/><else>x</else></box>").isEmpty)
}
@Test("xs:any with processContents=strict requires a global declaration")
func test_anyStrict() throws {
let xsd = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="known" type="xs:integer"/>
<xs:element name="box">
<xs:complexType>
<xs:sequence>
<xs:any processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
"""
// A declared element validates against its declaration through the wildcard.
#expect(try validate(xsd, "<box><known>1</known></box>").isEmpty)
#expect(try !validate(xsd, "<box><known>x</known></box>").isEmpty)
// An undeclared element is rejected under strict.
#expect(try !validate(xsd, "<box><mystery/></box>").isEmpty)
}
@Test("xs:anyAttribute admits undeclared attributes")
func test_anyAttribute() throws {
let xsd = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="item">
<xs:complexType>
<xs:attribute name="id" type="xs:string"/>
<xs:anyAttribute processContents="skip"/>
</xs:complexType>
</xs:element>
</xs:schema>
"""
#expect(try validate(xsd, "<item id=\"1\" extra=\"y\" more=\"z\"/>").isEmpty)
}
@Test("Undeclared attributes are still rejected without a wildcard")
func test_noWildcardRejects() throws {
let xsd = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="item">
<xs:complexType>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
"""
#expect(try !validate(xsd, "<item id=\"1\" extra=\"y\"/>").isEmpty)
}
@Test("List built-in datatypes validate each item")
func test_listBuiltins() throws {
let xsd = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tokens" type="xs:NMTOKENS"/>
</xs:schema>
"""
#expect(try validate(xsd, "<tokens>a b c</tokens>").isEmpty)
// A space is not a valid NMTOKEN item, but spaces separate items, so this
// tests that an item with an illegal character is rejected.
#expect(try !validate(xsd, "<tokens>a b@d</tokens>").isEmpty)
}
}