Saturday, September 24, 2016

XML Schema 1.1 assertion facet revisited

Consider the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<X>
    <a>1</a>
    <a>2</a>
    <a>3</a>
    <a>4</a>
    <a>5</a> 
</X>

We have the following requirement for XML Schema validation : The element "X" will be considered valid, when the values in each of element "a" within it has mathematical even values. In the example above, following three values of elements "a" makes the element "X" invalid : 1, 3 & 5. The following XML Schema 1.1 document using the <assertion> facet (its a facet just like XML Schema 1.0 facets "minInclusive" etc), implements these requirements:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="X">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="a" maxOccurs="10">
                    <xs:simpleType>
                        <xs:restriction base="xs:int">
                            <xs:assertion test="$value mod 2 = 0"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
   </xs:element>
 
</xs:schema>

Implementing this requirement, requires using the <assertion> facet, since we have to use the XPath 2.0 "mod" operator to test for even values.

When using Apache Xerces as an XML Schema 1.1 validation engine, we get the following outputs for the validation attempt:
 [Error] x1.xml:3:11: cvc-assertions-valid: Value '1' is not facet-valid with respect to assertion '$value mod 2 = 0'.
[Error] x1.xml:3:11: cvc-assertion: Assertion evaluation ('$value mod 2 = 0') for element 'a' on schema type '#AnonType_aX' did not succeed.
[Error] x1.xml:5:11: cvc-assertions-valid: Value '3' is not facet-valid with respect to assertion '$value mod 2 = 0'.
[Error] x1.xml:5:11: cvc-assertion: Assertion evaluation ('$value mod 2 = 0') for element 'a' on schema type '#AnonType_aX' did not succeed.
[Error] x1.xml:7:11: cvc-assertions-valid: Value '5' is not facet-valid with respect to assertion '$value mod 2 = 0'.
[Error] x1.xml:7:11: cvc-assertion: Assertion evaluation ('$value mod 2 = 0') for element 'a' on schema type '#AnonType_aX' did not succeed.

This is a really nice capability of XML Schema 1.1 I think. Also note that, within error messages we see type names as '#AnonType_aX' (this is fine and great, and is a historical Apache Xerces error reporting, and it stands for anonymous XML Schema types since the type doesn't have a name). Had we given a specific name to the complex type, like "TypeX", then that would have appeared in the error messages if errors are there during the XML document validation.


No comments: