Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Developer's Guide

Declaring Namespaces

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office provides support for associating elements and attributes with namespaces. You can use a default or explicit declaration to declare namespaces. In both cases, the declaration associates a Uniform Resource Identifier (URI) with particular element and attribute names.

Choosing Namespace URIs

Developers creating new XML vocabularies might need to choose URIs for use as namespace identifiers. The following are general guidelines for creating new vocabularies that need namespace identifiers.

Default Declaration

The default declaration declares a namespace in effect for an element and all other elements contained within it where the element is not prefixed. The following example declares the <catalog> element—and all its element descendents that do not have a prefix—to be members of the namespace declared using the URI http://www.example.microsoft.com/catalog/.

Example

<catalog xmlns="http://www.example.microsoft.com/catalog/">

  <book id="bk101">
     ...
  </book>
  <book id="bk109">
     ...
  </book>
</catalog>

If the URI reference is an empty string, unprefixed elements within the scope of the declaration are not considered to belong to any namespace. Default declarations are commonly used when a document contains only elements from a particular namespace, or when one namespace effectively dominates all others.

Note   Default namespaces do not directly apply to attributes.

Explicit Declaration

An explicit declaration defines a shorthand, or prefix, to substitute for the full name of a namespace. Use an explicit declaration to reference a node from a namespace separate from your default namespace.

If the catalog example created in the document map had to represent its currency values using an element from a different namespace, it might include both a declaration for the catalog as a whole and an explicit declaration for the element describing the prices.

Explicit declarations of namespace prefixes use attribute names beginning with the xmlns attribute and followed by the prefix. The value of the attribute is the namespace URI.

Example

The following example declares cat and money to be shorthand for the full names of their respective namespaces. All elements beginning with cat: or money: are considered to be from the namespace "http://www.example.microsoft.com/catalog/" or "http://www.example.microsoft.com/currency/", respectively.

<cat:catalog xmlns:cat="http://www.example.microsoft.com/catalog/"
   xmlns:money="http://www.example.microsoft.com/currency/">
  <cat:book id="bk101">
     <cat:author>&#71;ambardella, Matthew</cat:author>
     <cat:title>XML Developer's &#x47;uide</cat:title>
     <cat:genre>Computer</cat:genre>
     <money:price>44.95</money:price>
     <cat:publish_date>2000-10-01</cat:publish_date>
     <cat:description><![CDATA[An in-depth look at creating applications with XML, using <, >,]]> and &amp;.</cat:description>
  </cat:book>
  <cat:book id="bk109">
     <cat:author>Kress, Peter</cat:author>
     <cat:title>Paradox Lost</cat:title>
     <cat:genre>Science Fiction</cat:genre>
     <money:price>6.95</money:price>
     <cat:publish_date>2000-11-02</cat:publish_date>
     <cat:description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</cat:description>
  </cat:book>
</cat:catalog>

Explicit declarations are useful when a node contains elements from different namespaces. You can also fix default namespace declarations with explicit namespace declarations.

Example

The following example is almost identical to the preceding example. However, all of the elements except price use the default namespace declaration, avoiding repeated cat: prefixes. All of the elements except price are associated with the namespace URI "http://www.example.microsoft.com/catalog/"; price is associated with "http://www.example.microsoft.com/currency/".

<catalog xmlns="http://www.example.microsoft.com/catalog/"
   xmlns:money="http://www.example.microsoft.com/currency/">
  <book id="bk101">
     <author>&#71;ambardella, Matthew</author>
     <title>XML Developer's &#x47;uide</title>
     <genre>Computer</genre>
     <money:price>44.95</money:price>
     <publish_date>2000-10-01</publish_date>
     <description><![CDATA[An in-depth look at creating applications with XML, using <, >,]]> and &amp;.</description>
  </book>
  <book id="bk109">
     <author>Kress, Peter</author>
     <title>Paradox Lost</title>
     <genre>Science Fiction</genre>
     <money:price>6.95</money:price>
     <publish_date>2000-11-02</publish_date>
     <description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description>
  </book>
</catalog>

See Also

Document Map