Namespace in XSD
This blog explains the concept of namespace in XML schema with help of some java concepts. I personally found that relating the concept of namespace to java packages makes it easier to grasp and remember.
1) Namespace in XSD and Package in java
The concept of namespace in XSD is very similar to the use of package in java. Package in java is used to group related classes in a single unit, to avoid naming conflicts or ambiguity.
Similarly in XSD, Namespace is used to group all the elements in that XSD as a single unit to avoid any kind of ambiguity.
According to w3 standards, A namespace should start with a protocol.
Generally http is used
eg- http://Class.school.com
2) TargetNamespace in XSD and package declaration in java
Package declaration in java is simple as below
Package com.school.class;
Public class Student{
}
Here Class Student belongs to the package com.school.class
similar to package declaration in java, targetNamespace in XSD is used to group all entities in that XSD in a single namespace. Placing the targetNamespace attribute at the top of your XSD schema means that all entities defined in XSD are part of this namespace. In short It is a unique identifier. Here all the entities are part of http://Class.School.com namespace.
<schema targetNamespace="http://Class.School.com"
xmlns="http://www.w3.org/2001/XMLSchema">
<element name="Student">
<complexType>
<sequence>
<element name="RollNo" type="int"/>
<element name="Name" type="string"/>
</sequence>
</complexType>
</element>
</schema>
3) Import in java and namespace declaration in XSD
When a class belongs to a package, at the time of using that class we need to use fully qualified namespace.
e.g. Com.school.class.Student
,This however increases the overhead of using fully qualified name for the class whenever it is used. That is when import is used.
import java.lang.*;
class Student{
public static void main(String args[]){
system.out.println("Hello”);
}
}
Similarly in XSD xmlns is used. This can be explained as below
All the entities used in an xsd (element, sequence, complexType etc) belong to a w3 namespace "http://www.w3.org/2001/XMLSchema-instance".
Hence while writing an xsd we need to import the namespace http://www.w3.org/2001/XMLSchema-instance into the XSDas below.
<schema targetNamespace="http://Class.school.com"
xmlns="http://www.w3.org/2001/XMLSchema">
so here http://www.w3.org/2001/XMLSchema is similar to a built in package in java e.g. java.lang. Importing this namespace enables us to use the entities declared in the namespace in our xsd. We can similarly import more than one namespace in an XSD as per our requirement.
1) Use of prefix in XSD
Importing namespace gives birth to another concept of prefix in XSD.
A namespace declared without a prefix is called default namespace, there can be one or no default namespace in an XSD. In the above example http://www.w3.org/2001/XMLSchema is the default namespace. However the same XSDcan be modified as below
Here xs is the prefix for namespace http://www.w3.org/2001/XMLSchema .So all the entities in this namespace should be used with that prefix.
<xs:schema targetNamespace="http://Class.school.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
< xs:element name="Student">
< xs:complexType>
< xs:sequence>
< xs:element name="RollNo" type="int"/>
< xs:element name="name" type="string"/>
</ xs:sequence>
</ xs:complexType>
</ xs:element>
</ xs:schema>
HAPPY LEARNING!!!!