I was reading this article http://devresource.hp.com/drc/slide_presentations/schemaWSDL/index.jsp and it got me thinking about how I have been designing schemas in BizTalk… Yes I know I can already hear your groans about the “BizTalk XSD schema editor”.
Well contrary to popular believe you don’t need to use monolithic structures in BizTalk schema design. From the BizTalk Server 2004 book… p78 – Building Message Specifications – Imports – “Only schemas that are part of the current project may be imported”… I thought I’d take on building the xsd structure described in the above article using Visual Studio armed with only the BizTalk Editor and the XML Schema Editor. TIP For those that don’t know you can still access the XML Schema Editor after BizTalk is installed by right-clicking on the XSD in Visual Studio and selecting Open With… see below.

First I built the Common Types schema. I did this using the XML Schema Editor initially and then opening it in the BizTalk Editor afterwards… the resulting schema is shown below.
CommonTypes.xsd
<?xml version="1.0" encoding="utf16"?>
<schema xmlns:qc="http://XMLImport.CommonTypes/supplier/quoteCommon" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://XMLImport.CommonTypes/supplier/quoteCommon" xmlns="http://www.w3.org/2001/XMLSchema">
<complexType name="OwnerDetails">
<attribute name="CustId" type="string" use="required"/>
<attribute name="CustName" type="string" use="required"/>
<attribute name="CustEmail" type="string" use="required"/>
</complexType>
<complexType name="PartItemType">
<attribute name="SKU" type="string" use="required"/>
<attribute name="Descr" type="string" use="required"/>
<attribute name="Qty" type="int" use="required"/>
<attribute name="QuotedPrice" type="float" use="optional"/>
</complexType>
<complexType name="PartListType">
<sequence>
<element maxOccurs="unbounded" name="PartListType" type="qc:PartItemType"/>
</sequence>
</complexType>
</schema>

I then built the Quote Request schema. For this schema I followed the same method as the above schema but I added the import using Import function in the BizTalk Editor (Open the xsd in the BizTalk editor, click on and click on the ellipse in the Import field of the advanced properties, select the commontypes.xsd; I also changed the namespace to “common”).
QuoteRequest.xsd
<?xml version="1.0" encoding="utf16" ?>
<schema xmlns:supReq="http://XMLImport.CommonTypes/supplier/types" xmlns:common="http://XMLImport.CommonTypes/supplier/quoteCommon" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://XMLImport.CommonTypes/supplier/types" xmlns="http://www.w3.org/2001/XMLSchema">
<import schemaLocation=".\commontypes.xsd" namespace="http://XMLImport.CommonTypes/supplier/quoteCommon" />
<annotation>
<appinfo>
<b:references>
<b:reference targetNamespace="http://XMLImport.CommonTypes/supplier/quoteCommon" />
</b:references>
</appinfo>
</annotation>
<complexType name="QuoteRequestType">
<sequence>
<element name="CustomerDetails" type="common:OwnerDetails" />
<element name="PartList" type="common:PartListType" />
</sequence>
</complexType>
<element name="QuoteRequest" type="supReq:QuoteRequestType" />
</schema>

At this point I thought I’d take the example one step further what if I wanted to create a new schema that referenced “Both” of the previous two schemas… bearing in mind that the second schema already referenced the first set of complex types. Is this possible using the BizTalk Editor? It turns out is but there is a trick to it.
I followed the process of creating a new schema and opening it in the BizTalk Editor.
The trick here is the order that you apply the import statements!
If you attempt to import QuoteRequest.xsd and then import CommonTypes.xsd you get the following error:
This is because CommonTypes.xsd is already imported by QuoteRequest.xsd and the reference to common types has already been partially added but in an unusable way.
You need to import CommonTypes.xsd and then import QuoteRequest.xsd this way the BizTalk Editor is happy and you can proceed to use those imported types in your new schema.
CustomerQuoteRequests.xsd
<?xml version="1.0" encoding="utf16" ?>
<schema xmlns:supReq="http://XMLImport.CommonTypes/supplier/quoteCommon" xmlns:custSupReq="http://XMLImport.CustomerQuoteRequests" xmlns:common="http://XMLImport.CommonTypes/supplier/types" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://XMLImport.CustomerQuoteRequests" xmlns="http://www.w3.org/2001/XMLSchema">
<import schemaLocation=".\commontypes.xsd" namespace="http://XMLImport.CommonTypes/supplier/quoteCommon" />
<import schemaLocation=".\quoterequest.xsd" namespace="http://XMLImport.CommonTypes/supplier/types" /> <annotation>
<appinfo>
<b:references>
<b:reference targetNamespace="http://XMLImport.CommonTypes/supplier/quoteCommon" />
<b:reference targetNamespace="http://XMLImport.CommonTypes/supplier/types" />
</b:references>
</appinfo>
</annotation>
<element name="CustomerQuoteRequests">
<complexType>
<sequence>
<element name="CustomerDetails" type="supReq:OwnerDetails" />
<element minOccurs="0" maxOccurs="unbounded" name="QuoteRequest" type="common:QuoteRequestType" />
</sequence>
</complexType>
</element>
</schema>

In summary to steal a quote from the above article:
1. Use a modular schema design
o Maximizes reuse
o Simplifies code generation
2. Use namespaces effectively
o Simplifies external references
o Can enforce version control
3. Leverage import features
o Provides great reusability
o Use tools appropriately
4. Focus on interoperability
o All types aren't supported
o Add testing methodology
In the end . . .
the proper use of XML Schemas can greatly improve:
- reusability
- flexibility
- maintainability
- portability