I've been imagining that, what could be useful use cases of XML Schema (XSD) 1.1 <assert> construct.
According to the XSD 1.1 structures specification, "assertion components constrain the existence and values of related XML elements and attributes".
One of useful use cases possible for XSD 1.1 <assert> is, to constrain the standard behavior of XSD 1.0 / 1.1 <choice> construct. I'll attempt to write something about this, here on this blog post.
Below is an XSD schema example using the <choice> construct, that is correct for both 1.0 and 1.1 versions of XSD language:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="X">
<xs:complexType>
<xs:choice>
<xs:element name="a" type="xs:string"/>
<xs:element name="b" type="xs:string"/>
<xs:element name="c" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
The above schema document, ensures that following XML instance documents would be valid:
<X>
<a>some string</a>
</X>
,
<X>
<b>some string</b>
</X>
,
<X>
<c>some string</c>
</X>
(essentially showing that, element 'X' can have only one of the elements 'a', 'b' or 'c' as a child element)
Lets see how the above XSD example, can be made a little different using XSD elements <attribute> and <assert>. Below is such a modified XSD document,
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="X">
<xs:complexType>
<xs:choice>
<xs:element name="a" type="xs:string"/>
<xs:element name="b" type="xs:string"/>
<xs:element name="c" type="xs:string"/>
</xs:choice>
<xs:attribute name="isB" type="xs:boolean" use="required"/>
<xs:assert test="if (@isB = true()) then b else not(b)"/>
</xs:complexType>
</xs:element>
</xs:schema>
The complete meaning of above XSD document is following,
1) The <choice> with three <element> declarations below it, essentially are the same constraints as the earlier XSD document has shown.
2) This schema additionally specifies, a mandatory boolean typed attribute named 'isB'.
3) The <assert> specifies that, if value of attribute 'isB' is true then element 'b' must be present as a child of element 'X'. If value of attribute 'isB' is false, then element 'X' cannot have element 'b' as its child but one of elements 'a' or 'c' would be a valid child of element 'X'.
The following XML instance documents would be valid according to above mentioned XSD document:
<X isB="1">
<b>some string</b>
</X>
,
<X isB="0">
<a>some string</a>
</X>
,
<X isB="0">
<c>some string</c>
</X>
And, the following XML instance documents would be invalid according to the same XSD document:
<X isB="0">
<b>some string</b>
</X>
,
<X isB="1">
<a>some string</a>
</X>
,
<X isB="0">
<d>some string</d>
</X>
Now lets consider another XSD example, where the schema document specifies a choice between three or more sequences. Below is mentioned such a schema document:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="X">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element name="a" type="xs:string"/>
<xs:element name="b" type="xs:string"/>
</xs:sequence>
<xs:sequence>
<xs:element name="p" type="xs:string"/>
<xs:element name="q" type="xs:string"/>
</xs:sequence>
<xs:sequence>
<xs:element name="x" type="xs:string"/>
<xs:element name="y" type="xs:string"/>
</xs:sequence>
</xs:choice>
<xs:attribute name="isSeqTwo" type="xs:boolean" use="required"/>
<xs:assert test="if (@isSeqTwo = true()) then p else not(p)"/>
</xs:complexType>
</xs:element>
</xs:schema>
The complete meaning of above XSD document is following,
1) A <choice> is specified between three <sequence> elements. Therefore, element 'X' can have one of following sequences as its child: {a, b}, {p, q} or {x, y}.
2) This schema additionally specifies, a mandatory boolean typed attribute named 'isSeqTwo'.
3) The <assert> specifies that, if value of attribute 'isSeqTwo' is true then sequence {p, q} must be present as a child of element 'X'. If value of attribute 'isSeqTwo' is false, then element 'X' cannot have sequence {p, q} as its child but one of sequences {a, b} or {x, y} would be a valid child of element 'X'.
The following XML instance documents would be valid according to above mentioned XSD document:
<X isSeqTwo="1">
<p>string1</p>
<q>string2</q>
</X>
,
<X isSeqTwo="0">
<a>string1</a>
<b>string2</b>
</X>
,
<X isSeqTwo="0">
<x>string1</x>
<y>string2</y>
</X>
And, the following XML instance documents would be invalid according to the same XSD document:
<X isSeqTwo="0">
<p>string1</p>
<q>string2</q>
</X>
,
<X isSeqTwo="1">
<a>string1</a>
<b>string2</b>
</X>
,
<X isSeqTwo="0">
<i>string1</i>
<j>string2</j>
</X>
All the above examples, and any other XSD 1.0/1.1 constructs may be used with any standards compliant XSD validator.
That's about all I wanted to say, about this topic.
Saturday, February 15, 2020
Saturday, January 25, 2020
Apache Xerces-J 2.12.1 now available
On behalf of Apache Xerces XML project team, I'm pleased to share that version 2.12.1 of Apache Xerces-J is now available. For more information about this new Xerces-J release and to download Xerces-J, please visit the Xerces-J site.
Friday, March 29, 2019
XSLT 1.0 transformations for large xml input documents
I thought that, this topic could be of interest to XML community.
I've discovered an interesting aspect of JAXP API (Java API for XML Processing) that seems to have relations to streaming that we talk with XSLT 3.0. Please see following document, and an example given in its section 4.12 (that explains JAXP's StAX API and using it with JAXP's transformation APIs)
Using the cited JAXP code in above document, one can transform very large XML input documents (I've tried an XML input document with size of about 700 MB, that worked) using XSLT 1.0 (the JDK's built in JAXP implementation can do this. I've tried with JDK 1.8 which works fine for this). It can do certain kinds of XSLT 1.0 transformations with very large XML input documents, very well. When doing the same transformations with XSLT 2.0, or with XSLT 3.0 in non streaming way, we would usually get following errors 'java.lang.OutOfMemoryError: Java heap space'.
I've written few complete examples for this topic here, https://github.com/mukulga/largexml_xslt10.
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" control 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>.)
1) Using "if" control 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.
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.
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.
Subscribe to:
Posts (Atom)