xml – Distinguishing the target namespace from the default namespace

Question:

Can you please tell targetNamespace what is the difference between the target namespace ( targetNamespace ) and the default namespace? Thanks in advance!

Answer:

After a lot of thinking about how to formulate this, I found perhaps the best description . I will translate it into Russian.


Let's look at three XSD schemas. They all define one global type and one global element that refers to that type.

The first uses the 'xsd' prefix for the schema namespace and the default namespace for 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 schema, but defining and prefixing 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 the default namespace instead of 'xsd' for the namespace of the XML schema itself:

<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 schema authors choose the first or the last, because if the default namespace is available, then we can use it for something else.


In short: the default namespace (xmlns without prefix) is used in the XML schema itself .
The target namespace (targetNamespace) specifies in which namespace the elements should be in the XML document that is validated by this schema.


All this is easier to figure out if you use a good xml editor. For example, the editor from Visual Studio produces the following (we use the third variant of the scheme):

  • targetNamespace underscores element with an error:

    Namespace ' http://example.com ' is not available to be referenced in this schema

    That is, it sees that aGlobalType declared in this namespace. But does not know in which namespace this type should be in the target document (which will be validated by this schema).

  • If in addition to this to remove the prefix tns , the error is:

    Type ' http://www.w3.org/2001/XMLSchema:aGlobalType ' is not declared.

    Well, that one is understandable: without the prefix, our type falls into the same namespace as the elements of the schema. And there is no 'aGlobalType' among them.

Let's return the scheme to working view (any of the three versions). Open the document that is validated by this schema. We print the symbol < – in this case the editor will show a list of allowed elements. Select our aGlobalElement , press TAB – the following definition will be inserted:

<aGlobalElement xmlns="http://example.com/"

If you instead type <foo (whatever prefix you want) and press TAB , you will insert:

<foo:aGlobalElement xmlns:foo="http://example.com/"

It becomes clear that this element must be exactly in this namespace (targetNamespace) for the document to be valid in accordance with our schema.

Scroll to Top