Monday, August 7, 2017

Mathematical table data with XML Schema 1.1

Here's a simple example, using XML Schema 1.1 <assert> to validate elementary school mathematical tables.

XML document:
<?xml version="1.0"?>
<table id="2">
  <x>2</x>
  <x>4</x>
  <x>6</x>
  <x>8</x>
  <x>10</x>
  <x>12</x>
  <x>14</x>
  <x>16</x>
  <x>18</x>
  <x>20</x>
</table>

XSD 1.1 document:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
   <xs:element name="table">
     <xs:complexType>
        <xs:sequence>
           <xs:element name="x" minOccurs="10" maxOccurs="10"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:positiveInteger" use="required">
           <xs:annotation>
             <xs:documentation>Mathematical table of @id is represented.</xs:documentation>
           </xs:annotation>
        </xs:attribute>
        <xs:assert test="x[1] = @id"/>
        <xs:assert test="every $x in x[position() gt 1] satisfies $x = $x/preceding-sibling::x[1] + @id">
           <xs:annotation>
              <xs:documentation>An XPath 2.0 expression validating the depicted mathematical table.    
              </xs:documentation>
           </xs:annotation>
        </xs:assert>
     </xs:complexType>
   </xs:element>
 
</xs:schema>

No comments: