XML Schema Definition Part 6


XSD Simple Type Restrictions

One of the advantages of using a schema definition with your XML files is that you can validate the data. For example you can set the maximum string length. You can also force the data to be only an integer using the numerals zero to nine.

The best way to learn these is to use some examples.

Some of these examples are from the online course at www.Udemy.com. The course is called XML and XML Schema Definition in Easy Steps by Bharath Thippireddy. Here we have a Patient at a hospital or clinic. This is not the complete listing. You can see that the prefix is tns. There is a maximum string length set for the name. The age is an integer. You can have as may phone numbers as you want because the maxOccurs is set to unbounded. One of the more interesting ones is the enumeration in the gender. You can only have M or F.

	<complexType name="Patient">
		<sequence>
			<element name="name" type="tns:string15chars" />
			<element name="age" type="int" />
			<element name="email" type="string" minOccurs="0" />
			<element name="gender" type="tns:Gender" />
			<element name="phone" type="string" maxOccurs="unbounded" />
			<element name="paymentType" type="tns:PaymentType" />
		</sequence>
		<attribute name="id" type="common:Id" use="required" />
	</complexType>

	<simpleType name="string15chars">
		<restriction base="string">
			<maxLength value="15" />
		</restriction>
	</simpleType>

	<simpleType name="Gender">
		<restriction base="string">
			<enumeration value="M" />
			<enumeration value="F" />
		</restriction>
	</simpleType>

Another way to do the same thing (M or F) would be to use a pattern inside the restriction. Here is the code for that.

<pattern value="M|F"/>

Here is a modified example from www.w3schools.com. Using minInclusive and maxInclusive. The following example defines an element called “age” with a restriction. The value of age cannot be lower than 0 or greater than 120.

<element name="age">
  <simpleType>
    <restriction base="xs:integer">
      <minInclusive value="0"/>
      <maxInclusive value="120"/>
    </restriction>
  </simpleType>
</element>

Here are some examples of string restrictions that you may put between the restriction tags, where the base would equal string. These are also from w3schools.com.

<pattern value="[a-z]"/>  <!-- is ONE of the lowercase letters from a to z  -->
<pattern value="[A-Z][A-Z][A-Z]"/>  <!-- THREE of the UPPERCASE letters from A to Z  -->
<pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> <!-- THREE of the LOWERCASE OR UPPERCASE letters from a to z -->
<xs:pattern value="[xyz]"/>  <!-- ONE of the following letters: x, y, OR z: -->

Choice

Even more interesting is choice. You may have requirements that specify one tag or the other but not both (either/or). For example, consider the payment type. You can pay by cash or credit card. If it is by cash we just need to have a field for amount. If it is by credit card, you need to know the amount, the card type, the card number, name, expiry date and perhaps more. Consider the following part of an XML document.

   <![CDATA[ 
   For Payment types, you can have cash or credit card, not both.
   For cash, all we need is the monthly amount in dollars, as shown below:
      <PaymentType>
         <Cash>12</Cash>
      </PaymentType>
   ]]>
	<PaymentType>
		<CreditCard>
			<Provider>VISA</Provider>
			<ExpiryDate>0518</ExpiryDate>
			<Amount>12</Amount>
		</CreditCard>
	</PaymentType>

Consider another example. Suppose you are providing your subscribers/customers the choice of whether they prefer to be notified by email or SMS message. You need their valid email address or their 7-digit mobile phone number, but never both, and you must have at least one.

Series Navigation<< XML Schema Definition Part 5XML Schema Definition Part 7 >>

Leave a comment

Your email address will not be published. Required fields are marked *