XML Namespaces FAQ

Maintained by Ronald Bourret

Copyright 2000, by Ronald Bourret

Table of Contents

PART I: OVERVIEW

      SECTION 1: EXECUTIVE SUMMARY
            1.1) Can you give me an executive summary of what XML namespaces are?
            1.2) Can you give me an executive summary of what XML namespaces are not?

      SECTION 2: TRADITIONAL NAMESPACES
            2.1) What is a traditional namespace?
            2.2) What is the relationship between different traditional namespaces?
            2.3) What are traditional namespaces used for?

      SECTION 3: XML NAMESPACES
            3.1) What is the purpose of XML namespaces?
            3.2) What is an XML namespace?
            3.3) Do XML namespaces have any function except providing a two-part naming system for element types and attributes?
            3.4) What do XML namespaces actually contain?
            3.5) Are the names of all element types and attributes in some XML namespace?
            3.6) Do XML namespaces apply to entity names, notation names, or processing instruction targets?
            3.7) Who can create an XML namespace?
            3.8) Do I need to use XML namespaces?

PART II: DECLARING AND USING XML NAMESPACES

      SECTION 4: DECLARING XML NAMESPACES IN AN XML DOCUMENT
            4.1) How do I declare an XML namespace in an XML document?
            4.2) Where can I declare an XML namespace?
            4.3) Can I use an attribute default in a DTD to declare an XML namespace?
            4.4) Do the default values of xmlns attributes declared in the DTD apply to the DTD?
            4.5) How do I override an XML namespace declaration that uses a prefix?
            4.6) How do I override a default XML namespace declaration?
            4.7) How do I undeclare an XML namespace prefix?
            4.8) How do I undeclare the default XML namespace?
            4.9) Why are special attributes used to declare XML namespaces?

      SECTION 5: USING XML NAMESPACES IN AN XML DOCUMENT
            5.1) How do I use prefixes to refer to element type and attribute names in an XML namespace?
            5.2) How do I use the default XML namespace to refer to element type names in an XML namespace?
            5.3) How do I use the default XML namespace to refer to attribute names in an XML namespace?
            5.4) When should I use the default XML namespace instead of prefixes?

      SECTION 6: SCOPE OF XML NAMESPACE DECLARATIONS
            6.1) What is the scope of an XML namespace declaration?
            6.2) Does the scope of an XML namespace declaration include the element it is declared on?
            6.3) If an element or attribute is in the scope of an XML namespace declaration, is its name in that namespace?
            6.4) What happens when an XML namespace declaration goes out of scope?
            6.5) What happens if no XML namespace declaration is in scope?
            6.6) Can multiple XML namespace declarations be in scope at the same time?
            6.7) How can I declare XML namespaces so that all elements and attributes are in their scope?
            6.8) Does the scope of an XML namespace declaration ever include the DTD?

      SECTION 7: XML NAMESPACES AND DTDs
            7.1) Can I use XML namespaces in DTDs?
            7.2) Do XML namespace declarations apply to DTDs?
            7.3) Can I use qualified names in DTDs?
            7.4) Can the content model in an element type declaration contain element types whose names come from other XML namespaces?
            7.5) Can the attribute list of an element type contain attributes whose names come from other XML namespaces?
            7.6) How can I construct an XML document that is valid and conforms to the XML namespaces recommendation?
            7.7) How can I validate an XML document that uses XML namespaces?
            7.8) If I start using XML namespaces, do I need to change my existing DTDs?

      SECTION 8: USING DOCUMENTS THAT USE XML NAMESPACES
            8.1) How do I create documents that use XML namespaces?
            8.2) How can I check that a document conforms to the XML namespaces recommendation?
            8.3) Can I use the same document with both namespace-aware and namespace-unaware applications?
            8.4) What software is needed to process XML namespaces?
            8.5) How can I use XML namespaces to combine documents that use different element type and attribute names?
            8.6) How do I use XML namespaces with Internet Explorer 5.0 and/or the MSXML parser?

      SECTION 9: PROCESSING XML NAMESPACES IN XML APPLICATIONS
            9.1) How do applications process documents that use XML namespaces?
            9.2) How do I use XML namespaces with SAX 1.0?
            9.3) How do I use XML namespaces with SAX 2.0?
            9.4) How do I use XML namespaces with DOM level 1?
            9.5) How do I use XML namespaces with DOM level 2?
            9.6) Can an application process documents that use XML namespaces and documents that don't use XML namespaces?
            9.7) Can an application be both namespace-aware and namespace-unaware?
            9.8) What does a namespace-aware application do when it encounters an error?

PART III: NAMES, PREFIXES, AND URIs

      SECTION 10: NAMES
            10.1) What is a qualified name?
            10.2) What characters are allowed in a qualified name?
            10.3) Where can qualified names appear?
            10.4) Can qualified names be used in attribute values?
            10.5) How are qualified names mapped to XML namespaces?
            10.6) What is a prefixed name?
            10.7) What is an unprefixed name?
            10.8) Are unprefixed names in an XML namespace?
            10.9) What is a local name?
            10.10) What is a universal name?
            10.11) How are universal names represented?
            10.12) Are universal names universally unique?

      SECTION 11: XML NAMESPACE PREFIXES
            11.1) What is an XML namespace prefix?
            11.2) What characters are allowed in an XML namespace prefix?
            11.3) Are prefixes significant?
            11.4) Can I use the same prefix for more than one XML namespace?
            11.5) Can I use more than one prefix for a the same XML namespace?
            11.6) How are prefixes declared?
            11.7) Can I undeclare a prefix -- that is, dissociate a prefix from an XML namespace?
            11.8) What happens if I use a prefix that is not declared?
            11.9) What happens if there is no prefix on an element type name?
            11.10) What happens if there is no prefix on an attribute name?

      SECTION 12: XML NAMESPACE URIs
            12.1) What is an XML namespace URI?
            12.2) What does an XML namespace URI point to?
            12.3) Can I resolve an XML namespace URI?

PART IV: MISCELLANEOUS

      SECTION 13: POLITICS AND REVOLUTIONS
            13.1) Why are XML namespaces so hard to understand and use?
            13.2) Are there any alternatives to XML namespaces?
            13.3) How controversial are XML namespaces?

      SECTION 14: XML NAMESPACE RESOURCES
            14.1) What resources are available for learning about XML namespaces?

      SECTION 15: COMMENTS, COMPLAINTS, AND SUGGESTIONS

PART I: OVERVIEW

1) SECTION 1: EXECUTIVE SUMMARY

1.1) Can you give me an executive summary of what XML namespaces are?

Sure. Here it is in five easy bullet points.

1.2) Can you give me an executive summary of what XML namespaces are not?

They aren't a cure of cancer, they aren't a way to win the lottery, and they aren't a direct cause of world peace. They also aren't very difficult to understand or use. Two things that XML namespaces are not have caused a lot of confusion, so we'll mention them here:

2) SECTION 2: TRADITIONAL NAMESPACES

2.1) What is a traditional namespace?

Before discussing XML namespaces, it is useful to discuss namespaces in general. In this FAQ, we will refer to such namespaces as traditional namespaces. We will refer to XML namespaces as XML namespaces. The word namespace can refer to either a traditional namespace or an XML namespace, depending on context, but will generally refer to an XML namespace.

A traditional namespace is a set of zero or more names, each of which must be unique within the namespace and constructed according to the rules (if any) of the namespace. For example, the names of element types in an XML document inhabit a traditional namespace, as do the names of tables in a relational database and the names of class variables in a Java class. Traditional namespaces also occur outside the field of computer science -- for example, the names of people could be thought to inhabit a traditional namespace, as could the names of species.

2.2) What is the relationship between different traditional namespaces?

They are disjoint -- that is, they are not related. Because of this, a name in one traditional namespace does not collide with the same name in a different traditional namespace. This property is useful to applications that have multiple sets of names. By assigning each set of names to a different traditional namespace, they can allow the same name to occur in each set of names without fear of collision.

For example, in the following XML document, there is no conflict between the three different uses of the name Value.

  <AuctionItem>
     <Title Value="486Laptop"/>
     <Category Value="Computers"/>
     <Value>$100</Value>
  </AuctionItem>

This is because an XML document has one traditional namespace for element type names and, for each element type, one traditional namespace for the names of the attributes that apply to that element type. Thus, the two Value attribute names don't conflict because each is assigned to a different traditional namespace -- the first to the attribute namespace for the Title element type and the second to the attribute namespace for the Category element type. Furthermore, neither of the Value attribute names conflicts with the Value element type name because element type names are kept in a traditional namespace that is separate from the attribute namespaces.

Other examples of applications that use multiple traditional namespaces include:

2.3) What are traditional namespaces used for?

Perhaps the most common (computer science) use of traditional namespaces is to provide a container for a set of identifiers. For example, traditional namespaces are used to contain the names (identifiers) of element types in an XML document, the names of class variables in a Java class, and the names of tables in a relational database. Traditional namespaces are useful in this regard because of their requirement that each name in the namespace be unique. Thus, when a new name (identifier) is added to the namespace, the uniqueness of the identifier can be verified by checking that the name does not already exist in the namespace.

(Note that just because a set of objects draws its names from some traditional namespace does not mean that those names uniquely identify the objects. For example, two different people can share the same name, as can two different element nodes in a DOM tree, which use element type names as their names. For the names in a traditional namespace to uniquely identify the objects in a set, the objects in the set must draw their names only from that namespace and no name can be applied to more than one object. In practice, the names from a single traditional namespace are only used to identify the objects in a single set; otherwise, additional information must be stored stating which names apply to which set.)

3) SECTION 3: XML NAMESPACES

3.1) What is the purpose of XML namespaces?

XML namespaces are designed to resolve naming conflicts in XML documents that contain element types and attributes from multiple XML languages. For example, consider the following two XML documents:

  <?xml version="1.0" ?>
  <Address>
     <Street>Wilhelminenstr. 7</Street>
     <City>Darmstadt</City>
     <State>Hessen</State>
     <Country>Germany</Country>
     <PostalCode>D-64285</PostalCode>
  </Address>

and:

  <?xml version="1.0" ?>
  <Server>
     <Name>OurWebServer</Name>
     <Address>123.45.67.8</Address>
  </Server>

Each document uses a different XML language and each language defines an Address element type. Each of these Address element types is different -- that is, each has a different content model, a different meaning, and is interpreted by an application in a different way. This is not a problem as long as these element types exist only in separate documents. But what if they are combined in the same document, such as a list of departments, their addresses, and their Web servers? How does an application know which Address element type it is processing?

One solution is to simply rename one of the Address element types -- for example, we could rename the second element type IPAddress. However, this is not a useful long term solution. One of the hopes of XML is that people will standardize XML languages for various subject areas and write modular code to process those languages. By reusing existing languages and code, people can quickly define new languages and write applications that process them. If we rename the second Address element type to IPAddress, we will break any code that expects the old name.

A better answer is to assign each language (including its Address element type) to a different namespace. This allows us to continue using the Address name in each language, but to distinguish between the two different element types. The mechanism by which we do this is XML namespaces.

(Note that by assigning each Address name to an XML namespace, we actually change the name to a two-part name consisting of the name of the XML namespace plus the name Address. This means that any code that recognizes just the name Address will need to be changed to recognize the new two-part name. However, this only needs to be done once, as the two-part name is universally unique. For more information about the uniqueness of universal name, see question 10.12; for more information about processing universal names, see question 9.1.)

3.2) What is an XML namespace?

An XML namespace is a collection of element type and attribute names. The collection itself is unimportant; what is important is its name -- a URI. This allows XML namespaces to provide a two-part naming system for element types and attributes. The first part of the name is the URI of the XML namespace -- the namespace URI. The second part is the element type or attribute name itself -- the local part, also known as the local name. Together, they form the universal name.

This two-part naming system is the only function of XML namespaces.

By using multiple XML namespaces, multiple element types with the same local name can inhabit the same XML document. For example, the following document uses XML namespaces to distinguish between two different element types named Address.

  <Department>
     <Name>DVS1</Name>
     <addr:Address xmlns:addr="http://www.tu-darmstadt.de/ito/addresses">
        <addr:Street>Wilhelminenstr. 7</addr:Street>
        <addr:City>Darmstadt</addr:City>
        <addr:State>Hessen</addr:State>
        <addr:Country>Germany</addr:Country>
        <addr:PostalCode>D-64285</addr:PostalCode>
     </addr:Address>
     <serv:Server xmlns:serv="http://www.tu-darmstadt.de/ito/servers">
        <serv:Name>OurWebServer</serv:Name>
        <serv:Address>123.45.67.8</serv:Address>
     </serv:Server>
  </Department>

The first Address element type name belongs to the http://www.tu-darmstadt.de/ito/addresses XML namespace. It has a universal (two-part) name of "http://www.tu-darmstadt.de/ito/addresses" plus "Address". (Following the convention proposed by James Clark in his paper XML Namespaces (see question 14.1), we write this as {http://www.tu-darmstadt.de/ito/addresses}Address.) The second Address element type name belongs to the http://www.tu-darmstadt.de/ito/servers XML namespace and has a universal name of {http://www.tu-darmstadt.de/ito/servers}Address. Thus, each universal name is unique, meeting the requirement that each element type in an XML document have a unique name.

3.3) Do XML namespaces have any function except providing a two-part naming system for element types and attributes?

No.

This is a very important point and a source of much confusion, so we will repeat it:

   XML NAMESPACES DO NOT HAVE ANY FUNCTION
   EXCEPT PROVIDING A TWO-PART NAMING SYSTEM
   FOR ELEMENT TYPES AND ATTRIBUTES.

In particular, they do not provide or define any of the following:

3.4) What do XML namespaces actually contain?

XML namespaces are collections of names, nothing more. That is, they contain the names of element types and attributes, not the elements or attributes themselves. For example, consider the following document.

  <foo:A xmlns:foo="http://www.foo.org/">
     <B foo:C="foo" D="bar"/>
  </foo:A>

The element type name A and the attribute name C are in the http://www.foo.org/ namespace because they are mapped there by the foo prefix. The element type name B and the attribute name D are not in any XML namespace because no prefix maps them there. On the other hand, the elements A and B and the attributes C and D are not in any XML namespace, even though they are physically within the scope of the http://www.foo.org/ namespace declaration. This is because XML namespaces contain names, not elements or attributes.

XML namespaces also do not contain the definitions of the element types or attributes. This is an important difference, as many people are tempted to think of an XML namespace as a schema, which it is not. (For more information, see myth #8 in "Namespace Myths Exploded".)

(For information about the structure of an XML namespace, see myth #6 in "Namespace Myths Exploded".)

3.5) Are the names of all element types and attributes in some XML namespace?

No.

If an element type or attribute name is not specifically declared to be in an XML namespace -- that is, it is unprefixed and (in the case of element type names) there is no default XML namespace -- then that name is not in any XML namespace. If you want, you can think of it as having a null XML namespace URI, although no "null" XML namespace actually exists. For example, in the following, the element type name B and the attribute names C and E are not in any XML namespace:

  <foo:A xmlns:foo="http://www.foo.org/">
     <B C="bar"/>
     <foo:D E="bar"/>
  </foo:A>

For more information about unprefixed attribute names and XML namespaces, see myth #4 in "Namespace Myths Exploded".

3.6) Do XML namespaces apply to entity names, notation names, or processing instruction targets?

No.

XML namespaces apply only to element type and attribute names. Furthermore, in an XML document that conforms to the XML namespaces recommendation, entity names, notation names, and processing instruction targets must not contain colons.

3.7) Who can create an XML namespace?

Anybody can create an XML namespace -- all you need to do is assign a URI to identify it and decide what element type and attribute names are in it. The URI must be under your control and should not be being used to identify a different XML namespace, such as by a coworker.

(In practice, most people that create XML namespaces also describe the element types and attributes whose names are in it -- their content models and types, their semantics, and so on. However, this is not part of the process of creating an XML namespace, nor does the XML namespace include or provide a way to discover such information.)

3.8) Do I need to use XML namespaces?

Maybe, maybe not.

If you don't have any naming conflicts in the XML documents you are using today, as is often the case with documents used inside a single organization, then you probably don't need to use XML namespaces. However, if you do have conflicts today, or if you expect conflicts in the future due to distributing your documents outside your organization or bringing outside documents into your organization, then you should probably use XML namespaces.

Regardless of whether you use XML namespaces in your own documents, it is likely that you will use them in conjunction with some other XML technology, such as XSL, XHTML, or XML Schemas. For example, the following XSLT (XSL Transformations) stylesheet uses XML namespaces to distinguish between element types defined in XSLT and those defined elsewhere:

  <xsl:stylesheet version="1.0"
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="Address">
     <!-- The Addresses element type is not part of the XSLT namespace. -->
     <Addresses>
        <xsl:apply-templates/>
     </Addresses>
  </xsl:stylesheet>

PART II: DECLARING AND USING XML NAMESPACES

4) SECTION 4: DECLARING XML NAMESPACES IN AN XML DOCUMENT

4.1) How do I declare an XML namespace in an XML document?

To declare an XML namespace, you use an attribute whose name has the form:

  xmlns:prefix
        --OR--
  xmlns

These attributes are often called xmlns attributes and their value is the URI of the XML namespace being declared. The first form of the attribute (xmlns:prefix) declares a prefix to be associated with the XML namespace. The second form (xmlns) declares that the specified namespace is the default XML namespace.

For example, the following declares two XML namespaces, named http://www.tu-darmstadt.de/ito/addresses and http://www.tu-darmstadt.de/ito/servers. The first declaration associates the addr prefix with the http://www.tu-darmstadt.de/ito/addresses namespace and the second declaration states that the http://www.tu-darmstadt.de/ito/servers namespace is the default XML namespace.

  <Department
       xmlns:addr="http://www.tu-darmstadt.de/ito/addresses"
       xmlns="http://www.tu-darmstadt.de/ito/servers">

NOTE: Technically, xmlns attributes are not attributes at all -- they are XML namespace declarations. (They just happen to look like attributes.) This has important consequences for XML application writers. For example, in the XML Information Set (http://www.w3.org/TR/xml-infoset), "xmlns attributes" do not appear as attribute information items. Instead, they appear as namespace declaration information items. Similarly, SAX 2.0 (as currently proposed) optionally omits xmlns attributes.

4.2) Where can I declare an XML namespace?

You can declare an XML namespace on any element in an XML document. The namespace is in scope for that element and all its descendants unless it is overridden (see question 4.5 and question 4.6) or undeclared (see question 4.8). For more information about scope, see section 6.

4.3) Can I use an attribute default in a DTD to declare an XML namespace?

Yes.

For example, the following uses the FIXED attribute xmlns:foo on the A element type to associate the foo prefix with the http://www.foo.org/ namespace. The effect of this is that both A and B are in the http://www.foo.org/ namespace.

  <?xml version="1.0" ?>
  <!DOCTYPE foo:A [
     <!ELEMENT foo:A (foo:B)>
     <!ATTLIST foo:A
               xmlns:foo CDATA #FIXED "http://www.foo.org/">
     <!ELEMENT foo:B (#PCDATA)>
  ]>
  <!-- foo prefix declared through default attribute. -->
  <foo:A>
     <foo:B>abc</foo:B>
  </foo:A>

IMPORTANT: You should be very careful about placing XML namespace declarations in external entities (external DTDs), as non-validating parsers are not required to read these. For example, suppose the preceding DTD was placed in an external entity (foo.dtd) and that the document was processed by a non-validating parser that did not read foo.dtd. This would result in an error because the foo prefix was never declared:

  <?xml version="1.0" ?>
  <!-- foo.dtd might not be read by non-validating parsers. -->
  <!DOCTYPE foo:A SYSTEM "foo.dtd">
  <!-- foo prefix not declared unless foo.dtd is read. -->
  <foo:A>
     <foo:B>abc</foo:B>
  </foo:A>

4.4) Do the default values of xmlns attributes declared in the DTD apply to the DTD?

No.

Declaring a default value of an xmlns attribute in the DTD does not declare an XML namespace for the DTD. (In fact, no XML namespace declarations apply to DTDs.) Instead, these defaults (declarations) take effect only when the attribute is instantiated on an element. For example:

  <?xml version="1.0" ?>
  <!DOCTYPE foo:A [
     <!ELEMENT foo:A (foo:B)>
     <!ATTLIST foo:A
               xmlns:foo CDATA #FIXED "http://www.foo.org/">
     <!ELEMENT foo:B (#PCDATA)>
  ]>
  <foo:A>  <========== Namespace declaration takes effect here.
     <foo:B>abc</foo:B>
  </foo:A>  <========= Namespace declaration ends here.

For more information, see question 7.2. (Note that Internet Explorer 5.0 does use fixed xmlns attribute declarations as XML namespace declarations, but that this will be removed in future versions. For more information, see question 8.6.)

4.5) How do I override an XML namespace declaration that uses a prefix?

To override the prefix used in an XML namespace declaration, you simply declare another XML namespace with the same prefix. For example, in the following, the foo prefix is associated with the http://www.foo.org/ namespace on the A and B elements and the http://www.bar.org/ namespace on the C and D elements. That is, the names A and B are in the http://www.foo.org/ namespace and the names C and D are in the http://www.bar.org/ namespace.

  <foo:A xmlns:foo="http://www.foo.org/">
     <foo:B>
        <foo:C xmlns:foo="http://www.bar.org/">
           <foo:D>abcd</foo:D>
        </foo:C>
     </foo:B>
  </foo:A>

In general, this leads to documents that are confusing to read and should be avoided.

4.6) How do I override a default XML namespace declaration?

To override the current default XML namespace, you simply declare another XML namespace as the default. For example, in the following, the default XML namespace is the http://www.foo.org/ namespace on the A and B elements and the http://www.bar.org/ namespace on the C and D elements. That is, the names A and B are in the http://www.foo.org/ namespace and the names C and D are in the http://www.bar.org/ namespace.

  <A xmlns="http://www.foo.org/">
     <B>
        <C xmlns="http://www.bar.org/">
           <D>abcd</D>
        </C>
     </B>
  </A>

Using multiple default XML namespaces can lead to documents that are confusing to read and should be done carefully. For more information, see question 5.4.

4.7) How do I undeclare an XML namespace prefix?

You cannot "undeclare" an XML namespace prefix. It remains in scope until the end of the element on which it was declared unless it is overridden. Trying to undeclare a prefix by redeclaring it with an empty (zero-length) URI results in an error. For example:

  <foo:A xmlns:foo="http://www.foo.org/">
     <foo:B>
        <foo:C xmlns:foo="">   <==== This is an error.
           <foo:D>abcd</foo:D>
        </foo:C>
     </foo:B>
  </foo:A>

4.8) How do I undeclare the default XML namespace?

To "undeclare" the default XML namespace, you declare a default XML namespace with an empty (zero-length) URI. Within the scope of this declaration, unprefixed element type and attribute names do not belong to any XML namespace. For example, in the following, the default XML namespace is the http://www.foo.org/ for the A and B elements and there is no default XML namespace for the C and D elements. That is, the names A and B are in the http://www.foo.org/ namespace and the names C and D are not in any XML namespace.

  <A xmlns="http://www.foo.org/">
     <B>
        <C xmlns="">
           <D>abcd</D>
        </C>
     </B>
  </A>

4.9) Why are special attributes used to declare XML namespaces?

I don't know the answer to this question, but the likely reason is that they simplify the process of moving fragments of one document to another document. An early draft of the XML namespaces recommendation proposed using processing instructions to declare XML namespaces. While these were simple to read and process, they weren't easy to move to other documents. Attributes, on the other hand, were intimately attached to the elements being moved.

5) SECTION 5: USING XML NAMESPACES IN AN XML DOCUMENT

5.1) How do I use prefixes to refer to element type and attribute names in an XML namespace?

Make sure you have declared the prefix (see question 4.1) and that it is still in scope (see section 6). All you need to do then is prefix the local name of an element type or attribute with the prefix and a colon. The result is a qualified name (see question 10.1), which the application parses to determine what XML namespace the local name belongs to.

For example, suppose you have associated the serv prefix with the http://www.tu-darmstadt.de/ito/servers namespace and that the declaration is still in scope. In the following, serv:Address refers to the Address name in the http://www.tu-darmstadt.de/ito/servers namespace. (Note that the prefix is used on both the start and end tags.)

  <!-- serv refers to the http://www.tu-darmstadt.de/ito/servers namespace. -->
  <serv:Address>123.45.67.8</serv:Address>

Now suppose you have associated the xslt prefix with the http://www.w3.org/1999/XSL/Transform namespace. In the following, xslt:version refers to the version name in the http://www.w3.org/1999/XSL/Transform namespace:

  <!-- xslt refers to the http://www.w3.org/1999/XSL/Transform namespace. -->
  <html xslt:version="1.0">

5.2) How do I use the default XML namespace to refer to element type names in an XML namespace?

Make sure you have declared the default XML namespace (see question 4.1) and that that declaration is still in scope (see section 6). All you need to do then is use the local name of an element type. Even though it is not prefixed, the result is still a qualified name (see question 10.1), which the application parses to determine what XML namespace it belongs to.

For example, suppose you declared the http://www.tu-darmstadt.de/ito/servers namespace as the default XML namespace and that the declaration is still in scope. In the following, Address refers to the Address name in the http://www.tu-darmstadt.de/ito/servers namespace.

  <!-- http://www.tu-darmstadt.de/ito/addresses is the default XML namespace. -->
  <Address>123.45.67.8</Address>

For information about how to use the default XML namespace with attribute names, see question 5.3.

5.3) How do I use the default XML namespace to refer to attribute names in an XML namespace?

You can't.

The default XML namespace only applies to element type names, so you can refer to attribute names that are in an XML namespace only with a prefix. For example, suppose that you declared the http://www.tu-darmstadt.de/ito/addresses namespace as the default XML namespace. In the following, the type attribute name does not refer to that namespace, although the Address element type name does. That is, the Address element type name is in the http://www.tu-darmstadt.de/ito/addresses namespace, but the type attribute name is not in any XML namespace.

  <!-- http://www.tu-darmstadt.de/ito/addresses is the default XML namespace. -->
  <Address type="home">

For information about how to use the default XML namespace with element type names, see question 5.2. For more information about unprefixed attribute names and XML namespaces, see myth #4 in "Namespace Myths Exploded".

5.4) When should I use the default XML namespace instead of prefixes?

This is purely a matter of choice, although your choice may affect the readability of the document. When elements whose names all belong to a single XML namespace are grouped together, using a default XML namespace might make the document more readable. For example:

  <!-- A, B, C, and G are in the http://www.foo.org/ namespace. -->
  <A xmlns="http://www.foo.org/">
     <B>abcd</B>
     <C>efgh</C>
     <!-- D, E, and F are in the http://www.bar.org/ namespace. -->
     <D xmlns="http://www.bar.org/">
        <E>1234</E>
        <F>5678</F>
     </D>
     <!-- Remember! G is in the http://www.foo.org/ namespace. -->
     <G>ijkl</G>
  </A>

When elements whose names are in multiple XML namespaces are interspersed, default XML namespaces definitely make a document more difficult to read and prefixes should be used instead. For example:

  <A xmlns="http://www.foo.org/">
     <B xmlns="http://www.bar.org/">abcd</B>
     <C xmlns="http://www.foo.org/">efgh</C>
     <D xmlns="http://www.bar.org/">
        <E xmlns="http://www.foo.org/">1234</E>
        <F xmlns="http://www.bar.org/">5678</F>
     </D>
     <G xmlns="http://www.foo.org/">ijkl</G>
  </A>

In some cases, default namespaces can be processed faster than namespace prefixes, but the difference is certain to be negligible in comparison to total processing time.

6) SECTION 6: SCOPE OF XML NAMESPACE DECLARATIONS

6.1) What is the scope of an XML namespace declaration?

The scope of an XML namespace declaration is that part of an XML document to which the declaration applies. An XML namespace declaration remains in scope for the element on which it is declared and all of its descendants, unless it is overridden or undeclared on one of those descendants (see questions 4.5, 4.6, and 4.8).

For example, in the following, the scope of the declaration of the http://www.foo.org/ namespace is the element A and its descendants (B and C). The scope of the declaration of the http://www.bar.org/ namespace is only the element C.

  <foo:A xmlns:foo="http://www.foo.org/">
     <foo:B>
        <bar:C xmlns:bar="http://www.bar.org/" />
     </foo:B>
  </foo:A>

6.2) Does the scope of an XML namespace declaration include the element it is declared on?

Yes.

For example, in the following, the names B and C are in the http://www.bar.org/ namespace, not the http://www.foo.org/ namespace. This is because the declaration that associates the foo prefix with the http://www.bar.org/ namespace occurs on the B element, overriding the declaration on the A element that associates it with the http://www.foo.org/ namespace.

  <foo:A xmlns:foo="http://www.foo.org/">
     <foo:B xmlns:foo="http://www.bar.org/">
        <foo:C>abcd</foo:C>
     </foo:B>
  </foo:A>

Similarly, in the following, the names B and C are in the http://www.bar.org/ namespace, not the http://www.foo.org/ namespace because the declaration declaring http://www.bar.org/ as the default XML namespace occurs on the B element, overriding the declaration on the A element.

  <A xmlns="http://www.foo.org/">
     <B xmlns="http://www.bar.org/">
        <C>abcd</C>
     </B>
  </A>

A final example is that, in the following, the attribute name D is in the http://www.bar.org/ namespace.

  <foo:A xmlns:foo="http://www.foo.org/">
     <foo:B foo:D="In http://www.bar.org/ namespace"
             xmlns:foo="http://www.bar.org/">
        <C>abcd</C>
     </foo:B>
  </foo:A>

One consequence of XML namespace declarations applying to the elements they occur on is that they actually apply before they appear. Because of this, software that processes qualified names should be particularly careful to scan the attributes of an element for XML namespace declarations before deciding what XML namespace (if any) an element type or attribute name belongs to.

6.3) If an element or attribute is in the scope of an XML namespace declaration, is its name in that namespace?

Not necessarily.

When an element or attribute is in the scope of an XML namespace declaration, the element or attribute's name is checked to see if it has a prefix that matches the prefix in the declaration. Whether the name is actually in the XML namespace depends on whether the prefix matches. For example, in the following, the element type names A, B, and D and the attribute names C and E are in the scope of the declaration of the http://www.foo.org/ namespace. While the names A, B, and C are in that namespace, the names D and E are not.

  <foo:A xmlns:foo="http://www.foo.org/">
     <foo:B foo:C="foo" />
     <bar:D bar:E="bar" />
  </foo:A>

6.4) What happens when an XML namespace declaration goes out of scope?

When an XML namespace declaration goes out of scope, it simply no longer applies. For example, in the following, the declaration of the http://www.foo.org/ namespace does not apply to the C element because this is outside its scope. That is, it is past the end of the B element, on which the http://www.foo.org/ namespace was declared.

  <!-- B is in the http://www.foo.org/ namespace; C is not in any XML namespace. -->
  <A>
     <B xmlns="http://www.foo.org/">abcd</B>
     <C>efgh</C>
  </A>

In addition to the declaration no longer applying, any declarations that it overrode come back into scope. For example, in the following, the declaration of the http://www.foo.org/ namespace is brought back into scope after the end of the B element. This is because it was overridden on the B element by the declaration of the http://www.bar.org/ namespace.

  <!-- A and C are in the http://www.foo.org/ namespace. B is in the http://www.bar.org/ namespace. -->
  <A xmlns="http://www.foo.org/">
     <B xmlns="http://www.bar.org/">abcd</B>
     <C>efgh</C>
  </A>

6.5) What happens if no XML namespace declaration is in scope?

If no XML namespace declaration is in scope, then any prefixed element type or attribute names result in errors. For example, in the following, the names foo:A and foo:B result in errors.

  <?xml version="1.0" ?>
  <foo:A foo:B="error" />

In the absence of an XML namespace declaration, unprefixed element type and attribute names do not belong to any XML namespace. For example, in the following, the names A and B are not in any XML namespace. For more information, see question 3.5.

  <?xml version="1.0" ?>
  <A B="no error" />

6.6) Can multiple XML namespace declarations be in scope at the same time?

Yes, as long as they don't use the same prefixes and at most one of them is the default XML namespace. For example, in the following, the http://www.foo.org/ and http://www.bar.org/ namespaces are both in scope for all elements:

  <A xmlns:foo="http://www.foo.org/"
     xmlns:bar="http://www.bar.org/">
     <foo:B>abcd</foo:B>
     <bar:C>efgh</bar:C>
  </A>

One consequence of this is that you can place all XML namespace declarations on the root element and they will be in scope for all elements. This is the simplest way to use XML namespaces.

6.7) How can I declare XML namespaces so that all elements and attributes are in their scope?

XML namespace declarations that are made on the root element are in scope for all elements and attributes in the document. This means that an easy way to declare XML namespaces is to declare them only on the root element. For example:

  <Department
         xmlns:addr="http://www.tu-darmstadt.de/ito/addresses"
         xmlns:serv="http://www.tu-darmstadt.de/ito/servers">
     <Name>DVS1</Name>
     <addr:Address>
        <addr:Street>Wilhelminenstr. 7</addr:Street>
        <addr:City>Darmstadt</addr:City>
        <addr:State>Hessen</addr:State>
        <addr:Country>Germany</addr:Country>
        <addr:PostalCode>D-64285</addr:PostalCode>
     </addr:Address>
     <serv:Server>
        <serv:Name>OurWebServer</serv:Name>
        <serv:Address>123.45.67.8</serv:Address>
     </serv:Server>
  </Department>

6.8) Does the scope of an XML namespace declaration ever include the DTD?

No.

XML namespaces can be declared only on elements and their scope consists only of those elements and their descendants. Thus, the scope can never include the DTD. For more information, see question 7.2.

7) SECTION 7: XML NAMESPACES AND DTDs

7.1) Can I use XML namespaces in DTDs?

Yes and no.

In particular, DTDs can contain qualified names (see question 7.3) but XML namespace declarations do not apply to DTDs (see question 7.2).

This has a number of consequences. Because XML namespace declarations do not apply to DTDs:

  1. There is no way to determine what XML namespace a prefix in a DTD points to. Which means...
  2. Qualified names in a DTD cannot be mapped to universal names. Which means...
  3. Element type and attribute declarations in a DTD are expressed in terms of qualified names, not universal names. Which means...
  4. Validation cannot be redefined in terms of universal names as might be expected.

This situation has caused numerous complaints but, as XML namespaces are already a recommendation, is unlikely to change. The long term solution to this problem is an XML schema language: all of the proposed XML schema languages provide a mechanism by which the local name in an element type or attribute declaration can be associated with an XML namespace. This makes it possible to redefine validity in terms of universal names.

7.2) Do XML namespace declarations apply to DTDs?

No.

In particular, an xmlns attribute declared in the DTD with a default is not an XML namespace declaration for the DTD. For more information, see question 4.4. (Note that Internet Explorer 5.0 does use such declarations as XML namespace declarations, but that this will be removed in future versions. For more information, see question 8.6.)

7.3) Can I use qualified names in DTDs?

Yes.

For example, the following is legal:

  <!ELEMENT foo:A (foo:B)>
  <!ATTLIST foo:A
            foo:C CDATA #IMPLIED>
  <!ELEMENT foo:B (#PCDATA)>

However, because XML namespace declarations do not apply to DTDs (see question 7.2), qualified names in the DTD cannot be converted to universal names. As a result, qualified names in the DTD have no special meaning. For example, foo:A is just foo:A -- it is not A in the XML namespace to which the prefix foo is mapped.

The reason qualified names are allowed in the DTD is so that validation will continue to work. For more information, see question 7.6.

7.4) Can the content model in an element type declaration contain element types whose names come from other XML namespaces?

Yes and no.

The answer to this question is yes in the sense that a qualified name in a content model can have a different prefix than the qualified name of the element type being declared. For example, the following is legal:

  <!ELEMENT foo:A (bar:B, baz:C)>

The answer to this question is no in the sense that XML namespace declarations do not apply to DTDs so the prefixes used in an element type declaration are technically meaningless. In particular, they do not specify that the name of a certain element type belongs to a certain namespace. Nevertheless, the ability to mix prefixes in this manner is crucial when: a) you have a document whose names come from multiple XML namespaces (see question 8.5), and b) you want to construct that document in a way that is both valid and conforms to the XML namespaces recommendation (see question 7.6).

7.5) Can the attribute list of an element type contain attributes whose names come from other XML namespaces?

Yes and no, for the reasons listed in question 7.4.

For example, the following is legal:

  <!ATTLIST foo:A 
            bar:B CDATA #IMPLIED>

7.6) How can I construct an XML document that is valid and conforms to the XML namespaces recommendation?

In answering this question, it is important to remember that:

Thus, validity is the same for a document that uses XML namespaces and one that doesn't. In particular, with respect to validity:

Because of this, XML documents that you might expect to be valid are not. For example, the following document is not valid because the element type name A is not declared in the DTD, in spite of the fact both foo:A and A share the universal name {http://www.foo.org/}A:

  <?xml version="1.0" ?>
  <!DOCTYPE foo:A [
     <!ELEMENT foo:A (#PCDATA)>
     <!ATTLIST foo:A
               xmlns:foo CDATA #FIXED "http://www.foo.org/"
               xmlns CDATA #FIXED "http://www.foo.org/">
  ]>
  <A/>

Similarly, the following is not valid because the xmlns attribute is not declared in the DTD:

  <?xml version="1.0" ?>
  <!DOCTYPE A [
     <!ELEMENT A (#PCDATA)>
  ]>
  <A xmlns="http://www.foo.org/" />

Furthermore, documents that you might expect to be invalid are valid. For example, the following document is valid but contains two definitions of the element type with the universal name {http://www.foo.org/}A:

  <?xml version="1.0" ?>
  <!DOCTYPE foo:A [
     <!ELEMENT foo:A (bar:A)>
     <!ATTLIST foo:A
               xmlns:foo CDATA #FIXED "http://www.foo.org/">
     <!ELEMENT bar:A (#PCDATA)>
     <!ATTLIST bar:A
               xmlns:bar CDATA #FIXED "http://www.foo.org/">
  ]>
  <foo:A>
     <bar:A>abcd</bar:A>
  </foo:A>

Finally, validity has nothing to do with correct usage of XML namespaces. For example, the following document is valid but does not conform to the XML namespaces recommendation because the foo prefix is never declared:

  <?xml version="1.0" ?>
  <!DOCTYPE foo:A [
     <!ELEMENT foo:A (#PCDATA)>
  ]>
  <foo:A />

Therefore, when constructing an XML document that uses XML namespaces, you need to do both of the following if you want the document to be valid:

For example:

  <?xml version="1.0" ?>
  <!DOCTYPE foo:A [
     <!ELEMENT foo:A (foo:B)
     <!ATTLIST foo:A
               xmlns:foo CDATA #FIXED "http://www.foo.org/">
     <!ELEMENT foo:B (#PCDATA)>
  ]>
  <foo:A>
     <foo:B>abcd</foo:B>
  </foo:A>

There is no requirement that the same prefix always be used for the same XML namespace. For example, the following is also valid:

  <?xml version="1.0" ?>
  <!DOCTYPE foo:A [
     <!ELEMENT foo:A (bar:B)>
     <!ATTLIST foo:A
               xmlns:foo CDATA #FIXED "http://www.foo.org/">
     <!ELEMENT bar:B (#PCDATA)>
     <!ATTLIST bar:B
               xmlns:bar CDATA #FIXED "http://www.foo.org/">
  ]>
  <foo:A>
     <bar:B />
  </foo:A>

However, documents that use multiple prefixes for the same XML namespace or the same prefix for multiple XML namespaces are confusing to read and thus prone to error. They also allow abuses such as defining an element type or attribute with a given universal name more than once, as was seen earlier. Therefore, a better set of guidelines for writing documents that are both valid and conform to the XML namespaces recommendation is:

The latter three guidelines guarantee that prefixes are unique. This means that prefixes fulfill the role normally played by namespace URIs -- uniquely identifying an XML namespace -- and that qualified names are equivalent to universal names, so a given universal name is always represented by the same qualified name. Although this is contrary to the spirit of prefixes, which were designed for their flexibility, there is no other solution at this point.

7.7) How can I validate an XML document that uses XML namespaces?

When people ask this question, they usually assume that validity is different for documents that use XML namespaces and documents that don't. In fact, it isn't -- it's the same for both. Thus, there is no difference between validating a document that uses XML namespaces and validating one that doesn't. In either case, you simply use a validating parser or other software that performs validation.

For information about how to construct an XML document that is valid and conforms to the XML namespace recommendation, see question 7.6.

7.8) If I start using XML namespaces, do I need to change my existing DTDs?

Probably.

If you want your XML documents to be both valid and conform to the XML namespaces recommendation, you need to declare any xmlns attributes and use the same qualified names in the DTD as in the body of the document. (For more information, see question question 7.6.)

If your DTD contains element type and attribute names from a single XML namespace, the easiest thing to do is to use your XML namespace as the default XML namespace. To do this, declare the attribute xmlns (no prefix) for each possible root element type. If you can guarantee that the DTD is always read (see question 4.3), set the default value in each xmlns attribute declaration to your namespace URI. Otherwise, declare your XML namespace as the default XML namespace on the root element of each instance document.

If your DTD contains element type and attribute names from multiple XML namespaces, you need to choose a single prefix for each XML namespace and use these consistently in qualified names in both the DTD and the body of each document. You also need to declare your xmlns attributes in the DTD and declare your XML namespaces. As in the single XML namespace case, the easiest way to do this is add xmlns attributes to each possible root element type and use default values if possible.

Note that you should only need to make these changes once.

8) SECTION 8: USING DOCUMENTS THAT USE XML NAMESPACES

8.1) How do I create documents that use XML namespaces?

The same as you create documents that don't use XML namespaces. If you're currently using Notepad on Windows or emacs on Linux, you can continue using Notepad or emacs. If you're using an XML editor that is not namespace-aware, you can also continue to use that, as qualified names are legal names in XML documents and xmlns attributes are legal attributes. And if you're using an XML editor that is namespace-aware, it will probably provide features such as automatically declaring XML namespaces and keeping track of prefixes and the default XML namespace for you.

8.2) How can I check that a document conforms to the XML namespaces recommendation?

Unfortunately, I know of no software that only checks for conformance to the XML namespaces recommendation. It is possible that some namespace-aware validating parsers (such as those from DataChannel (Microsoft), IBM, Oracle, or Sun) check XML namespace conformance as part of parsing and validating. Thus, you might be able to run your document through such parsers as a way of testing conformance.

Note that writing an application to check conformance to the XML namespaces recommendation is not as easy as it might seem. The problem is that most parsers do not make DTD information available to the application, so it might not be possible to check conformance in the DTD. (This situation might change in SAX 2.0, which will probably support full DTD events.) Also note that writing a SAX 1.0 application that checks conformance in the body of the document (as opposed to the DTD) should be an easy thing to do. (John Cowan's Namespace SAX Filter probably already does this. For more information, see question 9.2.)

8.3) Can I use the same document with both namespace-aware and namespace-unaware applications?

Yes.

This situation is quite common, such as when a namespace-aware application is built on top of a namespace-unaware parser. Another common situation is when you create an XML document with a namespace-unaware XML editor but process it with a namespace-aware application.

Using the same document with both namespace-aware and namespace-unaware applications is possible because XML namespaces use XML syntax. That is, an XML document that uses XML namespaces is still an XML document and is recognized as such by namespace-unaware software.

The only thing you need to be careful about when using the same document with both namespace-aware and namespace-unaware applications is when the namespace-unaware application requires the document to be valid. In this case, you must be careful to construct your document in a way that is both valid and conforms to the XML namespaces recommendation. (It is possible to construct documents that conform to the XML namespaces recommendation but are not valid and vice versa.) For information about how to do this, see question 7.6.

8.4) What software is needed to process XML namespaces?

From a document author's perspective, this is generally not a relevant question. Most XML documents are written in a specific XML language and processed by an application that understands that language. If the language uses an XML namespace, then the application will already use that namespace -- there is no need for any special XML namespace software.

For information about the software application writers use to process XML namespaces, see section 9.

8.5) How can I use XML namespaces to combine documents that use different element type and attribute names?

Before we answer this question, we need to clear up a common misconception about XML namespaces. It is often believed that XML namespaces provide some sort of magic that allows you to automatically combine XML documents that use separate sets of element types and attributes. This is not true. Although XML namespaces provide some of the tools you need to do this, you still need to do most of the work yourself, as you will see in the answer to this question.

To combine documents that use separate sets of element types and attributes, all you really need to do is decide where the elements and attributes go in the final document. For example, does element type A from the first document go inside element type B? Is it a sibling? Or are they completely unrelated? Although the procedure for actually combining two documents can be easily automated, deciding how to combine them is not likely to ever be automated. Instead, it is a strictly human problem, requiring somebody to make choices.

For example, suppose we have a document containing addresses:

  <Address>
     <Street>Wilhelminenstr. 7</Street>
     <City>Darmstadt</City>
     <State>Hessen</State>
     <Country>Germany</Country>
     <PostalCode>D-64285</PostalCode>
  </Address>

and a document containing Web server information:

  <Server>
     <Name>OurWebServer</Name>
     <Address>123.45.67.8</Address>
  </Server>

Now suppose we would like a document of departments, their addresses, and their Web servers, which we can make from the above documents plus a little additional information:

  <Departments>
     <Department>
        <Name>DVS1</Name>
        <Address>
           <Street>Wilhelminenstr. 7</Street>
           <City>Darmstadt</City>
           <State>Hessen</State>
           <Country>Germany</Country>
           <PostalCode>D-64285</PostalCode>
        </Address>
        <Server>
           <Name>OurWebServer</Name>
           <Address>123.45.67.8</Address>
        </Server>
     </Department>
     ...
  </Departments>

Unfortunately, we have a problem: there are two Address element types and two Name element types. (Although the Name element types both have a content model of PCDATA, they have different meanings -- one is the name of the department and the other is the name of the server -- so we would like to recognize them as different element types.)

To solve these conflicts, we can use XML namespaces. We assign the address information to the http://www.tu-darmstadt.de/ito/addresses namespace, the server information to the http://www.tu-darmstadt.de/ito/servers namespace, and the newly added departmental information to the http://www.tu-darmstadt.de/ito/depts namespace. Thus, our new document looks like:

  <dept:Departments
        xmlns:dept="http://www.tu-darmstadt.de/ito/depts
        xmlns:addr="http://www.tu-darmstadt.de/ito/addresses
        xmlns:serv="http://www.tu-darmstadt.de/ito/servers>
     <dept:Department>
        <dept:Name>DVS1</dept:Name>
        <addr:Address>
           <addr:Street>Wilhelminenstr. 7</addr:Street>
           <addr:City>Darmstadt</addr:City>
           <addr:State>Hessen</addr:State>
           <addr:Country>Germany</addr:Country>
           <addr:PostalCode>D-64285</addr:PostalCode>
        </addr:Address>
        <serv:Server>
           <serv:Name>OurWebServer</serv:Name>
           <serv:Address>123.45.67.8</serv:Address>
        </serv:Server>
     </dept:Department>
     ...
  </dept:Departments>

Although we could have assigned only the conflicting element type names to XML namespaces, it is generally a better idea to keep the names of related element types in a single XML namespace. This avoids questions of having to remember which names have been placed in XML namespaces and which haven't, as well as avoiding future conflicts that might occur when these types are used elsewhere. (This does not necessarily mean that the names of all related element types and attributes should be placed in a single XML namespace. If you have a large and complex set of element types and attributes and expect to reuse subsets of it elsewhere, it might make more sense to place the names of each reusable subset in its own XML namespace.)

This example was fairly simple in that it did not require us to change any existing content models. However, suppose we wanted to keep information only about servers: their name, IP address, and physical location. In this case, we might want to add the Address element type that describes street address to the content of the Server element type:

  <serv:Servers
        xmlns:addr="http://www.tu-darmstadt.de/ito/addresses
        xmlns:serv="http://www.tu-darmstadt.de/ito/servers2>
     <serv:Server>
        <serv:Name>OurWebServer</serv:Name>
        <serv:Address>123.45.67.8</serv:Address>
        <addr:Address>
           <addr:Street>Wilhelminenstr. 7</addr:Street>
           <addr:City>Darmstadt</addr:City>
           <addr:State>Hessen</addr:State>
           <addr:Country>Germany</addr:Country>
           <addr:PostalCode>D-64285</addr:PostalCode>
        </addr:Address>
     </serv:Server>
     ...
  </serv:Servers>

Notice that we have changed the URI of the XML namespace for the server information to http://www.tu-darmstadt.de/ito/servers2. This is because we changed the content model of the Server element type. In other words, the content models of the two Server element types are different, so they are two different element types with the same name and should reside in separate XML namespaces. Had we not made this change, software searching for the old Server element type would have incorrectly processed the new Server element type and vice versa.

As you can see, there is nothing magic about combining documents. It is simply a matter of deciding how to fit them together and, although there are likely to be tools in the future that make this as easy as drag-and-drop, the decisions about exactly how to combine them are likely to always require human intervention.

You can also see that XML namespaces played a vital role in this process -- they allowed us to combine the documents without changing the local names of any of the element types. This is important, as without XML namespaces we would endlessly have to invent new ways of renaming simple element types like Address. However, it is also important to understand that resolving duplicate names is the only role XML namespaces played in this process -- the rest was left to the people making the decisions.

8.6) How do I use XML namespaces with Internet Explorer 5.0 and/or the MSXML parser?

The MSXML parser, which is shipped as part of Internet Explorer 5.0, requires that every XML namespace prefix used in an element type or attribute declaration must be "declared" in the attribute declaration for that element type. This must be done with a fixed xmlns attribute declaration. For example, the following is accepted by MSXML and both xmlns:foo attributes are required:

   <!ELEMENT foo:A (#PCDATA)>
   <!ATTLIST foo:A
             xmlns:foo CDATA #FIXED "http://www.foo.org/">
   <!ELEMENT foo:B (#PCDATA)>
   <!ATTLIST foo:B
             xmlns:foo CDATA #FIXED "http://www.foo.org/">

MSXML returns an error for the following because the second foo prefix is not "declared":

   <!ELEMENT foo:A (#PCDATA)>
   <!ATTLIST foo:A
             xmlns:foo CDATA #FIXED "http://www.foo.org/">
   <!ELEMENT foo:B (#PCDATA)>

The reason for this restriction is presumably so that MSXML can use universal names to match element type and attribute declarations to elements and attributes during validation. Although this would have simplified some of the problems of writing documents that are both valid and conform to the XML namespaces recommendation (see question 7.6), some users have complained about it because it is not part of the XML namespaces recommendation. In response to these complaints, Microsoft plans to remove this restriction in a later version, which will hopefully ship in spring of 2000.

9) SECTION 9: PROCESSING XML NAMESPACES IN XML APPLICATIONS

9.1) How do applications process documents that use XML namespaces?

Applications process documents that use XML namespaces in almost exactly the same way they process documents that don't use XML namespaces. For example, if a namespace-unaware application adds a new sales order to a database when it encounters a SalesOrder element, the equivalent namespace-aware application does the same. The only difference is that the namespace-aware application:

9.2) How do I use XML namespaces with SAX 1.0?

The easiest way to use XML namespaces with SAX 1.0 is to use John Cowan's Namespace SAX Filter (see http://www.ccil.org/~cowan/XML). This is a SAX filter that keeps track of XML namespace declarations, parses qualified names, and returns element type and attribute names as universal names in the form:

  URI^local-name

For example:

  http://www.tu-darmstadt.de/ito/sales^SalesOrder

Your application can then base its processing on these longer names. For example, the code:

   public void startElement(String elementName, AttributeList attrs)
      throws SAXException
   {
      ...
      if (elementName.equals("SalesOrder"))
      {
         // Add new database record.
      }
      ...
   }

might become:

   public void startElement(String elementName, AttributeList attrs)
      throws SAXException
   {
      ...
      if (elementName.equals("http://www.tu-darmstadt.de/sales^SalesOrder"))
      {
         // Add new database record.
      }
      ...
   }

or:

   public void startElement(String elementName, AttributeList attrs)
      throws SAXException
   {
      ...
      // getURI() and getLocalName() are utility functions to parse universal names.

      if (getURI(elementName).equals("http://www.tu-darmstadt.de/ito/sales"))
      {
         if (getLocalName(elementName).equals("SalesOrder"))
         {
            // Add new database record.
         }
      }
      ...
   }

If you do not want to use the Namespace SAX Filter, then you will need to do the following in addition to identifying element types and attributes by their universal names:

9.3) How do I use XML namespaces with SAX 2.0?

NOTE: SAX 2.0 is currently (1 March, 2000) in beta release, so the following information might change.

SAX 2.0 primarily supports XML namespaces through the following methods:

For example, the namespace-unaware SAX 1.0 code:

   public void startElement(String elementName, AttributeList attrs)
      throws SAXException
   {
      ...
      if (elementName.equals("SalesOrder"))
      {
         // Add new database record.
      }
      ...
   }

might become:

   public void startElement(String namespaceURI,
                            String localName,
                            String qualifiedName,
                            Attributes attrs)
      throws SAXException
   {
      ...
      if (namespaceURI.equals("http://www.tu-darmstadt.de/sales") &&
          localName.equals("SalesOrder"))
      {
         // Add new database record.
      }
      ...
   }

Although the above methods are sufficient for most applications, SAX 2.0 also supports the following:

For complete details, see the SAX 2.0 specification (http://www.megginson.com/SAX/SAX2/).

9.4) How do I use XML namespaces with DOM level 1?

This depends on what DOM level 1 implementation you are using. If you are using a namespace-aware DOM implementation, such as those from Data Channel (Microsoft), IBM, Oracle, or Sun, then all you need to do is use the methods provided by those implementations to retrieve the URI and local names. For example, the namespace-unaware code:

  // Check the local name.
  // getNodeName() is a DOM level 1 method.

  if (elementNode.getNodeName().equals("SalesOrder"))
  {
     // Add new database record.
  }

might become the following namespace-aware code when using Oracle's DOM implementation:

  // Check the XML namespace URI.
  // getNamespace() and NSElement are Oracle-specific.

  if ((NSElement)elementNode).getNamespace().equals("http://www.tu-darmstadt.de/ito/sales"))
  {

     // Check the local name.
     // getLocalName() is Oracle-specific.

     if ((NSElement)elementNode.getLocalName().equals("SalesOrder"))
     {
        // Add new database record.
     }
  }

Because DOM level 1 does not include XML namespace support, each DOM level 1 implementation provides a slightly different interface for accessing XML namespace information. Most of these implementations include some combination of methods for getting the qualified name, URI, local name, and prefix. Therefore, if you are writing a DOM-neutral application, you will need to define your own XML namespace interface and write converters for each DOM implementation you support. Fortunately, this is easy to do.

If you are using a namespace-unaware DOM implementation, such as Docuverse version 1 or OpenXML version 1 (these may support XML namespaces in a later version), you will need to perform namespace processing yourself. Although this is largely the same as in a SAX 1.0 application (see question 9.2) -- checking for xmlns attributes on each Element node and converting qualified names on Element and Attr nodes to universal names -- it is potentially more difficult to determine what XML namespace declarations are in scope for any given node. If your application traverses the DOM tree in order from the root, this is fairly easy, as you can maintain prefix-to-URI mappings as you go. However, if you access the tree randomly, you may want to construct a parallel tree with mapping information before you do any other processing or rebuild the DOM tree using prefixes that map to known URIs.

9.5) How do I use XML namespaces with DOM level 2?

NOTE: DOM level 2 is currently (26 April, 2000) not a final recommendation, so the following information might change.

DOM level 2 supports XML namespaces through a number of new methods and attributes. The most important of these are the namespaceURI and localName attributes in the Node interface; these allow applications to identify nodes by their namespace URI and local name rather than by qualified name. For example, the namespace-unaware code:

  // Check the local name.
  // getNodeName() is a DOM level 1 method.

  if (elementNode.getNodeName().equals("SalesOrder"))
  {
     // Add new database record.
  }

might become the following namespace-aware code:

  // Check the XML namespace URI.
  // getNamespaceURI() is a DOM level 2 method.

  if (elementNode.getNamespaceURI().equals("http://www.tu-darmstadt.de/ito/sales"))
  {

     // Check the local name.
     // getLocalName() is a DOM level 2 method.

     if (elementNode.getLocalName().equals("SalesOrder"))
     {
        // Add new database record.
     }
  }

Note that, unlike SAX 2.0, DOM level 2 treats xmlns attributes as normal attributes.

For complete details, see the DOM level 2 recommendation (http://www.w3.org/TR/DOM-Level-2/).

9.6) Can an application process documents that use XML namespaces and documents that don't use XML namespaces?

Yes.

This is a common situation for generic applications, such as editors, browsers, and parsers, that are not wired to understand a particular XML language. Such applications simply treat all element type and attribute names as qualified names. Those names that are not mapped to an XML namespace -- that is, unprefixed element type names in the absence of a default XML namespace and unprefixed attribute names -- are simply processed as one-part names, such as by using a null XML namespace URI.

Note that such applications must decide how to treat documents that do not conform to the XML namespaces recommendation. For example, what should the application do if an element type name contains a colon (thus implying the existence of a prefix), but there are no XML namespace declarations in the document? The application can choose to treat this as an error, or it can treat the document as one that does not use XML namespaces, ignore the "error", and continue processing.

9.7) Can an application be both namespace-aware and namespace-unaware?

Yes.

However, there is generally no reason to do this. The reason is that most applications understand a particular XML language, such as one used to transfer sales orders between companies. If the element type and attribute names in the language belong to an XML namespace, the application must be namespace-aware; if not, the application must be namespace-unaware. For example, such an application should never need to recognize the element type names SalesOrder and {http://www.tu-darmstadt.de/ito/sales}SalesOrder.

For a few applications, being both namespace-aware and namespace-unaware makes sense. For example, a parser might choose to redefine validity in terms of universal names (see myth #9 in "Namespace Myths Exploded") and have both namespace-aware and namespace-unaware validation modes. However, such applications are uncommon.

9.8) What does a namespace-aware application do when it encounters an error?

The XML namespaces recommendation does not specify what a namespace-aware application does when it encounters a document that does not conform to the recommendation. Therefore, the behavior is application-dependent. For example, the application could stop processing, post an error to a log and continue processing, or ignore the error.

PART III: NAMES, PREFIXES, AND URIs

10) SECTION 10: NAMES

10.1) What is a qualified name?

A qualified name is a name of the following form. It consists of an optional prefix and colon, followed by the local part, which is sometimes known as a local name.

  prefix:local-part
        --OR--
  local-part

For example, both of the following are qualified names. The first name has a prefix of serv; the second name does not have a prefix. For both names, the local part (local name) is Address.

  serv:Address
  Address

10.2) What characters are allowed in a qualified name?

The prefix can contain any character that is allowed in the Name [5] production in XML 1.0 except a colon. The same is true of the local name. Thus, there can be at most one colon in a qualified name -- the colon used to separate the prefix from the local name.

10.3) Where can qualified names appear?

Qualified names can appear anywhere an element type or attribute name can appear: in start and end tags, as the document element type, and in element type and attribute declarations in the DTD. For example:

  <!DOCTYPE foo:A [
     <!ELEMENT foo:A (foo:B)>
     <!ATTLIST foo:A
               foo:C CDATA #IMPLIED>
     <!ELEMENT foo:B (#PCDATA)>
  ]>
  <foo:A xmlns:foo="http://www.foo.org/" foo:C="bar">
     <foo:B>abcd</foo:B>
  <foo:A>

Qualified names cannot appear as entity names, notation names, or processing instruction targets.

10.4) Can qualified names be used in attribute values?

Yes, but they have no special significance. That is, they are not necessarily recognized as such and mapped to universal names. For example, the value of the C attribute in the following is the string "foo:D", not the universal name {http://www.foo.org/}D.

  <foo:A xmlns:foo="http://www.foo.org/">
     <foo:B C="foo:D"/>
  <foo:A>

In spite of this, there is nothing to stop an application from recognizing a qualified name in an attribute value and processing it as such. This done in technologies being developed today. For example, in the following XML Schemas definition, the attribute value xsd:string identifies the type of the foo attribute as the universal name {http://www.w3.org/1999/XMLSchema}string.

   <xsd:attribute name="foo" type="xsd:string" />

There are two potential problems with this. First, the application must be able to retrieve the prefix mappings currently in effect. Fortunately, both SAX 2.0 and DOM level 2 support this capability. Second, any general purpose transformation tool, such as one that writes an XML document in canonical form and changes namespace prefixes in the process, will not recognize qualified names in attribute values and therefore not transform them correctly. Although this may be solved in the future by the introduction of the QName (qualified name) data type in XML Schemas, it is a problem today.

10.5) How are qualified names mapped to XML namespaces?

If a qualified name in the body of a document (as opposed to the DTD) includes a prefix, then that prefix is used to map the local part of the qualified name to an XML namespace. (Note that the prefix must be in scope -- see section 6.) For example, in the following, the prefix foo is used to map the local names A, B, and C to the http://www.foo.org/ namespace:

  <?xml version="1.0" ?>
  <foo:A xmlns:foo="http://www.foo.org/" foo:C="bar">
     <foo:B>abcd</foo:B>
  <foo:A>

If a qualified name in the body of a document does not include a prefix and a default XML namespace is in scope then one of two things happens. If the name is used as an element tag, it is mapped to the default XML namespace. If it is used as an attribute name, it is not in any XML namespace (see myth #4 in "Namespace Myths Exploded"). For example, in the following, A and B are in the http://www.foo.org/ namespace and C is not in any XML namespace:

  <?xml version="1.0" ?>
  <A xmlns="http://www.foo.org/" C="bar">
     <B>abcd</B>
  <A>

If a qualified name in the body of a document does not include a prefix and no default XML namespace is in scope, then that name is not in any XML namespace. For example, in the following, A, B, and C are not in any XML namespace:

  <?xml version="1.0" ?>
  <A C="bar">
     <B>abcd</B>
  <A>

Qualified names in the DTD are never mapped to an XML namespace because they are never in the scope of an XML namespace declaration. (For more information, see question 7.2.)

10.6) What is a prefixed name?

A prefixed name is a qualified name (see question 10.1) that contains a prefix.

10.7) What is an unprefixed name?

An unprefixed name is a qualified name (see question 10.1) that does not contain a prefix.

10.8) Are unprefixed names in an XML namespace?

Only if they are used in the body of a document (as opposed to the DTD) as an element tag and a default XML namespace is in scope. For more information, see question 5.2.

10.9) What is a local name?

This is another term for the local part of a qualified name. For more information, see question 10.1.

10.10) What is a universal name?

A universal name is a two part name consisting of an XML namespace URI and a local name. For example, in the following, the universal name is the URI "http://www.tu-darmstadt.de/ito/servers" plus the local name "Address":

  <serv:Address xmlns:serv="http://www.tu-darmstadt.de/ito/servers">123.45.67.8</serv:Address>

The ability to specify universal names is the only function of XML namespaces.

10.11) How are universal names represented?

There is no standard way to represent a universal name. However, three representations are common.

The first representation keeps the XML namespace URI and the local name separate. For example, many DOM level 1 implementations have different methods for returning the XML namespace URI and the local name of an element or attribute node.

The second representation concatenates the URI and the local name with caret (^). The result is a universally unique (see question 10.12) name, since carets are not allowed in URIs or local names. This is the method used by John Cowan's Namespace SAX Filter (see question 9.2). For example, the universal name that has the URI http://www.tu-darmstadt.de/ito/servers and the local name Address would be represented as:

  http://www.tu-darmstadt.de/ito/servers^Address

The third representation, which we use in this FAQ, was suggested by James Clark in his XML Namespaces paper (see question 14.1). It places the URI in braces and concatenates this with the local name. This notation is suggested only for documentation and I am aware of no code that uses it. For example, the above name would be represented as:

  {http://www.tu-darmstadt.de/ito/servers}Address

10.12) Are universal names universally unique?

No, but it is reasonable to assume they are.

Universal element type and attribute names are not guaranteed to be universally unique -- that is, unique within the space of all XML documents -- because it is possible for two different people, each defining their own XML namespace, to use the same URI and the same element type or attribute name. However, this occurs only if:

The first case means somebody is cheating when assigning URIs (a process governed by trust) and the second case means that two people within an organization are not paying attention to each other's work. For widely published element type and attribute names, neither case is very likely. Thus, it is reasonable to assume that universal names are universally unique. (Since both cases are possible, applications that present security risks should be careful about assuming that universal names are universally unique.)

For information about the ability of universal names to uniquely identify element types and attributes (as opposed to the names themselves being unique), see myth #2 in "Namespace Myths Exploded".

11) SECTION 11: XML NAMESPACE PREFIXES

11.1) What is an XML namespace prefix?

An XML namespace prefix is a prefix used to specify that a local element type or attribute name is in a particular XML namespace. For example, in the following, the serv prefix specifies that the Address element type name is in the http://www.tu-darmstadt.de/ito/addresses namespace:

  <serv:Addresses xmlns:serv="http://www.tu-darmstadt.de/ito/addresses">

11.2) What characters are allowed in an XML namespace prefix?

The prefix can contain any character that is allowed in the Name [5] production in XML 1.0 except a colon.

11.3) Are prefixes significant?

No.

For example, the following are equivalent:

  <serv:Addresses xmlns:serv="http://www.tu-darmstadt.de/ito/addresses">

and

  <foo:Addresses xmlns:foo="http://www.tu-darmstadt.de/ito/addresses">

However, prefixes provide a visual cue to readers. Because of this, software (especially editors) should consider preserving prefixes. For example, this is much easier to read:

  <xsl:stylesheet version="1.0"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:html="http://www.w3.org/TR/xhtml1/strict">
  <xsl:template match="/">
     <html:html>
        <html:head><html:title>Title</html:title></html:head>
        <html:body>
           <xsl:apply-templates/>
        </html:body>
  <xsl:template>
  ...
  </xsl:stylesheet>

than this:

  <aaa:stylesheet version="1.0"
                  xmlns:aaa="http://www.w3.org/1999/XSL/Transform"
                  xmlns:bbb="http://www.w3.org/TR/xhtml1/strict">
  <aaa:template match="/">
     <bbb:html>
        <bbb:head><bbb:title>Title</bbb:title></bbb:head>
        <bbb:body>
           <aaa:apply-templates/>
        </bbb:body>
  <aaa:template>
  ...
  </aaa:stylesheet>

or (much worse yet) this:

  <html:stylesheet version="1.0"
                  xmlns:html="http://www.w3.org/1999/XSL/Transform"
                  xmlns:xsl="http://www.w3.org/TR/xhtml1/strict">
  <html:template match="/">
     <xsl:html>
        <xsl:head><xsl:title>Title</xsl:title></xsl:head>
        <xsl:body>
           <html:apply-templates/>
        </xsl:body>
  <html:template>
  ...
  </html:stylesheet>

In spite of this, people are likely to read significance into prefixes, so it is a good idea to use prefixes appropriate to their XML namespace, such as xslt for the XSLT namespace.

11.4) Can I use the same prefix for more than one XML namespace?

Yes.

For example, in the following, B is in the http://www.foo.org/ namespace and C is in the http://www.bar.org/ namespace:

  <A>
     <foo:B xmlns:foo="http://www.foo.org/">abc</foo:B>
     <foo:C xmlns:foo="http://www.bar.org/">abc</foo:C>
  </A>

Remember, however, that the prefix in an XML namespace declaration overrides the same prefix if that prefix is declared on an ancestor. For example, in the following, C is in the http://www.bar.org/ namespace, not the http://www.foo.org/ namespace:

  <A>
     <foo:B xmlns:foo="http://www.foo.org/">
        <foo:C xmlns:foo="http://www.bar.org/">abc</foo:C>
     </foo:B>
  </A>

In general, it is a good idea to use a prefix for only one XML namespace, as this reduces confusion when reading the document. In spite of this, there is a very real possibility that the same prefix will be used for multiple XML namespaces when combining fragments from different documents. For more information, see question 4.9.

11.5) Can I use more than one prefix for a the same XML namespace?

Yes.

For example, in the following, both B and C are in the http://www.foo.org/ namespace:

  <A xmlns:foo="http://www.foo.org/
     xmlns:bar="http://www.foo.org/">
     <foo:B>abc</foo:B>
     <bar:C>abc</bar:C>
  </A>

In general, it is a good idea to use only one prefix for an XML namespace, as this reduces confusion when reading the document.

11.6) How are prefixes declared?

Prefixes are declared in an XML namespace declaration (xmlns attribute). For more information, see question 4.1.

11.7) Can I undeclare a prefix -- that is, dissociate a prefix from an XML namespace?

No.

You can override a prefix (see question 4.5) or the XML namespace declaration that declares the prefix can go out of scope (see question 6.4), but you can't undeclare the prefix. (Note that you can undeclare the default XML namespace; for more information, see question 4.8.)

11.8) What happens if I use a prefix that is not declared?

This is an error. For example, the following document does not conform to the XML namespaces recommendation.

  <?xml version="1.0" ?>
  <foo:A />

However, the XML namespaces recommendation does not specify what a namespace-aware application does when it encounters a non-conformant document, so the behavior is application-dependent. For example, the application could stop processing, post an error to a log and continue processing, or ignore the error.

11.9) What happens if there is no prefix on an element type name?

If a default XML namespace declaration is in scope, then the element type name is in the default XML namespace. Otherwise, the element type name is not in any XML namespace. For example, in the following, B and C are in the http://www.foo.org/ namespace and A is not in any XML namespace:

  <A>
     <B xmlns="http://www.foo.org/">
        <C>abc</C>
     </B>
  </A>

11.10) What happens if there is no prefix on an attribute name?

The attribute name is not in any XML namespace. That is, the default XML namespace does not apply to unprefixed attribute names.

For more information, see myth #4 in "Namespace Myths Exploded".

12) SECTION 12: XML NAMESPACE URIs

12.1) What is an XML namespace URI?

An XML namespace URI is a URI that uniquely identifies the namespace. URIs are used because they are widely understood and well documented. Because people may only allocate URIs under their control, it is easy to ensure that no two XML namespaces are identified by the same URI.

12.2) What does an XML namespace URI point to?

An XML namespace URI is simply an identifier. It is not guaranteed to point to anything and, in general, it is a bad idea to assume that it does. This point causes a lot of confusion, so we'll repeat it here:

   XML NAMESPACE URIs ARE JUST IDENTIFIERS. THEY
   ARE NOT GUARANTEED TO POINT TO ANYTHING.

An early version of the W3C's XML Schemas used XML namespace URIs to point to an XML Schema document containing the definitions of the element types and attributes named in the namespace. However, this proved very controversial and the idea has been withdrawn.

12.3) Can I resolve an XML namespace URI?

Yes.

You can also eat a tractor, but that doesn't mean it's a good idea. XML namespace URIs are not guaranteed to point to anything, so there is generally no reason to resolve them. Furthermore, there is nothing in the processing of XML namespaces that requires you to resolve an XML namespace URI.

PART IV: MISCELLANEOUS

13) SECTION 13: POLITICS AND REVOLUTIONS

13.1) Why are XML namespaces so hard to understand and use?

Actually, they aren't. When trying to understand XML namespaces, all you need to do is remember one thing:

That's it. They don't do anything else and what little complexity there is comes from the flexibility of declaration and scoping rules. You can minimize this complexity by doing the following:

Processing XML namespaces isn't that hard either. In a SAX 1.0 application, you can use John Cowan's Namespace SAX Filter to return universal names; SAX 2.0 does this for you. In a DOM level 1 application, you can usually ask the DOM implementation for namespace information; DOM level 2 always provides this information. If you do your own processing, all you need to remember is to keep the prefix-to-URI mappings as part of your state, scan the attributes for xmlns attributes before doing anything else, and scan element type and attribute names for prefixes.

13.2) Are there any alternatives to XML namespaces?

Yes. You could resolve name clashes by simply renaming the offending element types or attributes and then, during processing, using architectural forms to transform them to names that are recognizeable by different modules in the application. Although you may have to rename some element types or attributes, you won't have to change your application modules. A discussion of architectural forms is beyond the scope of this paper. For more information, see Architectural Forms and SGML/XML Architectures on Robin Cover's XML Cover Pages.

As to the actual syntax used by XML namespaces, the idea of declaring XML namespace prefixes in a processing instruction at the start of an XML document is occasionally raised. Processing instructions were used in an early draft of the XML namespaces specification and have the advantage of simplicity (see question 4.9). They were later replaced with the more flexible xmlns attributes. Although you might be tempted to strike out on your own and use processing instructions or some other strategy, this is probably a bad idea, although not necessarily for technical reasons. The real problem is that XML namespaces are already widely used and supported, so any XML documents you produce will be non-standard and non-portable.

13.3) How controversial are XML namespaces?

The need for XML namespaces and the basic idea that a two-part naming system (or something similar) is needed is not controversial. However, the design of XML namespaces -- that is, the way XML namespaces are declared and used in an XML document, as well the confusion discussed in "Namespace Myths Exploded" -- has, at times, been very controversial. (If you want to see just how controversial, go to the archives of the XML-DEV mailing list and search on the word "namespace".) Although XML namespaces still have some very vocal detractors, most people have accepted and are using them. Furthermore, most new XML tools and technologies use them, a state of affairs that is only likely to increase.

14) SECTION 14: XML NAMESPACE RESOURCES

14.1) What resources are available for learning about XML namespaces?

More links are welcome.

15) SECTION 15: COMMENTS, COMPLAINTS, AND SUGGESTIONS

If you have comments, complaints, or suggestions for new questions, you can mail them to me (Ronald Bourret) at:

   rpbourret@rpbourret.com

Please note that I am currently traveling and have limited email access, so it might be two to three weeks before I reply.

Thanks also to Marc McDonald, Andrew Layman, Derek Denny-Brown, Arjun Ray, and Bob DuCharme for their input.