Reputation: 481
I have searched many places for namespaces, but doesnot get satisfactory answer
for the following xsd file
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="product"/>
</xs:schema>
for the above code, following are my understandings. please correct me if i am wrong. Also i have some query regarding the same:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
is the place where defination is stored of all the elements and datatypes i am using in my current document.
targetNamespace="http://www.example.org/schema
is actually the package name where the current file is going to be stored.
xmlns="http://www.w3.org/2001/XMLSchema"
: no idea what is this
**xmlns:tns**
is same as targetNamespace. then why we use it seperately?
what is use of first and third line
Upvotes: 0
Views: 1126
Reputation: 163262
Firstly, the two declarations:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2001/XMLSchema"
indicate that both names prefixed "xs", and unprefixed names, represent names whose meaning is defined in the W3C XSD specification. This gives you a choice of writing <xs:element>
or simply <element>
. There's no particular reason for declaring both; it's better to use one form or the other consistently.
The attribute targetNamespace="http://www.example.org/schema"
indicates that this schema is defining the structure of elements (and perhaps attributes) in the namespace http://www.example.org/schema, which is presumably a namespace for which you are the design control (by which I mean, you shouldn't be using that particular namespace except in an example, because it's not your namespace).
It's quite common to see a declaration like
xmlns:tns="http://www.example.org/schema"
that binds a prefix (in this case "tns") to the target namespace of the schema. If you have one schema component that references another one in the same target namespace (for example an element declaration referencing a type), then there are two ways of doing it:
(1) <xs:element name="e" type="t"/>
This works when the "xs" prefix is bound to "http://www.w3.org/2001/XMLSchema" and the default namespace is the same as the target namespace.
(2) <element name="e" type="tns:t"/>
This works when the default namespace is "http://www.w3.org/2001/XMLSchema" and the "tns" prefix is bound to the target namespace.
Upvotes: 2