XML Schema – ComplexTypes, Namespaces and Validation


If you are trying to create a new XML schema and have a requirement where a schema element requires a “Complex” element in a repeated fashion then you might be thinking about defining that complex element outside and referencing it like basic XMLSchema types.

Note: You might like to go through the W3C Schools tutorial first – it can be found here

I am using JDeveloper 11g to build and validate my schema. There are two ways of creating a schema in JDeveloper – you can do “New-> XML Schema” or you can create a new SOA project and create a Composite with a BPEL and the schema will be a part of the BPEL project.  In either case make sure your namesspaces are defined correctly – i.e. you XMLSchema namespace and your target namespace definitions are clear, otherwise you might not be able to use a complexType you define in the document.

Here is a good read on this topic

For example, you can define your namespaces in the following manner, but this will only work for types in the XMLSchema namespace – and for complexTypes you will need to define them within the element:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.example.org" elementFormDefault="qualified">

If you have this format the following will not work – you cannot define “MyComplexTypeA” and use it as a type for the element “myTopLevelSchemaElement”:

<complexType name="MyComplexTypeA">
<sequence>
<element name="myElement1InCTA" type="string"/>
<element name="myElement2InCTA" type="string"/>
</sequence>
</complexType>
<element name="myTopLevelSchemaElement" type="MyComplexTypeA"/>

What you can do is define the complexType inside the element but then for each element you want to use it in you will have to define it inside. Messy!

<element name="myTopLevelSchemaElement" type="MyComplexTypeA">
  <complexType name="MyComplexTypeA">
  <sequence>
  <element name="myElement1InCTA" type="string"/>
  <element name="myElement2InCTA" type="string"/>
  </sequence>
  </complexType>
</element>

A better thing to do is to be clear about your namespace definitions and use them explicitly, for example you can add a name space that uses your target namespace and it would work just fine.

<schema xmlns="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.example.org" xmlns:myns="http://www.example.org" elementFormDefault="qualified">

so you can define the elements like this

<complexType name="MyComplexTypeA">
<sequence>
<element name="myElement1InCTA" type="string"/>
<element name="myElement2InCTA" type="string"/>
</sequence>
</complexType>
<element name="myTopLevelSchemaElement" type="myns:MyComplexTypeA"/>

The other way of doing is to leave the default namespace as is and use a definition for the XMLSchema. For example:

<schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org"   elementFormDefault="qualified">

for this you will need to define the elements by using “xs:” qualifier before your XMLSchema types (your document complexType can be referenced without a qualifier)

<complexType name="MyComplexTypeA">
<sequence>
<element name="myElement1InCTA" type="xs:string"/>
<element name="myElement2InCTA" type="xs:string"/>
</sequence>
</complexType>
<element name="myTopLevelSchemaElement" type="MyComplexTypeA"/>

BPEL Process Schema with User Defined Complex Types:

The Schema file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://xmlns.oracle.com/DBAdapterSOA_jws/DBSOA/WriteMessageToDB"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:asg="http://xmlns.oracle.com/DBAdapterSOA_jws/DBSOA/WriteMessageToDB">
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="street1" type="xs:string"/>
<xs:element name="street2" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Contact">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="primaryPhone" type="xs:string"/>
<xs:element name="secondaryPhone" type="xs:string"/>
<xs:element name="contactAddress" type="asg:Address"/>
</xs:sequence>
</xs:complexType>
<xs:element name="process">
<xs:complexType>
<xs:sequence>
<xs:element name="input" type="asg:Contact"/>
</xs:sequence>
</xs:complexType>
333333333
</xs:element>
<xs:element name="processResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="result" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
The schema structure:

BPEL Process Schema with user-defined complex types

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s