Item 20. Use Namespaces for Modularity and Extensibility

Namespaces are the standard tool for mixing multiple vocabularies into a single document. They are essential for allowing software to quickly and definitively decide which elements come from which applications, even when there's no potential conflict between names in the different applications. All new XML applications should define a namespace for their elements. Even if you don't plan to integrate your vocabulary with other applications, your users might want to integrate it with theirs.

This need not be particularly onerous. The simplest case, a document that uses a single namespace, can be handled with a single default namespace declaration on the root element. You don't even need to use namespace prefixes. For example, the following document places all elements in the namespace.

 <?xml version="1.0"?> <game xmlns="">   <move>f3</move>   <move>e5</move>   <move>g4</move>   <move>Qh4++</move> </game> 

Of course, you should design all your processing so that it depends on the local name and namespace URI, rather than the namespace prefix (or lack thereof). The following document is exactly equivalent to the one above.

 <?xml version="1.0"?> <chess:game xmlns:chess=         "">   <chess:move>f3</chess:move>   <chess:move>e5</chess:move>   <chess:move>g4</chess:move>   <chess:move>Qh4++</chess:move> </chess game> 

Item 21 covers this further.

Effective XML. 50 Specific Ways to Improve Your XML
Effective XML: 50 Specific Ways to Improve Your XML
ISBN: 0321150406
EAN: 2147483647
Year: 2002
Pages: 144 © 2008-2017.
If you may any questions please contact us: