Monish Nagisetty's Space

Building connectivity on-premise, in the cloud and beyond

XML Namespace Prefixes

The other day another developer and I were discussing the significance of namespace prefixes in XML.  The developer insisted that all XML contain namespace
prefixes.  So I decided to do a little research and record my findings.  The W3C:  Namespaces in XML spec document explains the whole motivation behind
namespacing really well.  First of, all XML should at minimum use namespaces.  In web services, namespaces are an inherent nature of the messaging infrastructure.  A look at a WSDL document will make this pretty clear.  However, I am uncertain whether the use of namespace prefixes is an industry standard at this time.  Although, namespace prefixes does remove any ambiguity in XML in its human readable form.

For example:
Example 1: Namespace Defaulting:

    1 <Person xmlns=http://abccompany.schemas.common/v1.0>

    2     <Name>Simon Chang</Name>

    3     <Age>33</Age>

    4     <Employer>Macrosoft</Employer>

    5     <AccountNumber>123456789</AccountNumber>

    6 </Person>


   

Example 2: Namespace Scoping:

    1 <ns0:Person xmlns:ns0=http://abccompany.schemas.common/v1.0>

    2     <ns0:Name>Simon Chang</ns0:Name>

    3     <ns0:Age>33</ns0:Age>

    4     <ns0:Employer>Macrosoft</ns0:Employer>

    5     <ns0:AccountNumber>123456789</ns0:AccountNumber>

    6 </ns0:Person>

   

Example 2 is easier to read because every element is qualified with a namespace prefix.  This especially helps if there is a long XML document and it is
meant to be read by a end-user.  Other than that, both the examples above are valid xml instances of the same schema and should conform.  Moreover, any XML parser that is implemented well should treat both in the same fashion.

Proponents of namespace prefixing claim that prefixing should be used more in the development community.  However, I think one should be careful when requiring namespace prefixes in their applications.  Especially if interoperability is a concern and vendor application implementations do not support prefixes.  Also, that’s (Number of elements X Size of Prefix X 2) more characters in the payload!

Advertisements

September 3, 2005 Posted by | XML | | Leave a comment