Thursday, June 2, 2011

targetNamespace vs defaultNamespace

Come On! these two are totally different concept; If this is your response about the blog title you are correct. But I wanted some catchy title and ended up choosing this.

Lets first have Q n A session. These questions are well crafted to confuse you between targetNamespace and default namespace. Keys are provided at the end.
In the questions given below you have to identify correct and incorrect schema definition.

Q 1:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:complexType name="PurchaseOrderType"/>
<xs:complexType name="PurchaseDetails">
<xs:sequence>
<xs:element name="PO" type="PurchaseOrderType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>


Q 2:


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="www.shiv.com">
<xs:complexType name="PurchaseOrderType"/>
<xs:complexType name="PurchaseDetails">
<xs:sequence>
<xs:element name="PO" type="PurchaseOrderType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>


Q 3:


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="www.shiv.com" targetNamespace="www.shiv.com">
<xs:complexType name="PurchaseOrderType"/>
<xs:complexType name="PurchaseDetails">
<xs:sequence>
<xs:element name="PO" type="PurchaseOrderType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>


Q 4:


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="www.shiv.com" targetNamespace="www.shiv.com">
<xs:complexType name="PurchaseOrderType"/>
<xs:complexType name="PurchaseDetails">
<xs:sequence>
<xs:element name="PO" type="ns:PurchaseOrderType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

Q 5:


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="www.shiv.com" targetNamespace="www.shivendra.com">
<xs:complexType name="PurchaseOrderType"/>
<xs:complexType name="PurchaseDetails">
<xs:sequence>
<xs:element name="PO" type="PurchaseOrderType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>


Keys:

Q1 - Correct
Q2 - Incorrect: Could not resolve PurchaseOrderType
Q3 - Correct
Q4 - Correct
Q5 - Incorrect


If you got all the answers correct then you can skip the explanation below.

Each W3C XML Schema document is bound to a specific namespace through thetargetNamespace attribute, or to the absence of namespace through the lack of such an attribute. When we define a component (e.g. element, type), what namespace it will reside is decided by targetNamespace. Absence of tagetNamespce makes a defined element not belong to any namespce even if default namespace is present. Its legal to define element and attributes in schema without a namespace.

On the other hand default namespace serve only one purpose, it allows use of namespace qualified xml components without namespace prefix.

Just to give you one hint to solve above problems, when you define a component it’s targetNamespace you should consider but when you refer a component think of default namespace(or namespace with prefix).

Feel free to ask questions if you have any doubts!

1 comment: