Wednesday, May 17, 2017

Turing Award 2016

Its nice to see, Sir Tim Berners-Lee as a recipient of A.M. Turing Award. More details are available on http://awards.acm.org/about/2016-turing.

Sunday, December 18, 2016

XML Schema 1.1 type alternative patterns


Please note that, XML Schema 1.1 type alternatives allows selection of an XML Schema type depending on something related to attributes.

I think use of XML Schema 1.1 type alternatives fall into the following 2 broad patterns:

1) When an attribute is mandatory

The following XML Schema document illustrates this use:

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

   <xs:element name="X">
      <xs:alternative test="@a = 'val1'" type="XType1"/>
      <xs:alternative test="@a = 'val2'" type="XType2"/>
      <xs:alternative type="XType3"/>
   </xs:element>
  
   <xs:complexType name="XType1">
      <xs:sequence>
        <xs:element name="A" type="xs:integer"/>
        <xs:element name="B" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="required"/>
   </xs:complexType>
  
   <xs:complexType name="XType2">
      <xs:sequence>
         <xs:element name="C" type="xs:integer"/>
         <xs:element name="D" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="required"/>
   </xs:complexType>
  
   <xs:complexType name="XType3">
      <xs:sequence>
         <xs:element name="P" type="xs:integer"/>
         <xs:element name="Q" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="required"/>
   </xs:complexType>
 
</xs:schema>

Following are some of XML instance documents, which are valid according to the above schema document.

<X a="val1">
  <A>1</A>
  <B>2</B>
</X>

i.e when attribute's value is "val1", a specific complex type is assigned to X.

<X a="val2">
  <C>1</C>
  <D>2</D>
</X>

i.e when attribute's value is "val2", a specific complex type is assigned to X.

and

<X a="something else">
  <P>1</P>
  <Q>2</Q>
</X>

i.e when attribute's value is anything other than "val1" or "val2", a specific complex type is assigned to X.  

2) When an attribute is optional

The following XML Schema document illustrates this use:

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

   <xs:element name="X">
      <xs:alternative test="@a" type="XType1"/>
      <xs:alternative test="not(@a)" type="XType2"/>
   </xs:element>
  
   <xs:complexType name="XType1">
      <xs:sequence>
        <xs:element name="A" type="xs:integer"/>
        <xs:element name="B" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="optional"/>
   </xs:complexType>
  
   <xs:complexType name="XType2">
      <xs:sequence>
         <xs:element name="C" type="xs:integer"/>
         <xs:element name="D" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="optional"/>
   </xs:complexType>
 
</xs:schema>

Following are some of XML instance documents, which are valid according to the above schema document.

<X a="something1">
  <A>1</A>
  <B>2</B>
</X>

i.e when an attribute is present, a specific complex type is assigned to X.

and

<X>
  <C>1</C>
  <D>2</D>
</X>

i.e when an attribute is absent, a specific complex type is assigned to X.

I hope that this post is useful.

Sunday, November 13, 2016

XML Schema : <assert> helps us process wild-cards and attributes

Please let me illustrate my point with the following XML Schema (1.1) example:

XML Schema document:
<?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:any processContents="skip" minOccurs="3" maxOccurs="3"/>
        </xs:sequence>
        <xs:attribute name="y1" type="xs:string"/>
        <xs:attribute name="y2" type="xs:string"/>
        <xs:attribute name="y3" type="xs:string"/>
        <xs:assert test="deep-equal(for $el in * return name($el), for $at in @* return name($at))"/>
     </xs:complexType>
   </xs:element>
 
</xs:schema>

This schema document says following:
1) A wild-card requires 3 element nodes.
2) There are 3 attribute nodes, of same cardinality as the elements.
3) The <assert> says that, name of elements validated by wild-cards must be same as the names of attributes.

Here's a valid XML document for the above schema document:
<?xml version="1.0" encoding="UTF-8"?>
<X y1="A" y2="B" y3="C">
  <y1>A</y1>
  <y2>B</y2>
  <y3>C</y3>
</X>

Reference to XPath 2.0 language (for <assert> path expressions) : https://www.w3.org/TR/xpath20/.

I hope this example is useful.

Friday, October 28, 2016

XML Schema 1.1 : assertion refines enumeration

In this post, I'll try to explain how the XML Schema 1.1 <assertion> facet refines the XML Schema <enumeration> facet in useful ways, and also helps us build a useful domain vocabulary written in the XML Schema language.

Consider the following XML Schema 1.1 document:

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

   <xs:element name="day" type="Day"/>
  
   <xs:element name="workday" type="WorkDay"/>
  
   <xs:element name="holiday" type="Holiday"/>
  
   <xs:simpleType name="WorkDay">
      <xs:restriction base="Day">
         <xs:assertion test="$value != 'saturday' and $value != 'sunday'"/>
      </xs:restriction>
   </xs:simpleType>
  
   <xs:simpleType name="Holiday">
     <xs:restriction base="Day">
        <xs:assertion test="$value = 'saturday' or $value = 'sunday'"/>
     </xs:restriction>
   </xs:simpleType> 
  
   <xs:simpleType name="Day">
      <xs:restriction base="xs:string">
         <xs:enumeration value="monday"/>
         <xs:enumeration value="tuesday"/>
         <xs:enumeration value="wednesday"/>
         <xs:enumeration value="thursday"/>
         <xs:enumeration value="friday"/>
         <xs:enumeration value="saturday"/>
         <xs:enumeration value="sunday"/>
      </xs:restriction>
   </xs:simpleType>
 
</xs:schema>

This is a very simple XSD document, and the XML documents validated by this XSD document will also be very simple.

The following is one invalid document for the given schema,
<holiday>wednesday</holiday>

To my opinion, this example has illustrated how the <assertion> facet refines the <enumeration> facet during simple type derivation, and also helps us clearly build a fine domain vocabulary. The types "WorkDay" and "Holiday" are verbally linked to the type "Day" in the XML Schema document as in this example.

Monday, October 3, 2016

XML Schema 1.1 : overlap in concept of CTA and "assert"

Here's another example, where I thought Conditional Type Alternative would have worked. It cannot in this case, because we have an attribute on element "X" and the data type is simple. Therefore we have to use an <assert> to solve this (because there's lot of conditional stuff here, an use case of XPath 2.0 "if", and a straight forward case of co-occurrence constraints).

Two valid XMLs:

(for attribute value 1, the simple type content of element "X" must be even)
<?xml version="1.0" encoding="UTF-8"?>
<X xa="1">
   4
</X>

(for attribute value 2, the simple type content of element "X" must be odd)
<?xml version="1.0" encoding="UTF-8"?>
<X xa="2">
   5
</X>

Invalid XML (attribute value other than 1 or 2 is not allowed):
<?xml version="1.0" encoding="UTF-8"?>
<X xa="3">
   4
</X>

The XML Schema 1.1 solution is below:

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

   <xs:element name="X">
      <xs:complexType>
         <xs:simpleContent>
            <xs:extension base="xs:int">
               <xs:attribute name="xa" type="xs:int"/>
               <xs:assert test="if (@xa = 1) then $value mod 2 = 0
                            else if (@xa = 2) then $value mod 2 = 1
                                else false()"/>
            </xs:extension>
         </xs:simpleContent>
      </xs:complexType>
   </xs:element>
 
</xs:schema>

I hope this example is helpful.

Monday, September 26, 2016

XML Schema 1.1 : Conditional Type Assignment revisited

One of the other features of XML Schema 1.1, that I like very much is "conditional type assignment", or CTA. The only requirement is, that there must be an attribute on an XML element to use this feature.

Here is a very simple example.

I'm directly writing an XML Schema 1.1 document below using CTA:

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

   <xs:element name="X">
        <xs:alternative type="Type1" test="@xa = 1"/>
        <xs:alternative type="Type2" test="@xa = 2"/>
        <xs:alternative type="xs:error"/>
   </xs:element>
  
   <xs:complexType name="Type1">
        <xs:sequence>
            <xs:element name="a" type="xs:int"/>
            <xs:element name="b" type="xs:int"/>
        </xs:sequence>
        <xs:attribute name="xa" type="xs:int"/>
   </xs:complexType>
  
   <xs:complexType name="Type2">
        <xs:sequence>
            <xs:element name="p" type="xs:int"/>
            <xs:element name="q" type="xs:int"/>
        </xs:sequence>
        <xs:attribute name="xa" type="xs:int"/>
   </xs:complexType>
 
</xs:schema>

The requirement of XML Schema 1.1 validation in this case is: If the attribute "xa" on element "X" has value 1, then element "X" has a certain type. If the value of attribute "xa" is 2, then element "X" has another type.

The two valid XML documents for the given XML Schema document are following:

<?xml version="1.0" encoding="UTF-8"?>
<X xa="1">
    <a>1</a>
    <b>2</b> 
</X>

and,

<?xml version="1.0" encoding="UTF-8"?>
<X xa="2">
  <p>1</p>
  <q>2</q> 
</X>

For anything else as value of attribute "xa", or infact any other kind of content the element "X" will be assigned the type xs:error (which makes the element "X" invalid").

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.