Thursday, June 20, 2013

Namespace conflict in include and import in XSD




Namespace is a important concept in Schema.

One need to understand the basic difference in the two as

Include is used when the target schema has the same targetnamespace as source schema

Import is used when target schema has a different targetnamesapce as source schema


The example below will clarify the difference and will give a basic idea on namespaces too

Employee.xsd
================

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.org"
targetNamespace="http://raj.com"
xmlns:raj="http://raj.com"
elementFormDefault="qualified">
<xsd:element name="Employees">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="header_Id" type="xsd:integer"/>
<xsd:element name="Employee" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" maxOccurs="unbounded" type="xsd:string"/>
<xsd:element name="Age" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>




OtherEmployee.xsd
==================

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.org"
targetNamespace="http://raj.com"
xmlns:raj="http://raj.com"
elementFormDefault="qualified">
<xsd:element name="Employees" type="raj:employeeType"/>

<xsd:complexType name="employeeType">
<xsd:sequence>
<xsd:element name="header_Id" type="xsd:integer"/>
<xsd:element name="Employee" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" maxOccurs="unbounded" type="xsd:string"/>
<xsd:element name="Age" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>


Manager.xsd
============

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://anil.com"
xmlns:Mang="http://anil.com"
xmlns:raj="http://raj.com"
elementFormDefault="qualified">
<xsd:import namespace="http://raj.com"
schemaLocation="OtherEmployees.xsd"/>
<xsd:element name="Manager">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="EmployeeType" type="raj:employeeType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>


OtherManager.xsd
================

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://raj.com"
xmlns:Mang="http://raj.com"
elementFormDefault="qualified">
<xsd:include schemaLocation="OtherEmployees.xsd"/>
<xsd:element name="Manager">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="EmployeeType" type="Mang:employeeType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>


Just copy paste the above xsd in your local machine and you should be able to understand about types also in xsd.

A good example is also published in following

http://www.liquid-technologies.com/Reference/XmlDataBinding/source/HowTo/WorkingWithMultipleSchemas.htm

No comments: