Namespace Qualification in Instance Documents

The thing that gives most people real heartburn about namespaces is their use in instance documents. This issue has sent some of my standards committee colleagues into apoplectic fits when the topic of namespaces comes up in discussion. Since this chapter is intended to inform you about what you are likely to encounter and is not intended to be a “best practices” guide, I'm going to avoid taking sides in this issue. I will, however, try to point out what some of the controversy and consternation is about.

The most common and simplest way to use namespaces in an instance document is to not explicitly use them at all. The instance document, in essence, has an unnamed default namespace, and all the Elements and Attributes in the document come from that namespace. Very few people have much trouble with this approach.

Another way to use namespaces is to give them a prefix. We've already seen this in the schema declaration Attributes of our instance document.

<SimpleCSV xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="SimpleCSV1.xsd">

The xmlns attribute assigns the xsi prefix to the namespace http://www.w3.org/2001/XMLSchema-instance. The next Attribute uses that prefix to say that the noNamespaceSchemaLocation Attribute is defined in that namespace. Also note that this is the only place in the instance document that you see a namespace or a prefix. This minimalist strategy for instance documents is the next simplest strategy (after the choice of not using named namespaces at all).

Namespace qualification in instance documents is specified by two Attributes in the schema's root xs:schema Element. elementFormDefault and attribute Form Default, respectively, say whether or not your instance document's Elements and Attributes need to have namespaces specified. There is a general preference for setting these to “unqualified,” though I have seen schemas that set them to “qualified.”

At the other end of the spectrum we can have instance documents in which every Element and Attribute is qualified, sometimes from different namespaces. In this form, elementFormDefault has a value of “qualified.” Here's an example.

Qualified Namespace Usage in SimpleCSVNamespaces.xml
<?xml version="1.0" encoding="UTF-8"?>
<bb:SimpleCSV
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:bb="http://www.babelblaster.org/SimpleCSV"
    xmlns:bbc="http://www.babelblaster.org/common"
    xmlns="http://www.usps.org"
    xsi:schemaLocation="
      http://www.babelblaster.org/SimpleCSV SimpleCSVN.xsd
      http://www.babelblaster.org/common common.xsd
      http://www.usps.gov http://www.usps.gov/ZipCodes.xsd
      ">
  <bb:Row>
    <bbc:Column01>Jones</bbc:Column01>
    <bbc:Column02>Mary</bbc:Column02>
    <bbc:Column03>312 Renner Road</bbc:Column03>
    <bbc:Column04>Apartment C</bbc:Column04>
    <bbc:Column05>Richardson</bbc:Column05>
    <bbc:Column06>TX</bbc:Column06>
    <ZipCode xmlns="http://www.usps.org">75080</ZipCode>
    <bbc:Column08>USA</bbc:Column08>
    <bbc:Column09>972-996-1051</bbc:Column09>
  </bb:Row>
  <bb:Row>
    <bbc:Column01>Smith</bbc:Column01>
    <bbc:Column02>Sue</bbc:Column02>
    <bbc:Column03>Highway 118</bbc:Column03>
    <bbc:Column05>Terlingua</bbc:Column05>
    <bbc:Column06>TX</bbc:Column06>
    <ZipCode xmlns="http://www.usps.gov">79852</ZipCode>
    <bbc:Column10>[email protected]</bbc:Column10>
  </bb:Row>
</bb:SimpleCSV>

Note: This document won't validate since there is no U.S. Post Office namespace that defines a ZipCode Element (at least not yet, anyway).

There are tradeoffs to the two extremes. If only the default, unnamed namespace is used, the instance document is limited to a single set of names. This can make it harder in some senses to support standard vocabularies. At the other extreme, liberal use of different namespaces makes everything from DOM names (and even some method calls) in Java and C++ to XPath expressions in XSLT more cumbersome.

There are ways to use multiple namespaces in different schema documents while using just one namespace in instance documents that are valid with the schemas. We'll talk about that later in the chapter when discussing structuring schemas using several different schema files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset