# XML namespaces

XML namespaces are defined by a W3C recommendation, dating 14 January 1999, called Namespaces in XML. XML tag names should be globally unique, as well as short for performance reasons. In order to resolve this conflict, the W3C namespace recommendation defines an attribute xmlns which can amend any XML element. If it is present in an element, it identifies the namespace for this element.

The xmlns attribute has the following syntax:

`xmlns: prefix:namespace`

where `namespace` is a unique URI (such as www.ibm.com) and where `prefix` represents the namespace and provides a pointer to it.

In the following customer element definition, an accounting namespace is defined in order to be able to distinguish the element tags from those appearing in customer records created by other business applications:

    <acct:customer xmlns:acct="http://www.my.com/acct-REV10">
    	<acct:name>Corporation</acct:name>
    	<acct:order acct:ref="5566"/>
    	<acct:status>invoice</acct:status>
    </acct:customer>

The namespace definition in the first line assigns the namespace http://www.my.com/acct-REV10 to the prefix. This prefix is used on the element names such as name in order to attach them to the namespace. A second application, for example, a fulfillment system, can assign a different namespace to its customer elements:

    <ful:customer xmlns:ful="http://www.your.com/ful">
    	<ful:name>Corporation</ful:name>
    	<ful:order ful:ref="A98756"/>
    	<ful:status>shipped</ful:status>
     </ful:customer>

An application processing both data structures is now able to treat the accounting and the fulfillment data differently. There is a default namespace. It is set if no local name is assigned in the namespace definition:

    <acct:customer xmlns="http://www.my.com/acct-REV10" xmlns:acct="http://www.my.com/acct-REV10 ">
    <name>Corporation</name>
    <order acct:ref="5566"/>
    <status>invoice</status>
    </customer>

In this example, all tags in the customer record are qualified to reside in the namespace http://www.my.com/acct-REV10. No explicit prefix is needed because the default namespace is used. Note that the default namespace applies to any attributes definitions.

#### XML schemas and namespaces

In the XML schema below, the default namespace for the schema is defined as the standard XML schema namespace http://www.w3.org/2001/XMLSchema; there is also a schema specific namespace http://www.ibm.com.

    <?xml version="1.0"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:TestSchema="http://www.ibm.com">
     <simpleType name="ZipCodeType">
     <restriction base="integer">
      <minInclusive value="10000"/>
     <maxInclusive value="99999"/>
    </restriction>
     </simpleType>
     <!--element definitions skipped -->
    </schema>

Assuming that the XML schema defined above is saved as C:\temp\TestSchema.xsd, a sample XML file that validates against this schema is:

    <?xml version="1.0"?>
    <x:addressList xmlns:x="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.ibm.com file:///C:/temp/TestSchema.xsd">
     xsi:schemaLocation="http://www.ibm.com file:///C:/temp/TestSchema.xsd">
    <x:address>
     <x:street>x:Vangerowstrasse</x:street>
      <x:zipCode>69115</x:zipCode>
     <x:city>x:Heidelberg</x:city>
     </x:address>
        <x:address>
    <x:street>x:Bernal Road</x:street>
    <x:zipCode>90375</x:zipCode>
         <x:city>x:San Jose</x:city>
     </x:address>
    </x:addressList>

#### Target namespace

The target namespace serves to identify the namespace within which the association between the element and its name exists. In the case of declarations, this association determines the namespace of the elements in XML files conforming to the schema. An XML file importing a schema must reference its target namespace in the schemaLocation attribute. Any mismatches between the target and the actual namespace of an element are reported as schema validation errors. In our example, the target namespace is http://www.ibm.com; it is defined in the XML schema file and referenced twice in the XML file. Any mismatch between these three occurrences of the namespace lead to validation errors.

The following are some examples of how target namespace and namespace prefixes work in XML schemas and their corresponding XML instance documents.

#### Sample 1 - A schema with both a default and target namespace and unqualified locals

The XML schema:

    <?xml version="1.0"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com">
    <complexType name="AddressType">
    <sequence>
    <element name="name" type="string"></element>
    </sequence>
    </complexType>
    <element name="MyAddress" type="x:AddressType"></element>
    </schema>

A valid XML instance document created from this schema looks like this. Local elements and attributes are unqualified.

    <?xml version="1.0"?>
    <x:MyAddress xmlns:x="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com x.xsd ">
    <name>Peter Smith</name>
    </x:MyAddress>

When local elements (such as the "name" element) and attributes are unqualified in an XML file, then only the root element is qualified. So, in this example, the "x" namespace prefix is assigned to the root element "MyAddress", associating it with the namespace "http://www.ibm.com", but the"x" prefix is not assigned to the local element "name".

#### Sample 2 - A schema with both a default and target namespace and qualified locals

    <?xml version="1.0"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com" elementFormDefault="qualified">
    <complexType name="AddressType">
    <sequence>
    <element name="name" type="string"></element>
    </sequence>
    </complexType>
    <element name="MyAddress" type="x:AddressType"></element>
     </schema>

A valid XML instance document created from this schema looks like this. Local elements and attributes are qualified This is because the elementFormDefault attribute is set to qualified in the XML schema.

    <?xml version="1.0"?>
      <x:MyAddress xmlns:x="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.ibm.com x.xsd ">
    <x:name>Peter Smith</x:name>
     </x:MyAddress>

In this example, the "x" namespace prefix is assigned to both the root element "MyAddress" and the local element "name", associating them with the namespace "http://www.ibm.com",.

#### Sample 3 - Schema with target Namespace, and explicitly defines xmlns:xsd

This XML schema adds this attribute:

`xmlns:xsd="http://www.w3.org/2001/XMLSchema`

What this means is that each of the constructs that are defined by the XML schema language will need to be qualified with the "xsd" prefix. For example, xsd:complexType and xsd:string

. Note that you can chose any other prefixes such as "xs" or "foobar" in your declaration and usage.

You can specify this prefix in the XML Schema preferences page. For more information, refer to the related tasks below.

All user defined types belong to the namespace http://www.ibm.com as defined by the targetNamespace attribute, and the prefix is "x" as defined by the xmlns:x attribute.

    <?xml version="1.0"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com">
    <xsd:complexType name="AddressType">
    <xsd:sequence>
         <xsd:element name="name" type="xsd:string"></xsd:element>
    </xsd:sequence>
     </xsd:complexType>
     <xsd:element name="MyAddress" type="x:AddressType"></xsd:element>
    </xsd:schema>

A valid XML instance document created from this schema looks like this. Local elements and attributes are unqualified. The semantics of qualification is the same as Sample 1.

    <?xml version="1.0"?>
     <x:MyAddress xmlns:x="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.ibm.com x.xsd ">
    <name>Peter Smith</name>
     </x:MyAddress>

#### Sample 4 - Schema with undeclared target Namespace that explicitly defines xmlns:xsd

This XML schema has no target namespace for itself. In this case, it is highly recommended that all XML schema constructs be explicitly qualified with a prefix such as "xsd". The definitions and declarations from this schema such as AddressType are referenced without namespace qualification since there is no namespace prefix.

    <?xml version="1.0"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="AddressType">
    <xsd:sequence>
    <xsd:element name="name" type="xsd:string"></xsd:element>
    <xsd:element name="name" type="xsd:string"></xsd:element>
    <xsd:element name="name" type="xsd:string"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="MyAddress" type="AddressType"></xsd:element>
    </xsd:schema>

A valid XML instance document created from the schema looks like this. All elements are unqualified.

    <?xml version="1.0"?>
    <MyAddress xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="x.xsd">
    <name>name</name>
    </MyAddress>

#### Sample 5 - A schema where the target namespace is the default namespace

This is an XML schema where the target namespace is the default namespace. As well, the namespace has no namespace prefix.

    <?xml version="1.0"?>
     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns="http://www.ibm.com">
    <xsd:complexType name="AddressType">
    <xsd:sequence>
    <xsd:element name="name" type="xsd:string"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>
     <xsd:element name="MyAddress" type="AddressType"></xsd:element>
     </xsd:schema>

A valid XML instance document created from the schema looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <MyAddress xmlns="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com NewXMLSchema.xsd">
    <name>name</name>
     </MyAddress>

(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.