Reputation: 3564
First of all , I know there's a similar thread that tries to answer the same question.But Unfortunately for me , that does not clear the confusion.
So here's the question again :
Why do we need to specify targetNamespace in xml schema documents?
We could define multiple namespaces in an instanceDocument & associate these namespaces to corresponding schema documents.Where does the need of targetNamespace come up?
Upvotes: 2
Views: 550
Reputation: 12154
targetNamespace is the namespace that is going to be assigned to the schema you are creating. It is the namespace an instance is going to use to access the types it declares. In the following code, the schema will be assigned to the namespace http://www.somewebsite.com/Something
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.somewebsite.com/Something/Foo">
...
</schema>
In an XML document instance, you declare the namespaces you are going to be using by means of the xmlns attribute. For example:
<purchaseOrder xmlns="http://www.somewebsite.com/Something/Foo"
xmlns:addr="http://www.somewebsite.com/Something/Foo/addr">
<accountName>Shanita</accountName>
<accountNumber>123456</accountNumber>
<addr:street>20 King St</addr:street>
</purchaseOrder>
The default namespace here is http://www.somewebsite.com/Something/Foo
, which makes reference to the schema previously created. This namespace applies to the element that declares it, and its child elements, unles they are prefixed. In the example, all the elements belong to the default namespace, except addr:street
. Since it is prefixed, it belongs to the addr namespace (xmlns:addr="http://www.somewebsite.com/Something/Foo/addr
")
Upvotes: 4