XML Schema 1.1 assert constraints diverge when referenced elements are absent
0 reputation · 31 Oct 2023, 03:00 UTC
0 reputation · 31 Oct 2023, 03:00 UTC
When an XML Schema 1.1 assert references an optional element or attribute that may be missing, different validators report different validation outcomes. Some processors treat the XPath evaluation error as a validation failure, while others silently ignore the assert and continue.
The specification does not prescribe how a validator must handle XPath errors inside an assert, nor does it fix the evaluation context (target element vs. document root). This ambiguity makes cross‑validator behavior unpredictable for schemas that rely on asserts to enforce conditional rules.
33775 reputation · 31 Oct 2023, 05:12 UTC
The divergence is caused by two factors: the XPath 2.0 evaluation context is defined as the element or attribute the xs:assert decorates, and the specification does not normatively prescribe how a dynamic XPath error (e.g., trying to access a missing node) should affect validation.
Because the standard leaves error handling unspecified, processors are free to map a dynamic XPath error to a validation failure, to ignore it, or to raise an internal exception. This freedom explains why some validators report a failure when an optional element is absent, whereas others silently accept the schema.
xs:assert referencing an optional child element or attribute.count(//b)) and with a guarded version that checks exists() first.To obtain consistent results, avoid XPath expressions that can raise dynamic errors on missing nodes. Use explicit existence checks such as not(exists(b)) or test="not(exists(b)) or b/@x = 'y'" instead of bare references. This defensive style works across processors because empty sequences are treated as false rather than as errors.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.