Simple Type

A simple type of element or attribute has only text data and does not contain child elements or attributes within it. The data types of simple type can be date, time, boolean, string, decimal. It can be used to create new a type. Simple type is used for element and attribute declarations.

Example of simple type of XML schema

<xs:element name="itemName">

   <xs:simpleType>

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

      <xs:pattern value=”([A-Z][a-z])+” />

</xs:restriction>

   </xs:simpleType>

</xs:element>

The name is embedded simple style element.

A simple type declarations can have restrictions on the acceptable values on elements and attributes called facets. The restrictions can be used when a element or attribute expects certain kind of inputs. For example, telephone input accepts only numbers and maximum number of digits is 10, item name can be only in alphabets, with first word capitals and rest in small letters (Ex, Toy, Bag …etc)

The restriction conditions in XML Schema are called restriction facet. The restriction facets used in Schema are:

  • length - it defines the length of the value.
  • minLength - it defines the length of the minimum value
  • maxLength - it defines the length of the maximum value
  • pattern - it defines a sequence of characters which is acceptable.
  • enumeration - it defines the allowable values for an element or attribute
  • minInclusive - it defines minimum value of the value range (including the given value)
  • maxInclusive - it defines maximum value of the value range (including the given value)
  • minExclusive - it defines minimum value of the value range (does not include given value)
  • maxExclusive - it defines maximum value of the value range (does not include given value)
  • whiteSpace - Normalization of blank characters
  • totalDigits - it defines maximum number of digits allowed
  • fractionDigits - it defines the maximum number of decimals allowed

Complex type

A complex type of data has child elements or attributes defined in it. An element can be defined as <xs:complex type> and can have it’s own data types. Complex element can have empty elements, other elements, text and combination of elements and text as it’s content.

The child elements of complex type can occur in 7 orders:

  • all - All the child elements given must appear once and can appear in any order.
  • choice - It specifies one of the child element can occur.
  • sequence - It specifies child element can occur in specific order.
  • maxOccurs - Indicates maximum number of time an element can occur
  • minOccurs - Indicates minimum number of time an element can occur
  • group name - It groups the elements under a name.
  • attributeGroup name - It groups the attributes under a name.

The use of <any> element helps the user to extend the XML document with elements not defined in schema. The <anyAttribute> can be used to extend XML documents with attributes not defined in schema.

Example of complex type of XML schema

  <xs:element name="item">

  <xs:complexType>

    <xs:sequence minOccurs=”1” maxOccurs=”unbounded”>

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

         <xs:element name="cost" type="xs:decimal"/>

         <xs:element name="material" type="xs:string"/>

         <xs:any minOccurs=”1” />

   <xs: anyAttribute />

    </xs:sequence>

   </xs:complexType>

  </xs:element>

 

›› go to examples ››