Which namespace is used to associate namespace identifiers with element names ? Is there any effect on the attributes ?

Platform: WPF| Category: XML Namespaces

There is one more type of namespace declaration that can be used to associate namespace identifiers with element names. This is known as a default namespace declaration which uses the following syntax.

xmlns=’<namespace identifier>’

Notice that there is no prefix. When a default namespace declaration is used on an element, all unqualified element names within it’s scope are automatically associated with the specified namespace identifier.

Default namespace declarations, however, have absolutely no effect on attributes. The only way to associate an attribute with a namespace identifier is through a prefix.

Consider the following example.

[XAML]

<d:student  xmlns:d=’http://www.develop.com/student’
     xmlns=’urn:foo’ id=’3235329’
>
  <name>Jeff Smith</name>
  <language xmlns=’’>C#</language>
  <rating>35</rating>
</d:student>

Here, ‘student’ is from the http://www.develop.com/student namespace while ‘name’ and ‘rating’ are from the default namespace urn:foo. The ’id’ attribute doesn’t belong to a namespace since attributes aren’t automatically associated with the default namespace identifier.

This example also illustrates that you can undeclare a default namespace by simply setting the default namespace identifier back to the empty string as shown in the language element (remember you cannot do this with prefix declarations). As a result, the language element also doesn’t belong to a namespace.

The syntax for default namespaces was designed for convenience but they tend to cause more confusion than their worth. The confusion typically stems from the fact that elements and attributes are treated differently and it’s not immediately apparent that nested elements are being assigned the default namespace identifier. Nevertheless, in the end, choosing between prefixes and default namespaces is mostly a matter of style, except when attributes come into play.

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.