Sunday, November 25, 2012

How to hide/expose namespace from element in XSD

Whenever we are talking about XSD(Schema defination), we are using namespace. Basically what is namespace? and why it is required. Is is possible to hide the namespace from element name.

This post answer all those questions.

namespace - namespaces are using to avoid element name conflicts. If you have little bit knowledge of SQL then you can understand it better way. Suppose you have two tables like 'Employee' and 'Department' and you are trying to join two table to get the employee name, employee id, department name and department id. In this case, you query should be:

select ename, department_name, id, id from Employee e, Department d where e.dept_no=d.id;

In this query there is an issue. In the select we have written two id column, but it is not clear which table's id column it is, so there is a name conflict. In this case you have to prefix the table alias. So, that it is understandable.

So, it is possible to define two element with the same name with different namespace. namespaces are declare at the begining of schema defination.





In the above example, you can see the attribute name "xmlns", this attribute is used to define XML namespace. The namespace are declared using ":" colon. The text after the ":" is called a namespace.

If keyword "xmlns" define alone with a value of http://www.w3.org/2001/XMLSchema, is called default namespace.

Basically default namespace defines the rules and regulations for the schema.

you can refer other schemas using the keyword "xmlns" prefix with the namespace name, like the above example i have refer globaltypes using the "xmlns:gtns". Later on you can simply use the namespace name before the element name.

I hope from the above discussion, you have understand what is "namespace" and why it is required.

Now, the only question remains is "Can we hide namespace name from element name?" answer is Yes.

If you closely look the above code snipet of schema defination, you found there is another attribute i.e "elementFromDetault". Basically this attibute handles to hide/unhide the namespace from the element name.

Example:
If you use the above declaration for the schema, you will get the below:

Unqualified version:
 In the above example you can see the element name is not prefix with namespace.

Qualified version:









 In the above example you can see the element name is prefix with namespace.

Hence, you can say using the attribut "elementFromDefault" you can hide/unhide namespace from the element.