When Does xsd:any processContents='lax' Hide Undefined Elements Instead of Reporting Them?
0 reputation · 20 Feb 2024, 14:19 UTC
0 reputation · 20 Feb 2024, 14:19 UTC
In XSD 1.0, the xsd:any wildcard supports schema extensibility by allowing elements not declared in the base schema. The processContents attribute controls whether those elements are validated against no schema, a fixed schema, or a lax schema. A common design goal is to permit extension elements while still catching accidental or misspelled elements that belong to a known namespace.
The unresolved decision concerns the interaction between processContents="lax" and the wildcard's namespace attribute. When a lax wildcard encounters an element for which no schema is available, validation ignores it. If the namespace attribute is misconfigured, such as using ##other when a specific target namespace was intended, the validator may silently accept elements that should have been flagged. The same document might validate successfully under lax processing but fail under strict processing.
Verification typically involves comparing validator output for a document containing an undefined element under both modes, using a standard JAXP validator. However, the correct choice for a given extensibility contract remains unclear.
Which conditions cause a lax wildcard to silently ignore an element that has no matching schema declaration? How does the namespace attribute on xsd:any alter that outcome? And when should a schema author prefer strict processing despite the loss of extensibility?
32775 reputation · 20 Feb 2024, 21:55 UTC
An xsd:any element with processContents="lax" silently ignores an element when the validator cannot find a corresponding global declaration for that element's namespace and local name. Under lax processing, the validator attempts to find a schema; if none is available, it treats the element as valid by default rather than reporting an error.
The namespace attribute on xsd:any acts as a primary filter. The validator first checks if the element in the XML instance matches the namespace constraint of the wildcard. If it matches, the processContents logic is applied:
namespace attribute (e.g., ##other or a specific URI) but no schema definition for that element is loaded in the validator's context, the element is ignored. No error is raised.namespace attribute, the document is invalid regardless of the processContents setting.Using namespace="##other" combined with processContents="lax" is a common source of "silent failures." Because ##other accepts any namespace except the target namespace of the current schema, any element from any external namespace will be accepted without a schema. If a developer misspells a namespace URI, the validator will likely fail to find the intended schema and simply skip validation for that element.
Schema authors should prefer processContents="strict" when the extensibility contract requires that all extensions be explicitly defined and validated. While strict processing removes the ability to accept "unknown" future extensions, it is necessary when:
To verify if your validator is silently ignoring elements, perform these three tests:
lax. It should pass.processContents="strict" and pass the undefined element from step 1. It should fail.Note: To provide a more specific recommendation, please specify if you are using a JAXP-compliant validator or a specific XSD version (1.0 vs 1.1).
Use comments to ask for clarification. Post a solution as an answer.
32,775 reputation · 20 Feb 2024, 22:38 UTC
To further clarify the behavior of processContents="lax", it is important to distinguish it from processContents="skip". While both can result in a document passing validation despite containing undefined elements, they operate on different logic:
For those using JAXP or similar validators, this means lax provides a safety net: it allows for unknown extensions but still catches errors in known extensions. To verify this, test a document where an element matches a loaded schema but contains invalid data; lax will report the error, whereas skip will remain silent.