Namespaces
One important advantage of schemas over DTDs is the support for namespaces. A target namespace is used in a schema to indicate the namespace to which all components belong. A revised schema that uses a namespace is shown in Example 5, with the changes indicated in bold font. A targetNamespace attribute appears, indicating that the target namespace is http://example.org/emp. All components defined in this schema now belong to this namespace, namely the three elements and the two types (EmployeeType and StatusType). In addition, this namespace is declared as the default namespace. This is necessary in order to allow the references to EmployeeType and StatusType in element declarations. Otherwise, the processor would search for those types as if they had no namespace, and would come up empty-handed.
Example 5. Employee Schema with Target Namespace
<xsd:schema xmlns:xsd=http://www.w3.org/2001/XMLSchema targetNamespace="http://example.org/emp" xmlns="http://example.org/emp" elementFormDefault="qualified"> <xsd:element name="employee" type="EmployeeType"/> <xsd:complexType name="EmployeeType"> <xsd:sequence> <xsd:element name="number" type="xsd:integer"/> <xsd:element name="status" type="StatusType" default="FT"/> </xsd:sequence> <xsd:attribute name="hireDate" type="xsd:date"/> </xsd:complexType> <xsd:simpleType name="StatusType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="FT"/> <xsd:enumeration value="PT"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
Example 6 shows an instance that conforms to this new schema. The default namespace matches exactly the target namespace of the schema.
Example 6. Using a Namespace in the Instance
<employee xmlns="http://example.org/emp" hireDate="2001-04-02"> <number>557</number> <status>FT</status> </employee>