DIfferent Namespaces in XML Schema

Introduction -

Hi Everyone, in this post I will explore the structure of XML Schema language and namespaces in a schema language.
It is suggested the reader to have basic understanding of namespaces in XML and also knowledge about XML Schema.
If the reader is not familiar about XML Namespaces please refer to my earlier post titled "Namespace in XML"

Description -

Defn - XML Schema is a XML based language used for defining the structure of an XML document.

In a XML Schema language the first element should always be the element with name "schema" and this element comes immediately after the XML declaration.
The element contains attributes and has few namespaces that complete the XML Schema document. 



In this post I will take two different Schema files created using Visual Studio, although the user is free to create the schema using any of the tools


STRUCTURE OF A SCHEMA FILE

Sample Schema-1

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xlmns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://abc.com/Information/Schemas/OPFetchInfo/v1.0"
xmlns="http://abc.com/Information/Schemas/OPFetchInfo/v1.0"
xmlns:b="http://schemas.microsoft.com/BizTalk/2003" >
.
<!--Actual XML Elements are defined here -->
.
</xs:schema>

Explanation:

a) <?xml version="1.0" encoding="utf-16"?>
i) The above indicates that XML Schema is based on XML Language

b) <xs:schema xmlns: xs="http://www.w3.org/2001/XMLSchema">
i)The above indicates that the schema element comes from the "http://www.w3.org/2001/XMLSchema" namespace.
ii)Also implies that the elements (like schema,complexType,sequence etc) that come under the "http://www.w3.org/2001/XMLSchema" namespace is prefixed with characters "xs:" In general these characters can be any two or three letters.
iii)Also indicates that element <schema> is the root element of the Schema.

c) targetNamespace="http://abc.com/Information/Schemas/OPFetchInfo/v1.0"
i) The above line indicates that the elements defined in this schema have http://abc.com/Information/Schemas/OPFetchInfo/v1.0 namespace.
It means that XML elements in the XML file against which this schema is used validation belongs to the namespace
http://abc.com/Information/Schemas/OPFetchInfo/v1.0 .

d) xmlns=http://abc.com/Information/Schemas/OPFetchInfo/v1.0"
i) The above indicates that the default namespace for this document is http://abc.com/Information/Schemas/OPFetchInfo/v1.0

e) xmlns:b="http://schemas.microsoft.com/BizTalk/2003"
i)This is required if the XML we are verifying has elements from more then one namespace like multi part schemas.

f) </xs:schema>
i) The above indicates that it is the end of the schema definition.


Sample Schema-2

Lets take another simple way a schema language can be.

<?xml version="1.0" encoding="utf-16"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://abc.com/Information/Schemas/OPFetchInfo/v1.0" >
.
<!--Actual XML Elements are defined here -->
.
</schema>

Here we can see that the default namespace has changed to "http://www.w3.org/2001/XMLSchema", hence the element schema need not be prefixed with any characters like "xs:"

Explanation:

a) <?xml version="1.0"?>
As seen above the above is required because since schema is also based on XML.

b) <schema xmlns="http://www.w3.org/2001/XMLSchema"
i)The default namespace for this document is http://www.w3.org/2001/XMLSchema .
Since the default namespace is set to http://www.w3.org/2001/XMLSchema no prefix is required for any of the elements (like schema,complexType,sequence etc).

c) targetNamespace="http://abc.com/Information/Schemas/OPFetchInfo/v1.0"
i) The above line indicates that the elements defined in this schema have http://abc.com/Information/Schemas/OPFetchInfo/v1.0 namespace.
It means that XML elements in the XML file against which this schema is used validation belongs to the namespace
http://abc.com/Information/Schemas/OPFetchInfo/v1.0 .

d)</xs:schema>
The above indicates that it is the end of the schema definition.


Further Reading -