SAXONICA |
Saxon 9.0 introduces preliminary support for assertions, as defined in the XML Schema 1.1 Working Draft of 31 August 2006. Note that this specification is provisional and is quite likely to change in future drafts.
Assertions enable cross-validation of different elements or attributes within a complex type. For example, specifying:
<xs:assert test="xs:date(@date-of-birth) lt xs:date(@date-of-death)"/>
will cause a run-time validation error if an instance document is validated in which the relevant condition does not hold.
The current Working Draft also defines an xs:report
element that does the opposite: it asserts a condition that must hold.
This is not implemented in Saxon because there are rumours it may be dropped from the specification.
Saxon allows any XPath 2.0 expression to be used in the test
attribute. The context node supplied to the
expression is the element being validated, in its untyped state. The static context for the expression comes from the
containing schema document: any namespace prefixes used in the expression must be declared using namespace declarations
in the schema in the usual way. There is no default namespace for elements and types: any unprefixed element or type name
is assumed to be in no namespace (not in the targetNamespace of the schema document).
It is not possible to use any variables or user-defined functions within the expression.
The XPath expression is evaluated against a temporary document that contains the subtree rooted at this element: more specifically, the subtree contains a document node with this element as its only child. Validation succeeds if the effective boolean value (EBV) of the expression is true, and fails if the EBV is false or if an error occurs during the evaluation.
If a complex type is derived by extension or by restriction, then the assertions supplied on the base type must be satisfied as well as those supplied on the type itself.
Note that when assertions are defined on a complex type, the subtree representing an element with that type will be built in memory. It is therefore advisable to exercise care when applying this facility to elements that have very large subtrees.