Thursday, April 2, 2026

XML Schema 1.1 validation

I did try again after a very long time, writing an XML Schema 1.1 validation and using Xerces XML Schema 2.12.2 validator for verification.

I've used the following XML document input:

<?xml version="1.0" encoding="utf-8"?>

<products>

<product id="id1">

    <name>Abc</name>

    <desc>Description 1</desc>

        </product>

        <product id="id2">

    <name>Mno</name>

   <desc>Description 2</desc>

</product>

<product id="id3">

   <name>Pqr</name>

   <desc>Description 3</desc>

</product>

</products>

And the following XML Schema 1.1 document, which results in the above mentioned XML instance document as valid:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="products" type="PRODUCTS_TYPE"/>

   <xs:complexType name="PRODUCTS_TYPE">

          <xs:sequence>

<xs:element name="product" type="PRODUCT_TYPE" maxOccurs="unbounded"/>

  </xs:sequence>

   </xs:complexType>

   <xs:complexType name="PRODUCT_TYPE">

         <xs:complexContent>

      <xs:extension base="DUMMY_TYPE1">

                     <xs:sequence>

                  <xs:element name="name" type="CamelCaseString"/>

          <xs:element name="desc" type="CamelCaseString"/>

             </xs:sequence>

     <xs:attribute name="id" type="IDType"/>

</xs:extension>

  </xs:complexContent>

   </xs:complexType>

   <xs:complexType name="DUMMY_TYPE1">

      <xs:complexContent>

     <xs:restriction base="xs:anyType"/>

       </xs:complexContent>

   </xs:complexType>

   <!-- This XML Schema simple type definition, requires the string value

           to begin with substring "id" followed by a positive integer and then followed by any integer value. -->

   <xs:simpleType name="IDType">

         <xs:restriction base="xs:string">

      <xs:pattern value="id[1-9][0-9]*"/>

        </xs:restriction>

   </xs:simpleType>

   <!-- This XML Schema simple type definition, requires first character

        of string value to be an English capital letter. -->

   <xs:simpleType name="CamelCaseString">

      <xs:restriction base="xs:string">

     <xs:assertion test="substring($value, 1, 1) = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 

                                                'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',

'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')"/>

  </xs:restriction>

   </xs:simpleType>

</xs:schema>

No comments: