Click to return to the XML (Extensible Markup Language) home page    
Reasons for Namespaces     Object Model Support     Introduction to Namespace...    
Web Workshop  |  XML (Extensible Markup Language)

Declaring Namespaces


The XML parser provides support for associating elements and attributes with namespaces. There are two ways of declaring namespaces: default declaration and explicit declaration.

Default Declaration

The default declaration declares a namespace for all elements within scope.

<BOOK xmlns="urn:BookLovers.org:BookInfo"> 
  <TITLE></TITLE>
</BOOK>

The following example declares the "BOOK" element and all elements and attributes within it ("TITLE", "PRICE", "currency") are from the namespace "urn:BookLovers.org:BookInfo". The attribute "xmlns" is an XML keyword and is understood by the parser to be a namespace declaration.

<BOOK xmlns="urn:BookLovers.org:BookInfo">
  <TITLE>A Suitable Boy</TITLE>
  <PRICE currency="US Dollar">22.95</PRICE>
</BOOK>

Default declarations are useful when a node (element and subelements) is from the same namespace.

Explicit Declaration

The following example declares "bk" and "money" to be shorthand for the full names of their respective namespaces. All elements beginning with "bk:" or "money:" are considered to be from the namespace "urn:BookLovers.org:BookInfo" or "urn:Finance:Money," respectively.

<bk:BOOK xmlns:bk="urn:BookLovers.org:BookInfo"
         xmlns:money="urn:Finance:Money">
  <bk:TITLE>A Suitable Boy</bk:TITLE>
  <money:PRICE money:currency="US Dollar">22.95</money:PRICE>
</bk:BOOK>

Explicit declarations are useful when a node contains elements from different namespaces.



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.