Sunday, July 29, 2018

Co-occurrence constraints and Conditional Type Assignment, with XML Schema 1.1

With respect to following article, that I wrote for XML.com : https://www.xml.com/articles/2018/05/29/co-occurrence-cta-xsd/ , I wish to add few more points to that article, via this blog post as mentioned below,

1) Using "if" expressions in an XSD <assert>

The XPath 2.0 "if" expression is an useful facility for an <assert>. I'll explain this, via a XML schema validation example, as mentioned below.

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="X">
     <xs:complexType>
        <xs:sequence>
           <xs:choice>
              <xs:element name="b" type="xs:integer"/>
              <xs:element name="c" type="xs:integer"/>
           </xs:choice>
           <xs:element name="a" type="xs:integer"/>
        </xs:sequence>
        <xs:attribute name="el" use="required">
           <xs:simpleType>
              <xs:restriction base="xs:string">
                 <xs:enumeration value="b"/>
                 <xs:enumeration value="c"/>
              </xs:restriction>
           </xs:simpleType>
        </xs:attribute>
        <xs:assert test="if (@el = 'b') then b else c"/>
     </xs:complexType>
  </xs:element>

</xs:schema>

(the XSD <choice> specifies, that either element "b" should occur or the element "c" should occur. this is further controlled by an <assert> constraint, which specifies that if value of attribute "el" is 'b' then element "b" should occur, otherwise element "c" should occur for the <choice>.)

Below are few XML instance documents, that can be validated with the above XSD document:

<X el="b">
  <b>100</b>
  <a>200</a>
</X>

(valid document)

<X el="b">
  <c>100</c>
  <a>200</a>
</X>

(invalid document, since the preceding-sibling of element "a" must be element "b")

<X el="p">
  <b>100</b>
  <a>200</a>
</X>

(invalid document, since the value of attribute "el" is not as per the simpleType definition of attribute "el". the <assert> would also fail.)

2) Using XSD <alternative> instead of <assert>

The XSD example mentioned in point 1) above, can easily be converted to an XSD document using <alternative> to solve the same use case. Below is a modified XSD 1.1 document, using <alternative> element solving the same use case as mentioned in point 1) above.

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

  <xs:element name="X">
     <xs:alternative test="@el = 'b'" type="B_type"/>
     <xs:alternative test="@el = 'c'" type="C_type"/>
     <xs:alternative type="xs:error"/>
  </xs:element>
  
  <xs:complexType name="B_type">
     <xs:sequence>
        <xs:element name="b" type="xs:integer"/>
        <xs:element name="a" type="xs:integer"/>
     </xs:sequence>
     <xs:attribute name="el" type="xs:string" use="required"/>
  </xs:complexType>
  
  <xs:complexType name="C_type">
     <xs:sequence>
        <xs:element name="c" type="xs:integer"/>
        <xs:element name="a" type="xs:integer"/>
     </xs:sequence>
     <xs:attribute name="el" type="xs:string" use="required"/>
  </xs:complexType>

</xs:schema>


Tuesday, May 8, 2018

Technology cloud regions

Following is an interesting high-level statistics, for the regional deployment of cloud services, on cloud platforms of few reputed vendors:

IBM Cloud:
The following regions are supported.
us-south
us-east
au-syd
eu-gb
eu-de

GCP:
https://cloud.google.com/compute/docs/regions-zones/

AWS:
https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/

Following is my analytical sense of this:
1) GCP and AWS provide many more cloud regions than IBM Cloud. The region coverage of GCP and AWS is almost same. A region is a particular geographic area where the cloud data-center is located. The user fetching the cloud hosted application from their nearest region, will result in lower latency and higher throughput. All cloud vendors provide an option to the merchant, to select the region where they'd like to host their cloud applications.
2) IBM supporting fewer regions, may even be good from the perspective of simplicity. IBM cloud regions seems to be uniformly distributed around the world. If building a truly global cloud hosted application, having fewer regions to think about may be a good thing. With this, it may be necessary to optimize the application at other parts of the infrastructure.
3) Although AWS is quite good for various reasons, it doesn't offer all services at all its regions. But overall, AWS is ok on this aspect.

Monday, May 7, 2018

XML vs. JSON discussion

In this article, https://www.xml.com/articles/2017/08/06/xml-vs-json-discussion/ G. Ken Holman very nicely explains the pros and cons of XML vs. JSON data formats. In this article, Ken seems not at all biased towards XML or JSON. I'm sure people having this concern will find the cited article useful.

Friday, May 4, 2018

Apache software mirrors

If you're interested to download software releases from Apache Software Foundation, you'd need to use one of the Apache mirror sites for the download. Usually, the download page of respective Apache project will present the user with the closest mirrors to the user's location. But sparingly, the mirror site presented by the project download page might not be responding (i.e there could be some problem with a specific mirror, like it may be down). You might then wonder, what should I do now, and I need this software?

Following is a handy site, to see the runtime status of all Apache mirrors: https://www.apache.org/mirrors/. Any mirror can be fully navigated via this link. Each mirror hosts all Apache projects. In a sparing situation, that the project download page presents with a non-responsive mirror, the users could go to the site https://www.apache.org/mirrors/ and download software from there by choosing an alternative mirror, and navigating to the respective project.

Usually, various Apache software download pages, allow user to select certain alternative mirrors. Refreshing the Apache project download pages usually also presents alternative mirror sites. If that solves the problem, then going to https://www.apache.org/mirrors/ for download may not be necessary.

I hope this could help someone, while downloading Apache softwares.

Wednesday, February 28, 2018

XML Schema 1.1 enhancements for simpleType with variety list

In this blog post, I wish to express my thoughts about new features that have been introduced in XML Schema 1.1 while defining XSD simpleType lists. I'd like to write few XML Schema validation examples here illustrating the same.

Example 1: Using the <xs:assertion> facet, to enforce sorted order on the list data.

Here's the XSD 1.1 document:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" vc:minVersion="1.1"
                    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning">

   <xs:element name="X">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="a" type="xs:integer"/>
            <xs:element name="b">
               <xs:simpleType>
                  <xs:restriction base="IntegerList">                   
                     <xs:assertion test="every $val in (for $x in 1 to count($value)-1 return ($value[$x] le $value[$x+1])) 
                                         satisfies ($val eq true())">
                        <xs:annotation>
                           <xs:documentation>
                              Assertion facet checking that, items in the list are in ascending sorted order.
                           </xs:documentation>
                        </xs:annotation>
                     </xs:assertion>
                   </xs:restriction>               
               </xs:simpleType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:simpleType name="IntegerList">
      <xs:list itemType="xs:integer"/>
   </xs:simpleType>

</xs:schema>

A valid XML document, when validated by the above schema document:

<?xml version="1.0"?>
<X>
   <a>100</a>
   <b>-20 1 2 3</b>
</X>

(the integer list in element "b", is sorted)

An invalid XML document, when validated by the above schema document:

<?xml version="1.0"?>
<X>
   <a>100</a>
   <b>-20 1 2 3 1</b>
</X>

(the integer list in element "b", is not in a sorted order)

Example 2: Using the <xs:assertion> facet, to enforce size of the list using relational operators other than equality (the equality was supported by XSD 1.0 using the xs:length facet).

Here's the XSD 1.1 document:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" vc:minVersion="1.1"
                    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning">

   <xs:element name="X">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="a" type="xs:integer"/>
            <xs:element name="b">
               <xs:simpleType>
                  <xs:restriction base="IntegerList">                   
                     <xs:assertion test="count($value) lt 10">
                        <xs:annotation>
                           <xs:documentation>
                              Assertion facet checking that, cardinality/size of list should be less than 10.
                           </xs:documentation>
                        </xs:annotation>
                     </xs:assertion>
                   </xs:restriction>               
               </xs:simpleType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:simpleType name="IntegerList">
      <xs:list itemType="xs:integer"/>
   </xs:simpleType>

</xs:schema>

A valid XML document, when validated by the above schema document:

<?xml version="1.0"?>
<X>
   <a>100</a>
   <b>-20 1 2 3</b>
</X>

(the integer list in element "b", has less than 10 items)

An invalid XML document, when validated by the above schema document:

<?xml version="1.0"?>
<X>
   <a>100</a>
   <b>-20 1 2 3 1 1 1 1 1 1 1</b>
</X>

(the integer list in element "b", has more than 10 items)

Example 3: Using the <xs:assertion> facet, to enforce that each item of list must be an even number.

Here's the XSD 1.1 document:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" vc:minVersion="1.1"
                    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning">

   <xs:element name="X">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="a" type="xs:integer"/>
            <xs:element name="b">
               <xs:simpleType>              
          <xs:list>
             <xs:annotation>
                <xs:documentation>
                   The simpleType definition below, is itemType of this list.
                </xs:documentation>
             </xs:annotation>
             <xs:simpleType>
                <xs:annotation>
                  <xs:documentation>
                      Every item of list must be an even number.
                  </xs:documentation>
                </xs:annotation>
                <xs:restriction base="xs:integer">
                   <xs:assertion test="$value mod 2 = 0"/>
                </xs:restriction>
             </xs:simpleType>
          </xs:list>
               </xs:simpleType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

</xs:schema>

A valid XML document, when validated by the above schema document:

<?xml version="1.0"?>
<X>
   <a>100</a>
   <b>2 4 6</b>
</X>

(the integer list in element "b", has each item as even number)

An invalid XML document, when validated by the above schema document:

<?xml version="1.0"?>
<X>
   <a>100</a>
   <b>2 1 6</b>
</X>

(the integer list in element "b", has one or more item not even)

As illustrated by the above examples, some new XML Schema validation scenarios are possible with the introduction of <xs:assertion> facet on XSD simple types in 1.1 version of the XML Schema language.

2018-04-11: I've been thinking about this post for a while, and wish to say something about sorting. I think, sorting as a problem in programming is more meaningful to solve, when we have to *do* sorting and not when, we have to determine whether some list is sorted or not. What my XSD Example 1 showed above, is *not doing* sorting but the other thing. Nevertheless, I'm happy to discover what my XSD shows.

Sunday, January 7, 2018

XML validation. Some thoughts

I think, there are various people using XML who like having XML data without any validation. I'm a strong proponent of having validation nearly always when using XML. Comparing the situation with RDBMS data, would make this clear I think (I don't mind proving things about a technology, taking cues from another technology which is hugely popular). Do we ever use data in RDBMS tables, without the schema (we don't)? The same should apply to XML, since validation is very closely defined alongside XML (DTD at least, and then XSD). If DTD or XSD is provided along with XML parsing, by the XML toolkit of choice, then why shouldn't we use validation whenever we're using XML -- as a consequence, we're working with a better design?

Interestingly, validation doesn't always happen when using XML, because it hasn't been made mandatory in the XML language (like schemas with RDBMS). People using XML, sometimes like having XML data quickly transported between components or stored locally -- and they don't use validation in the process; which is fine since it meets the needs of an application.

Sometimes, people using XML are influenced by how JSON is used. Presently, JSON doesn't has a schema language (but I came to know, that this may change in the future), and JSON is very popular & useful for certain use cases. Therefore, people try to use XML the same way -- i.e without validation.

Sunday, December 31, 2017

Xerces bug XERCESJ-1687

I wish to share my anguish, that following Xerces bug has caused me:

https://issues.apache.org/jira/browse/XERCESJ-1687

The bug reporter is very right in his arguments. But somehow I've to say, the Xerces team cannot fix this bug right now. I've also been thinking to "resolve" this bug with a fix reason "later" (but my conscience doesn't allow that either).

I hope the situation will improve.