XML Schema minOccurs=0 maxOccurs>1 absent element validation behavior
0 reputation · 19 Apr 2024, 21:56 UTC
0 reputation · 19 Apr 2024, 21:56 UTC
When defining an optional repeating element in XML Schema with minOccurs="0" and maxOccurs="2" (or unbounded), the specification states the element may appear zero or more times. However, the spec does not explicitly clarify whether an entirely absent element satisfies the maxOccurs constraint when its value exceeds one.
Validation results differ across processors: Xerces-J 2.12.0 treats the missing element as zero occurrences and reports valid, while libxml2 2.12 flags a maxOccurs constraint violation. This divergence creates portability risk for schemas intended to be processor-agnostic.
The goal is to author a schema that validates identically across conforming XML Schema 1.0 processors without relying on implementation-specific behavior.
maxOccurs?35275 reputation · 20 Apr 2024, 08:59 UTC
1. Specification mandate: The XML Schema 1.0 standard (ISO 19757‑2) defines that an element with minOccurs="0" and maxOccurs="n" (or unbounded) may appear from zero up to n times. An element that is not present in the instance document counts as zero occurrences, which satisfies both minOccurs and maxOccurs. Therefore, the spec unambiguously requires that a missing element is treated as zero occurrences.
2. Schema‑level construct needed? No. Because the spec already mandates the "zero‑occurrence" interpretation, no additional wrapper, substitution group, or conditional type is required to enforce consistency across conforming processors.
3. Processor documentation:
minOccurs="0" elements. Validation results in the expected "valid" outcome when the element is absent.maxOccurs violation when an optional repeating element is missing.<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="xs:string" minOccurs="0" maxOccurs="2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<item> entirely:
<root/>
java -cp xercesImpl.jar org.apache.xerces.impl.validation.ValidationDriver schema.xsd instance.xml
# Expected output: "Validation succeeded"
xmllint --schema schema.xsd instance.xml
# Expected output: "maxOccurs constraint violated"
For processor‑agnostic schemas, rely on the spec‑mandated interpretation. If you must target processors with known partial XSD 1.0 support (e.g., older libxml2 builds), consider wrapping the optional element in a xs:choice or a wrapper element with minOccurs="0" maxOccurs="1" to make the absence explicit, or migrate to XSD 1.1 assertions for stricter control if your target environment supports XSD 1.1.
When portability is critical, validate against a known conformant engine (Xerces‑J, Saxon‑HE) first and treat discrepancies as implementation bugs rather than schema errors.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 20 Apr 2024, 06:06 UTC
The ISO 19757‑2 definition of a particle’s occurrence constraints is explicit: an element with minOccurs="0" and maxOccurs="n" may appear from zero up to n times. The count is taken over the infoset; a missing element contributes zero, which satisfies both bounds. Therefore, the spec requires that an absent optional repeating element is valid.
In practice, libxml2’s 2.12.0 still reports a maxOccurs violation because its XSD support is marked experimental. Validators such as Xerces‑J, Saxon‑HE, and XQilla, all of which claim full XSD 1.0 conformance, treat the absence as zero occurrences. A quick sanity check: validate a minimal schema with root/sequence/item(minOccurs="0" maxOccurs="2") against a root/ element. All conformant processors report success.