XML Schema Definition Part 2


In this post we will begin to learn how to create our XML schema definitions from scratch. They are referred to as XSD.

The first example is a simple one based on a member of a fictitious company that sends out newsletters on tips and new about XML and XSD. The parent element is Person. There are several child elements. The UniqueId is 9 numerals long and cannot be longer or shorter. The first name, last name, email, gender and status are all test. Age must be a number. Gender can be M or F only. You can use Email or MobileNumber elements, but you cannot use both for the same member. It is “either/or”. The PaymentMethod is either CreditCard or Cash, but never both. It is one or the other.

<?xml version='1.0' encoding="UTF-8"?>
<Members>
	<!-- This is a comment Use Case: -->
	<!-- Memberships to an XML/XSD newsletter subscription. -->
	<UniqueId>430982341</UniqueId>
	<FirstName>John</FirstName>
	<LastName>Doe</LastName>
	<Email>jd873462@gmail.com</Email>
	<Age>42</Age>
	<Gender>M</Gender>
   <![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>
</Members>

Eclipse

If you have Eclipse installed you can create a new project in Eclipse to work with this XML example. Go to File, New, Project…, General, New Project, Next and type in a name for you new project. Call it MembersXML. Leave everything else and click on Finish. Once the IDE is ready, right-click on your new project name and select New from the pop-up menu. Click Other. In the Wizards box type xml. Under the XML templates choose the XML File and click on the Next button. Give your file a name such as Members.xml (or whatever you want). Click Finish.

You can start writing your own XML, or copy and paste the code above. To format the code in Eclipse, press Ctrl+Shift+F.

Schema Types

There are three different types defined in XSD: inbuilt, simple and complex. Please see the Introduction post for more information.

Series Navigation<< XML Schema Definition IntroductionXML Schema Definition Part 3 >>

Leave a comment

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