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.
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:
- Select Perspective => Open => Other => XML to switch to the XML Perspective.
- Select File => New => Project => Simple => Project to bring up the New Project wizard to create a simple project. We can call the project, XMLProject.
- Select File => New => XML Schema to launch the New XML Schema wizard.
- In the File name field, type
- Click Finish. The
University.xsdfile is created and the XML Schema Editor is automatically opened for you.
Figure 1. 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.
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
Step 1: Adding a complex type and a simple type
University.xsd file contains
a complex type, Student, for defining student information and a simple type,
Faculties, for listing the faculties in the university.
- 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.
- 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:
- 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.
- 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.
- 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
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.
- 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:
- Click on the Faculties simple type, and select Add Restriction. This will create a new restriction element with a base type of string.
- 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.
- 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:
- 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.
- 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
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.
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
- Switch to the Design View.
- Select the Faculties simple type in the Outline. In the Design View, change it to Faculty.
- 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 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.
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:
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"/>
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:
- Select the file object, that is University.
- In the Design View, change the Prefix field to univ.
- In the Design View, change the Target namespace field to http://www.utoronto.ca.
- 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.
- 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.
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
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
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>
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.
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.