Sunday, September 25, 2016

XML Schema 1.1 : accessing an XML tree structure during validation

In this post, I'll discuss an XML Schema validity definition that spans sibling elements in an XML document. Implementing this has become possible with XML Schema 1.1, by its new co-occurence facility.

Here's the XML document that needs to be validated by an XML Schema document:

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

The validation requirement is : write an XML Schema document, that meets following conditions:
Element "X" is valid, if sum of values within its child elements is greater than 7 (this is a hypothetical number for this problem).

The following XML Schema document solves this problem:

<?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" type="xs:int"/>
                 <xs:element name="b" type="xs:int"/>
                 <xs:element name="c" type="xs:int"/>
                 <xs:element name="d" type="xs:int"/>
                 <xs:element name="e" type="xs:int"/>
            </xs:sequence>         
            <xs:assert test="sum(*) gt 7"/>
            <!-- this assert also does the same thing : <xs:assert test="sum(a | b | c | d | e) gt 7"/> -->
         </xs:complexType>
    </xs:element>
   
</xs:schema>

This and earlier few posts illustrates the usefulness that XML Schema 1.1 <assert> (and also <assertion>) construct has. The simplicity behind this is, that XML Schema 1.1 <assert> / <assertion> can use the whole 'schema type aware' XPath 2.0 language, expressions of which work on the context tree (in case of <assert>) on which a particular set of schema <assert>'s works. Remember that, <assertion> is a facet (just like <minInclusive> for example) that has access only to an atomic value that is validated.

Please don't be mislead by the title of this post, "accessing an XML tree structure during validation" in a sense that, it applies only to an <assert>. It means also similarly, for example during the complex type definition of an XML element (in which we're defining the XML structure as a tree below a specific XML element). This post uses this terminology for XML Schema 1.1 <assert> trees, and not for other kinds of trees as mentioned.

No comments: