XML

Default Namespaces

A default namespace is used by an element if the element does not have a namespace prefix; this default is also used by all child elements of that element if they do not have a namespace prefix. The declaration for a default namespace looks like this:

  <elementName xmlns='URL'>

Our earlier example could be rewritten using default namespaces as follows:

  <?xml version="1.0"?>
  <message xmlns='http://www.northwindtraders.com/bill'
     xmlns:body='http://www.northwindtraders.com/message'>
     <date>1/1/2002</date>
     <body:body>Your current balance is $1,999,999.00. Please pay
        immediately. Thank you. </body:body>
  </message>

The first namespace is the default namespace. The default namespace is used by the message element and the date child element. The body child element still uses the body namespace.

You can also declare namespaces in the begin tag of child elements, as shown here:

  <?xml version="1.0"?>
  <message xmlns='http://www.northwindtraders.com/bill'
     xmlns:body='http://www.northwindtraders.com/message'>
     <date>1/1/2002</date>
     <body:body>
        <customerName xmlns='http://www.northwindtraders.com/name'>
           Fred Smith
        </customerName>
        Your current balance is $1,999,999.00. Please pay
        immediately. Thank you.
     </body:body>
  </message>

Here the customerName child element is defined by a default namespace.

You can set the default namespace equal to an empty string (""), as shown here:

  <?xml version="1.0"?>
  <message xmlns=""
     xmlns:body='http://www.northwindtraders.com/message'>
     <date>1/1/2002</date>
     <body:body>Your current balance is $1,999,999.00. Please pay
        immediately. Thank you. </body:body>
  </message>

If you do this, there will be no namespace associated with elements that are not preceded by a namespace prefix.

NOTE
Default namespaces apply only to elements; they do not apply to attributes.