Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

IBM WebSphere Developer Technical Journal: XML and WebSphere Studio Application Developer -- Part 1

Developing XML Schema

Christina Lau (clau@ca.ibm.com), Senior Technical Staff Member, IBM
Christina Lau is a Senior Technical Staff Member at IBM. She currently leads the XML effort in WebSphere Studio Application Developer.

Summary:  This is Part 1 of a series that focuses on the XML tools provided with WebSphere Studio Application Developer. Part 1 describes the XML Schema Editor, a visual tool that supports the building of XML Schema that conforms to the XML Schema Recommendation Specification.

Date:  21 Nov 2001
Level:  Introductory

Activity:  5507 views
Comments:  

Introduction

IBM ® WebSphere® Studio Application Developer is a new application development product that supports the building of a large spectrum of applications using different technologies such as JSPTM, servlets, HTML, XML, Web services, databases, and EJBs. This is Part 1 in a series of articles that focuses on the XML tools provided with Application Developer. Part 1 covers the XML Schema Editor. The XML Schema Editor is a visual tool that supports the building of XML Schema that conforms to the XML Schema Recommendation Specification (May 2001). For more information on XML Schema, go to the W3C XML Schema Web site.

We recommend that you read the other articles in this series:

  • Part 1 Learn how to use Application Developer to develop XML Schema.
  • Part 2 Learn how to create an SQL query using Application Developer's SQL Builder.
  • Part 3 Learn about the Application Developer features available to incorporate data access and XML in your application.
  • Part 4 Learn how to use the XML Editor, a visual tool for creating and editing XML documents.
  • Part 5 Learn how to use the RDB to XML Mapping Editor to create DAD files for use with DB2 XML Extender.
  • Part 6 Learn how to use the XML Schema Editor and the XML Editor together to develop XML applications that make use of XML namespaces.

Creating a simple XML Schema

In this section, we will create a new XML Schema for a university. To create a new schema from scratch, use the New XML Schema wizard as follows:

  1. Select Perspective => Open => Other => XML to switch to the XML Perspective.
  2. Select File => New => Project => Simple => Project to bring up the New Project wizard to create a simple project. We can call the project, XMLProject.
  3. Select File => New => XML Schema to launch the New XML Schema wizard.
  4. In the File name field, type University.xsd.
  5. Click Finish. The University.xsd file is created and the XML Schema Editor is automatically opened for you.

Figure 1. XML Schema Editor
XML Schema Editor

The XML Schema Editor has three main views: Outline View, Design View, and Source View. You can use the Outline View to add, remove or rearrange components in your schema. When you select an object in the Outline View, the Design View will display the properties that are associated with that schema component object. You can use the Design View to enter values for the selected object. You can switch to the Source View to edit the schema source directly. The XML Schema Editor also uses the Task View from the workbench for errors reporting.


Using the Outline View to add schema components

The XML Schema specification defines a large number of components such as schema, complexType, simpleType, group, annotation, include, import, element, and attribute, etc. To create a valid schema, you must understand the containment relationships between these components. For example, an annotation element can only appear as the first child of any element. The include, import or redefine elements must appear before any other children of the schema element. An attribute can only be added to a complex type, but not a simple type. A group can only be defined at the schema level, but can be referenced by a complex type, etc.

The XML Schema Editor removes the burden to remember all these details for you. You can use the Outline View to add schema components via the pop-up menu. The pop-up menu will only display the list of objects that are relevant for the selected object. It will also add the object at the correct location in the XML Schema. The following tutorial will show you some of the basic methodology.


Figure 2. Pop-up on schema file level
Pop-up on schema file level

Step 1: Adding a complex type and a simple type

The University.xsd file contains a complex type, Student, for defining student information and a simple type, Faculties, for listing the faculties in the university.

  1. In the Outline View, click on the University file object, and select Add Complex Type from the pop-up menu. This will create a new complex type with the name, New_ComplexType1. In the Design View, change the name to Student.
  2. Click on the University file object again, and select Add Simple Type. This will create a new simple type with the name, New_SimpleType1. In the Design View, change the name to Faculties.

Step 2: Adding contents to the complex type

A student type contains a student name, the student's major, and her ID. We will add this information to the Student complex type by completing the following:

  1. Click on the Student complex type, and select Add Content Model. This will create a new sequence object, represented by a "," icon in the outline view.
  2. Click on the sequence (,) object, and select Add Element from the pop-up menu. This will create a new element with the name, New_Element1. In the Design View, change the element name to name. Notice the type of this element is defaulted to string.
  3. Click on the sequence (,) object, and select Add Element again to add the element, major. In the Design View for the major element, change the type of this element to the simple type, Faculties, by selecting the User-defined simple type radio button
    Figure 3. Changing the type of an element to a user-defined simple type
    Changing the type of an element to a user-defined simple type

    Notice that the prefix for the simple type, Faculties, is set to University. The is the default prefix for referring to types in this schema. We will discuss target namespace and namespace prefix below.

  4. Click on the Student complex type. Select Add Attribute to add the attribute, id . In the Design View for the id , change the id type to integer.

Step 3: Adding enumeration to the simple type

The simple type, Faculties, is derived by restriction from the simple type string and its value is limited to one of the faculties in the university. To define this list, we create the enumeration facet as follows:

  1. Click on the Faculties simple type, and select Add Restriction. This will create a new restriction element with a base type of string.
  2. Click on the restriction object and select Add Enum. This will create a new enumeration element. In the Design View, change the value to science.
  3. Repeat to add the enumeration, arts and dentistry, to the Faculties simple type.

Step 4: Adding a global element

To create an instance document from this XML Schema, the XML Schema must have a global element. We will add a global element student as follows:

  1. Click on the University file object, and select Add Global Element from the pop-up menu. This will create a new global element with the name, New_GlobalElement1. In the Design View, change the name to student.
  2. Click on the student global element. In the Design View for the student global element, change its type to the complex type, Student, by selecting the User-defined complex type radio button.

The completed schema from the source view is shown in Figure 4 below.


Figure 4. Completed University.xsd schema
The completed University.xsd schema

Making changes and referential integrity

As a schema becomes bigger and more complex, there will be more type definitions, and references to those types. So what happens after you have defined a type, created ten references to that type, and you want to change the name of the type? The XML Schema Editor has a built-in referential integrity mechanism that will propagate the changes automatically, freeing you from the tedious and error-prone task of doing the manual updates. The following illustrates this feature.

In our University.xsd file, we have defined a simple type called Faculties. There is a reference to this type in the Student complex type. Let's say we want to change the simple type to Faculty.

  1. Switch to the Design View.
  2. Select the Faculties simple type in the Outline. In the Design View, change it to Faculty.
  3. Now switch over to the Source View. Notice how the type for the element major is changed to University:Faculty automatically.

The XML Schema Editor's referential integrity mechanism is not limited only to name change. The same rule applies when you delete a schema component. For example, if you delete a type, all references to that type will automatically be reset to the default string data type. Whenever such an automatic update occurs, an information message will be displayed in the Task View. You can always invoke the undo action if you want to change your mind.

One thing to note is that the built-in referential integrity mechanism will only be enforced if the change is made from the Design View. If you make the changes directly by typing in the Source View, then it is your responsibility to ensure that you make all the changes correctly. Any change you made in the Source View will automatically be reflected in the Design and Outline View.


Namespace

Namespace provides a way to identify where an element or attribute comes from. For example, two elements from two different schemas might have the same name (for example, Product). To identify them, let's say Company A's Product vs. Company B's Product, we can use the XML Namespaces mechanism to distinguish them.

This section provides a quick introduction on how to define namespace for your schema in the XML Schema Editor. In future articles, we will discuss the complete topic of namespace design as it relates to creating instance documents, and using constructs from other schemas via the include, import or redefine elements.

In the University.xsd schema, the target namespace is http://www.ibm.com. This is indicated by the targetNamespace attribute in the schema element. This means that all the types that are defined in this schema belong to the target namespace http://www.ibm.com.

The following line on the schema element defines the prefix University for this target namespace:

 
xmlns:University="http://www.ibm.com" 

To refer to the type defined in this schema, you must use this defined prefix. Referring to the source in Figure 4 above, notice how the major element and the student element refers to the type as follows:

 
<element name="major" type="University:Faculties"/> 
<element name="student" type="University:Student"/> 


Changing the namespace prefix and target namespace

If you want to change the namespace prefix or the target namespace for your schema, you can use the Design View to do this. The following demonstrates this feature:

  1. Select the file object, that is University.
  2. In the Design View, change the Prefix field to univ.
  3. In the Design View, change the Target namespace field to http://www.utoronto.ca.
  4. Because such a change has a global impact to your entire document, you have to press the the Apply button to make this global change.
  5. Switch over to the Source View. You will notice that the attributes on the schema element and all the prefixes for the types are automatically changed for you.

Namespace for XML Schema Language constructs

Just as we can define a prefix for the types that we defined in our own schema, we can also associate a prefix for the vocabulary of the XML Schema Language itself. A common convention is to use the prefix xsd: to associate with the XML Schema namespace through the declaration

xmlns:xsd="http://www.w3.org/2001/XMLSchema

but any other prefix such as xs: or foo: will be equally valid.

If you want to create schemas that follow this pattern, you can indicate such preference by going to the Window => Preferences => XML => XML Schema page. Select the Qualify XML Schema language constructs check box. Accept the default XML Schema language prefix (xsd) or specify a new prefix.


Figure 5. XML Schema Preference Page
XML Schema Preference Page

Once you have specified this preference, any new schema that you create will have the xsd prefix appear in all the vocabulary XML Schema Language, such as xsd:complexType and xsd:string. The following shows a small snippet of such a schema.

<?xml version="1.0" encoding="UTF-8"?>  
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
            targetNamespace="http://www.ibm.com"  
            xmlns:University="http://www.ibm.com">  
 
    <xsd:complexType name="Student">  
        <xsd:sequence>  
            <xsd:element name="name" type="xsd:string"></xsd:element>  
        </xsd:sequence>  
    </xsd:complexType>  
</xsd:schema> 


Validating schema

Another useful feature of the XML Schema Editor is the incremental validation feature. At any point during your development of an XML Schema, you can invoke the Validate button to validate the schema. The validation is also automatically run when you save the document, or when you finish typing in the Source View (as indicated by switching focus to a different view). Any validation errors will be reported in the Task View with a little red marker for the corresponding line in the Source View and the corresponding object in the Outline View.

Also, when making changes in the Design View, pay attention to the status bar. It will contain hints for potential errors in the schema that you are developing.


Summary

This paper provides a birds-eye view of the XML Schema Editor that is included in WebSphere Studio Application Developer. In future articles, we will cover more advanced topics such as:

  • Creating schemas from multiple documents
  • Identity constraints
  • Generating JavaTM beans from XML Schema
  • Generating XML documents from XML Schema
  • How wildcard works

Please let us know what you think.

Top of page


About the author

Christina Lau

Christina Lau is a Senior Technical Staff Member at IBM. She currently leads the XML effort in WebSphere Studio Application Developer.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=14446
ArticleTitle=IBM WebSphere Developer Technical Journal: XML and WebSphere Studio Application Developer -- Part 1
publish-date=11212001
author1-email=clau@ca.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers