I would like to understand the purpose of targetNamespace, which is used both in the XML schema and in the WSDL. In fact, to keep things simple, let me restrict this question to an XML schema.
It seems to me that I fully understand the concept of (simple) XML namespaces. By convention, we use a URI / URL, but we can use any string that we then assign a prefix for reuse by XML nodes and attributes, or simply use the default namespace for the scope. So far, so good?
Now an XML schema is introduced. For some reason, the inventors of XML Schema believed that the concept of simple namespaces was not enough, and they needed to enter targetNamespace. My question is: what is the significant advantage of the targetNamespace representation, which cannot be provided by the usual XML namespace? If the XML document refers to the xsd document, either using schemaLocation or using the import statement, in any case, I give the path to the actual xsd document referenced. This is what uniquely identifies the scheme I want to reference. If, in addition, I want to bind this schema to a specific namespace in my reference document, why should I be obligated to replicate the exact target namespace already defined in the XML schema that I reference? Why can't I just override this namespace, but I want the XML document in which this namespace will be used to reference this particular XML schema document that I want to reference?
Update:
To give an example, if the XML instance document has the following:
<p:Person xmlns:p="http://contoso.com/People" xmlns:v="http://contoso.com/Vehicles" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://contoso.com/schemas/Vehicles http://contoso.com/schemas/vehicles.xsd http://contoso.com/schemas/People http://contoso.com/schemas/people.xsd"> <name>John</name> <age>28</age> <height>59</height> <v:Vehicle> <color>Red</color> <wheels>4</wheels> <seats>2</seats> </v:Vehicle> </p:Person>
Why, for example, does the people.xsd schema need to define a target namespace that is "http://contoso.com/schemas/People"? Why do we even need to define targetNamespace in an xsd document? It seems to me that everything you need to get from part of the schema namespace is already contained in the XML instance document. What is the advantage of having targetNamespace with equal value in an xsd document?
The next question for Paul:
Can you give me a concrete example where such “collisions” between xsd element names become apparent, and this explains the need for targetNamespace?
Ok, here is an attempt to answer my own question. Let me know if this seems consistent to you. Looking at the examples on the page related to Paul helped me.
If we take the XML instance example in the original question above, we have two links to the vehicle element definition. One of them is explicit and visible in the XML instance document itself, but we must also imagine that the person.xsd XML schema refers to the same vehicle definition as the permitted child of the person. If we used regular namespaces, where each document was allowed to define its own namespace for the vehicle, how do we know that the XML instance refers to the same XML schema definition for the vehicle as man.xsd? The only way is to use the concept of a namespace, which is more strict than the original simple one, and which should be written in the same way in several documents.
If I hadn’t written this on a tablet, I would have provided an example code, but here I’ll just try to describe the example that I have in mind.
Imagine that we have two different XML schema definitions for a vehicle element. location1 / vehicles.xsd will contain a definition that confirms an example from the question about this message (containing the color, wheels and elements of a child element), while location2 / vehicles.xsd will contain a completely different definition for a vehicle element (for example, with children , model and volume). Now, if the XML instance document refers to the location1 schema, as in the example above, but person.xsd says that the person element can contain a child of a vehicle of the type defined in Location2, and then without the concept of targetNamespace, the XML instance will check, even if he clearly does not have a suitable type of vehicle as a child of his personal element.
Then the namespace will help us make sure that if two different documents refer to the same third XML schema, they both are in fact referring to the same schema, not just a schema containing elements that are similar but not identical to another ...
Does that make sense?