Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 37,594 times

Contents

Related Categories

Understanding XML Namespaces - Default Namespace

shohoudy

Default Namespace

If most of the elements in the document belong to the same namespace, it is not necessary to prefix each element name. Instead you can define a default namespace that applies to all non-prefixed elements and attributes. The syntax for defining a default namespace is xmlns=”namespace”. For example, if you define the HR namespace as the default namespace, the document becomes easier to read:

<employees xmlns="http://www.devxpert.com/schemas/hr/"
xmlns:py="http://www.devxpert.com/schemas/payroll/">
<employee>
    <id>49982</id>
    <name>Bart Simpson</name>
    <hireDate>2000-07-04</hireDate>
    <py:salary>4000765.00</py:salary>
    <py:taxes>3980765.27</py:taxes>
</employee>
<employee>
    <id>12345</id>
    <name>Hugo Simpson</name>
    <hireDate>2000-05-29</hireDate>
    <py:salary>82000.00</py:salary>
    <py:taxes>16567.87</py:taxes>    
</employee>
</employees>

The default namespace applies to the element on which it was defined and all descendants of that element. If one of the descendants has another default namespace defined on it, this new namespace definition overrides the previous one and becomes the default for that element and all its descendants. For example, in the following document the default namespace for the <salary> element (and its descendants if it had any) is http://www.developerfusion.com/schemas/payroll/:

<employees xmlns="http://www.devxpert.com/schemas/hr/"
xmlns:py="http://www.devxpert.com/schemas/payroll/">
<employee>
    <id>49982</id>
    <name>Bart Simpson</name>
    <hireDate>2000-07-04</hireDate>
    <salary
xmlns="http://www.devxpert.com/schemas/payroll/”>
4000765.00
</salary>
    <py:taxes>3980765.27</py:taxes>
</employee>
</employees>

Yasser started programming at the age of 12 when he wrote his first text-based game on a Commodore PET. He's since moved to IBM mainframes then to Microsoft technologies and has worked as Systems Engineer for IBM and a Chief Architect for Best Software (now Sage Software). Now an independent consultant and trainer, Yasser specializes in XML and Web services and has authored a book on Web Services for Addison Wesley. A Microsoft Most Valuable Professional (MVP) for ASP.NET, Yasser speaks at industry conferences including VS Live and VS Connections and his articles appear regularly in VSM, XML Magazine, and MSDN Magazine. Yasser also publishes the monthly .NET Web Services newsletter.

Comments

  • Multiple namespace prefix from same URI

    Posted by yodha on 26 Jul 2005

    Hi,
    I'm having a problem with xml namespaces.
    My application accepts an xml document from another application, which is then validated using an xsd. The xsd has a namespace defined and uses a prefi...

  • Thank You

    Posted by rakeshmoza on 11 Feb 2005

    Just to say thank you for this article. I have just started learning xml. And after reading this article, I am clear in namespaces atleast.

  • Posted by lars_huttar on 30 Dec 2004

    Glad you found the answer.

  • Posted by puthurjamesaustin on 29 Dec 2004

    The beloe format gives the required o/p



    hi i got the o/p for it

    Posted by puthurjamesaustin on 29 Dec 2004


    The beloe format gives the required o/p



       

    Related Content

  • Extensible Markup Language (XML) Tutorial