Not long ago, XSLT 3.0 has reached the W3C Recommendation status. Below is the link to the XSLT 3.0 spec:
https://www.w3.org/TR/2017/REC-xslt-30-20170608/
XSLT 3.0 is a very advanced and useful language, as compared to XSLT 2.0. One of the main features (among others) introduced in XSLT 3.0, is that the XSLT transformation can be done in streaming mode.
Tuesday, June 13, 2017
Thursday, June 1, 2017
XPath 2.0 atomization with XML Schema 1.1 validation
XPath 2.0 atomization as a concept, as applicable to XML Schema 1.1 validation is worth looking at. I would attempt to write something about this topic, here in this blog post.
Lets look at the following XML Schema 1.1 validation example, that we'll use to discuss this topic.
XSD 1.1 document:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="X">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:integer"/>
<xs:element name="b" type="xs:integer"/>
</xs:sequence>
<xs:assert test="a gt b"/>
</xs:complexType>
</xs:element>
</xs:schema>
XML instance document that is validated with above mentioned XSD document:
<?xml version="1.0"?>
<X>
<a>4</a>
<b>7</b>
</X>
Upon XML Schema validation, the above mentioned XML instance document would be reported as invalid, because numeric value of "a" is less than "b". Now what is XPath 2.0 atomization, as for in this example that I wish to talk about?
Since the XML document has been validated with the mentioned XSD document, while building the XPath data model tree to evaluate <assert>, the nodes of XPath tree are bound with the XSD types as mentioned in the XSD document. Therefore, the <assert> XPath 2.0 expression "a gt b", comes with runtime availability of the corresponding XSD types on <assert> tree nodes for elements "a" and "b". In XPath 2.0 terms, the values as a result of atomization operation of nodes for XML elements "a" and "b" are used when an XPath expression "a gt b" is evaluated. We can't test a greater/less than relation on XML nodes, but we can do that on numbers for example, and the conversion of XML runtime nodes to atomic values like number is what XPath 2.0 atomization achieves.
I've used Apache Xerces as an XSD 1.1 validator, for testing examples for this blog post.
Lets look at the following XML Schema 1.1 validation example, that we'll use to discuss this topic.
XSD 1.1 document:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="X">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:integer"/>
<xs:element name="b" type="xs:integer"/>
</xs:sequence>
<xs:assert test="a gt b"/>
</xs:complexType>
</xs:element>
</xs:schema>
XML instance document that is validated with above mentioned XSD document:
<?xml version="1.0"?>
<X>
<a>4</a>
<b>7</b>
</X>
Upon XML Schema validation, the above mentioned XML instance document would be reported as invalid, because numeric value of "a" is less than "b". Now what is XPath 2.0 atomization, as for in this example that I wish to talk about?
Since the XML document has been validated with the mentioned XSD document, while building the XPath data model tree to evaluate <assert>, the nodes of XPath tree are bound with the XSD types as mentioned in the XSD document. Therefore, the <assert> XPath 2.0 expression "a gt b", comes with runtime availability of the corresponding XSD types on <assert> tree nodes for elements "a" and "b". In XPath 2.0 terms, the values as a result of atomization operation of nodes for XML elements "a" and "b" are used when an XPath expression "a gt b" is evaluated. We can't test a greater/less than relation on XML nodes, but we can do that on numbers for example, and the conversion of XML runtime nodes to atomic values like number is what XPath 2.0 atomization achieves.
I've used Apache Xerces as an XSD 1.1 validator, for testing examples for this blog post.
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.
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.
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.
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.
Subscribe to:
Posts (Atom)