Understanding XML Namespaces

Namespaces & Attributes

You can prefix attributes names with a namespace prefix just like element names. For example, in the following snippet, the value attribute of the <salary> element belongs to the payroll namespace http://www.developerfusion.com/schemas/payroll/:

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

However, default the namespaces does not apply to attribute names. For example, in the following snippet, the id attribute does not belong to any namespace even though there’s a default namespace declared:

<employee xmlns="http://www.devxpert.com/schemas/hr/" id="90385">
</employee>

I hope that this article has helped you understand XML Namespaces and how they work. If you’re not familiar with XSD schemas, stay tuned for my upcoming articles on this topic. Learning XSD will help you fully understand XML Namespaces.

You might also like...

Comments

About the author

Yasser Shohoud United States

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 System...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”