Build business-ready XML Schema with JDeveloper
If XML is being used increasingly to describe business actions, then XML Schema is becoming the language that enables companies to actually communicate clearly with, and to understand, each other.
While XML has become the standard medium for exchanging data, it's XML Schema that defines the structure, content and semantics of the XML documents involved in transactions.
Imagine a shipping company sends an XML document to a client containing details on a cargo shipment. The client won't be able to interpret the XML document if it contains elements and attributes it's not used to. An XML Schema can make the contents understandable.
In this piece, I shall explain how you can create and register an XML Schema, and then create an XML document from your XML Schema, using Oracle's JDeveloper 10.1.3 development environment.
I've selected JDeveloper because I've found it to deliver the most comprehensive XML Schema tools of its peers in the Java market. JDeveloper 10.1.3 features XML Schema support without need for a separate plug in, there's an integrated design view and the ability to instantiate an XML document from an XML schema.
Creating a Schema
In this section we shall create an XML Schema,
catalog.xsd, in JDeveloper's Schema Visual Editor. Before that, though, make sure you've got JDeveloper 10.1.3 installed and running on your machine, and then create an application and a project.
Now, add a Schema file to the JDeveloper project. Select the project node in Applications Navigator, and select File>New in the main menu. In the New Gallery window select the General>XML node in Categories. Subsequently select XML Schema in the Items listed. In the Create XML Schema window, specify a File Name. An XML Schema will be added to the project. In the Design view the different components of the schema are displayed in the schema visual editor. In the Source view, the schema document source is listed. The different schema components available to be added to the schema are listed in the Schema Components Component Palette.
You may add components to a schema from either the component palette or from a component node in the schema design view. As an example of adding a component from a component node, add root element,
catalog. Right-click on
<schema> node in Design view and select Insert inside
A schema component may also be added by selecting a node in Design View and selecting a component in the Component Palette. A component may also be added from the Component Palette to a Design view component by selecting the component in the Component Palette and dragging the component to the Design view component and dropping the component. An element will be added to
<schema> element. Set element name to
catalog, and element type to
catalogType in the Property Inspector.
Element name and type may also be specified by right-clicking on the element node and selecting Properties. In the element properties window specify element name and, in Advanced Properties, specify element type. In Advanced Properties
substitutionGroup values may also be specified. To delete exampleElement element, right-click on exampleElement node and select Delete. Next, add a complextType component to <schema> node from the Component Palette.
Set complexType name to
catalogType. Add a
sequence component to
catalogType and add an
element to the
sequence. Set attribute
journal in the
element node. Attribute
name should not be set for an element if
ref attribute is set. Specifying elements using the
ref attribute has the advantage that XML documents may be instantiated from an XML Schema fragment.
When elements in a complexType component are specified using the
ref attribute a corresponding global elements are also specified. When instantiating an XML document from the XML Schema any of the global elements may be selected as the root element. Also, set attribute
minOccurs to 0, and
unbounded in the
journal node. Next, add
journal element to
Now, add a complexType component
schema node. Add a
sequence component to the
journalType node and add an
article element to the
sequence. In the Property Inspector, set attribute
minOccurs to 0 and
Adding an xsd:element XML Schema Component to xsd:schema Element
When that's done, add attributes
journalType. To add an attribute, select
journalType node and select
attribute in the component palette. Set attribute name to
title, and type to
xsd:string. Similarly, add attributes
article element to schema node and set element type to
articleType. Having added an element of type
articleType, add a complexType component
articleType. Next, add a
articleType, add an
element node to the
sequence node, set element name to
title, and type to
xsd:string. Similarly, add element
author of type
xsd:string to the sequence.
articleType also has an attribute
section. Add an attribute to
articleType from the Component Palette. Set attribute name to
section and type to
xsd:string. Select File>Save to save the XML schema.
Adding a sequence to journalType
Registering a Schema
Registration means that you may create an instance of an XML document that conforms to the schema you've just created. To register a schema in JDeveloper 10.1.3, select Tools>Preferences in the main menu. In the Preferences window select the XML Schemas node. In the XML Schemas window, click on the Add button to register a schema. In Add Schema, click on Browse to select a schema then Select
.xml specifies the extension of file type for which a registered schema may be used to create an XML document instance.
Creating an XML Document from Schema
Now it's time to create an XML document instance,
catalog.xml, from the registered schema,
catalog.xsd. While we're using a registered schema, a registered schema is not essential as its also possible to create an XML document instance using an XML Schema in the file system.
Using a registered XML Schema, though, does have the advantage that you may associate it with a specific file extension. For example, to register an XML Schema for generating only
.xslt files you would associate the
.xslt extension with the XML Schema.
Select project node XMLSchema in Applications-Navigator, and select File>New in the main menu. In the New Gallery window select the General>XML node. Select XML Document from XML Schema in Items listed. The Create XML Document from XML Schema wizard will now get started.
Click Next, In File Location specify XML file name and then. select Use Registered Schemas and then click on Next. In Options, select the target namespace for the XML Schema. Select root Element as
catalog, and click on Next.
To create an XML document from an XML Schema fragment select another global element as the root element. The other options that may be specified are depth of the XML document to be generated in terms of element levels, encoding, and the option to generate only the required elements.
In the Finish window, click on Finish and an XML document instance will be added. The XML document will have all the elements defined in the XML Schema needed to instantiate the XML document.
Finally, we add element and attributes to the XML document to construct XML document
catalog.xml. Select the journal node in Structure Panel. In the Property Inspector specify values for the attributes
article element node in the Structure Panel and specify a value for
section attribute, and
To add another
journal element, position the cursor inside the closing tag of
catalog element, and select
journal in component palette. A
<journal/> node will be added to
catalog element. Select
journal node in Structure Panel and add values to
edition attributes in Property Inspector.
Next, add an
article element node to
journal element. Position your cursor inside the closing tag of the
journal element and select
article from the component palette. In the Insert Article window specify the values for
title elements. Click on Advanced Properties,specify a value for
section attribute and then click on OK for the
Catalog.xml to be created.
Constructing an XML Document
You're new armed with the practical knowledge needed on to create a recognizable and commonly understandable XML document using an XML Schema you've created using Oracle's JDeveloper IDE.