targetNamespace and xmlns without prefix, what is the difference? targetNamespace and xmlns without prefix, what is the difference? xml xml

targetNamespace and xmlns without prefix, what is the difference?


targetNamespace is an XML Schema "artifact"; its purpose: to indicate what particular XML namespace the schema file describes.

xmlns - because the XML Schema is an XML document, it is then possible to define a default XML namespace for the XML file itself (this is what xmlns attribute does); the implications are multiple: authoring, and composition. For example, one does not have to use a prefix for the items defined in the schema, that are later on referenced elsewhere in the same file (e.g. a global simpleType used as a type for an attribute or element).

From my experience, many XML Schema authors consider this as a "best practice"... so you're on the right track.

In terms of XSD, the targetNamespace prescribes the namespace part of a qualified name of a schema component, which includes elements, attributes, groups and attribute groups, and simple and complex types. Some of the qualified names defined in an XSD (elements and attributes) are "directly" used by an XML instance document. Others, such as for types, can be referenced through the xsi:type attribute in instance XML documents. The rest (groups, attribute groups) are there to facilitate schema composition (through references).

I'm also of opinion that (in general) people come at designing XSD from two angles:

  • to match an existing XML. In this case, if your XML uses namespaces, for each of the namespaces used, you'll end up with an XSD schema element with a matching targetNamespace attribute.

  • pure modeling. You then think of targetNamespace similar to an UML package, or database schema, or a Java package, or a .NET namespace, and all it means in this case. Fundamentally it is a mechanism to avoid naming collisions; nonetheless, it is also a mechanism to partition models in subject areas, etc.


For those who are still confused, consider these three xsds. They all define one global type and one global element definition that references it.

First, an xsd like the one posted above. It uses the prefix 'xsd' for the schema namespace, and a default namespace for the targetNamespace:

<xsd:schema   xmlns:xsd="http://www.w3.org/2001/XMLSchema"   targetNamespace="http://example.com/"   xmlns="http://example.com/">  <xsd:element name="aGlobalElement" type="aGlobalType"/>  <xsd:simpleType name="aGlobalType">    <xsd:restriction base="xsd:string"/>  </xsd:simpleType>   </xsd:schema>  

Now the same xsd, but defining and using a namespace prefix for the target namespace:

<xsd:schema   xmlns:xsd="http://www.w3.org/2001/XMLSchema"   targetNamespace="http://example.com/"   xmlns:tns="http://example.com/">  <xsd:element name="aGlobalElement" type="tns:aGlobalType"/>  <xsd:simpleType name="aGlobalType">    <xsd:restriction base="xsd:string"/>  </xsd:simpleType> </xsd:schema>  

...and finally, a version that uses a default namespace instead of 'xsd' for the XML schema namespace:

<schema   xmlns="http://www.w3.org/2001/XMLSchema"   targetNamespace="http://example.com/"   xmlns:tns="http://example.com/">  <element name="aGlobalElement" type="tns:aGlobalType"/>  <simpleType name="aGlobalType">    <restriction base="string"/>  </simpleType></schema>

Most of the schema authors choose the first or the last, because if the default namespace facility is available then we might as well use it for something.


xmlns

The xmlns attribute sets the default name space of the described element. The default name space is thus applied to all the elements inside the described element, which do not explicitly declare another name space for themselves.

The default name space is set to a standard value for WSDL files: http://www.w3.org/ns/wsdl

targetNameSpace

This attribute contains the name space of your web service. You can choose this name space freely, but there is a convention saying that the URI should point to the WSDL of the service.

xmlns:tns

This name space should be set to the same URI as the targetNameSpace attribute. That way you can refer to the target name space via this name space prefix (tns).

Source : http://tutorials.jenkov.com/wsdl/description.html