Thursday, March 20, 2008

Eclipse Schema Editor

Working with schema files in Eclipse


A schema (XSD) is a complex language for describing the structure of your XML data. Writing or modify a schema file is quite difficult, but most the times it is required to write a new schema or understand an existing file because it the only widely used standard for describing xml structure (others are: 1) DTD, which is not used these days as it has lot of short commings including suport for namespaces 2) Relax NG). However schemas are widely used in Web services (for describing the structure of input data a service can accept – specified in wsdl types section). Check this link for more details about different languages for describing XML document http://www.xml.com/pub/a/2001/12/12/schemacompare.html

However Eclipse WTP plugin has a nice graphical Schema (XSD) editor that allows you to create, view and edit a schema file easily, effectively and without much pain in understanding the schema language.


This presentation gives you a basic introduction and features present in schema editor.


1) Configuring Eclipse

If Eclipse is not readily available on your system, you can download it from http://www.eclipse.org/downloads/, download “Eclipse IDE for J2EE development”.

Check and Install Schema editor

Click Help -> About Eclipse platform -> plugin details button





Verify that there is a plugin with name “XML Schema Editor”. If Schema editor is not installed please follow the below steps to install a Schema editor.


Installing a WTP plugin if not installed

The best way to install eclipse plugins is to use “Software updates” instead of manully downloading zip file and extracting it to the eclipse folder. “Software updates” not only downloads the dependent plugins but also downloads the correct version of the plugin based on your current version of Eclipse.

Help -> Software updates -> Find and Install this open up “Install/Update” dialog. “Choose search for new features to install” option and click next, select “Web Tools platform” and click finish. This will download the latest version of WTP plugins for your Eclipse enviroment.


2) Creating a new schema file

Now we try to create a schema file for a shipment notice data (sample data is shown in figure). Even though there are tools that allows to generate schema from a sample xml file (available in XML Spy and Stylus studio), the idea here to familarize with Eclipse schema editor.


<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"

xmlns="urn:shiporder">

<orderperson>John Smith</orderperson>

<shipto>

<name>Ola Nordmann</name>

<address>

<street>Langgt 23</street>

<city>4000 Stavanger</city>

<country>Norway</country>

<pincode>35004</pincode>

</address>

</shipto>

<item seq="1">

<title>Empire Burlesque</title>

<note>Special Edition</note>

<quantity>1</quantity>

<price>10.90</price>

</item>

<item seq="2">

<title>Hide your heart</title>

<quantity>1</quantity>

<price>9.90</price>

</item>

</shiporder>


The shiporder is the root tag that has a orderid attribute and it can has detailed about name of the person who ordered the items (orderperson) and his address (address tag). It also contains the items he has order (item tag, he can order different items using same request – hence item tag can be one or more). Address contains an street, city, country and pincode. Let us assume that country is an optional tag and if it is not given the country will be US. Also the country is fixed list like US, UK, India, Japan. Pincode is five digit number. The item has an seq attribute (which is interger and a unique), title, quantity (a positive five digit integer) and price (a positive double value).


Now let us see how we construct a xml schema using the above constaints using Eclipse WST editor.


I assume you don't have in depth knowledge about xml schemas (that is the reason why you and I should use an Editor – a person who wrote the schema spec can easily write schemas using notepad :-). However basic idea about schemas is required – able to identify an Element, Type (complexType or simpleType) and attributes.


1) Create a new project, XMLSamples – using File -> New project and selecting “Project” under the General section




2) Click next, and type “XMLSample” in Project name field and click Finish – This will create a new Eclipse project named “XMLSample”





3) Create a new Schema file, by using new file wizard: File -> New -> Others and selecting “XML Schema” under XML section as shown in figure.



4) Click next and type “shiporder.xsd” and click finish.




This will create a file “shiporder.xsd” in the XMLSamples project and opens in the editor. Go the design view page (if is opened in source page).




The Eclipse schema editor has five section in the Design page

  1. Directives – Other schema files that are included or improted in this schema will be listed here.

  2. Elements – All elements that the defined this schema will be shown here

  3. Types – All types that the defined in this schema will be shown here

  4. Attributes – Attributes defined will be listed in this section

  5. Groups – Goups will be listed here.


In addition to the Design page, you need to know about Outline view and Properties view.

If these views are not visible, you can bring them up by selecting Window -> Show view -> Others and selecting Outline view and clicking ok (same way to bring the Properties view also) – we see later how to use these views.




Now, let us start adding element to our schema file.


The root tag should be shiporder that contains orderperson, shipto and item tags. Also the schema elements should be in the namespace “urn:shiporder”

Right click the Element section and select Add Element menu item. Make the element names as “shiporder”.




You can rename the element name by click on the name text (or using Properties view).


By default XSD Editor creates a element of type string, but as shiporder contains others tags like ordername, so let us change the type of the shiporder.
























This will open up the New type dialog, create a complex type with name “shiporderType”


Now double click the shiporderType item present in the Types section. Right click on the shiporderType and select “Add Element”. This will add a new element of type string. Add three element and name it as “orderperson”, “shipto” and “item” - same names as the element tags we want inside the shiporder tag.




But again our shipto and item tags are of complex type not simple type like string or int. So, right click on the shipto column and select menu item Step type -> New, this open up the “New type” dialog we saw previously. Name it as shiptoType. Similarly do it for item also.



The final output should look like:


Now we need to add street, city, country and pincode for shiptoType and title, quantity and price for itemType.

You can do it by right click on the shiptoType and selecting “Add Element”


To change the type from string to int, you can click on the type and select int as shown in figure.


Adding all the tags the screen will look like:


By now we have created all the require tags.


Adding constraints


item” can occur one or more time in the shiporder. To make this right click on the item and select “Set multiplicity -> 1...*” menu item



Alternatively you can change the Min occurance and max occurance using properties view. If properties view is not visible, can make it appear by selecting the menu item “Window -> Show view -> Others” and select “Properties” in the general section..




This will open the Properties view.


Click on each item will update the properties view to show the properties of that selected item.

When we select the item in Design view the properties will look like:




You can set the minimum occurrence and maximum occurrence of the element.


Also our pincode should be a five digit interger. To apply this contraint, click on the “pincode” and select the “constraints” tab in properties view. In the “Specify constraint values” group choose “Patterns” option and click “Add” button.



This will bring up the “Regular Expression wizard”, type the regualar expression “[0-9]{5}” which specifies any letter from 0 to 9 and should occur five times. Click next to test your regualar expression and then click “Finish”.
































































This will make the pincode to be an integer with only five characters (or digits).


To make the quantity a positive integer, click on the “quantity” tag and in the constraints tab of properties view, give minimum value as “0”.


You can do the same for price also to make it a positive double.




Finally let us make our country tag to accept only a fixed set of values like “US, UK, France, Germany, Japan and India” using the Properties view.


To do this, click on the country item in the Design page and in the properties view select “Constraints” tab and choose “Enumerations” in the “Specific constraint values” group box. Click on Add and give a name “India”, to add one more item to the list, click “Add” button again and give “US” as value this time.




Now save the file (using Ctrl + s keyboard short cut), you can view the source by click on the source tab:




This completes our creating schemas using XML Schema editor.


2 comments:

Manuel said...

Hello Suresh,

Helpful post for newcomers to the eclipse schema editor.

Although i couldn't see how you realized the "uniqueness" of the seq attribute.

Kind regards,
Manuel

Unknown said...

Thanks for posting !! Now Schema become vital part of the website HTML code to give more information to search engines. Schema creator for local business is one of the best tool for local business.

Copyright (c) 2008 - Suresh