XML Schemas and Namespaces
- The Utility of Namespaces
- The Schema Namespace
- Target Namespaces
- The XML Schema Instance Namespace
- Namespace Issues with Multipart Schemas
The Utility of Namespaces
Although XML namespaces have been around since 1999, many XML authors have not seen them widely used with XML documents. With the introduction of XML Schemas, however, that has changed, as XML Schemas make extensive use of namespacesboth as a mechanism for the structure of the schema itself, and linking a schema to a particular XML instance document.
XML namespaces provide a mechanism for creating a unique means of identifying components that might have similar names, but different structures. For example, let's say we're a manufacturer, working with suppliers and vendors to coordinate the distribution of our products. Now, suppose that both our suppliers and our vendors use contact information in their XML documents. Our primary supplier uses the following:
<name>John Doe</name>
While our primary vendor uses the following:
<name> <first>John</first> <last>Doe</last> </name>
Both are perfectly valid, when used in the context of each respective document/schema. However, if we're merging the two into one common document, we could end up with a problem:
<contact> <name>John Doe</name> <name> <first>John</first> <last>Doe</last> </name> </contact>
Now we have two name elements, with different content models. This could be problematic if we tried to use this document in applications in the current form. However, we could easily correct the problem by employing namespaces. First, we could define the namespaces for use within the context of our contact element:
<contact xmlns:supplier="http://www.supplier.com/contact" xmlns:vendor=" http://www.vendor.com/contact">
By using the xmlns attribute, we have defined two new namespaces we can use within the context of the contact element. The text following xmlns, such as :supplier or :vendor, is the namespace prefix we can use with elements to indicate that they're members of a particular namespace. So now our contact element, complete with content, would look like this:
<contact xmlns:supplier="http://www.supplier.com/contact" xmlns:vendor=" http://www.vendor.com/contact"> <supplier:name>John Doe</supplier:name> <vendor:name> <first>John</first> <last>Doe</last> </vendor:name> </contact>
The result might be a little harder to read, but there is now no ambiguity about the validity of each name element, since each one is clearly identified as being the member of a specific namespace. Namespace applications are not complex, although it can sometimes be a chore to sort through all of the prefixes and colons. Now let's take a look at how XML Schemas make use of namespaces.