XML namespaces and attributes

Most of the time, attributes will not be in any namespace. The namespace spec says (emphasis mine):

A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

There’s a reason that most XML vocabularies use non-namespaced attributes:
When your elements have a namespace and those elements have attributes, then there can be no confusion: the attributes belong to your element, which belongs to your namespace. Adding a namespace prefix to the attributes would just make everything more verbose.

So why do namespaced attributes exist?
Because some vocabularies do useful work with mostly attributes, and can do this when mixed in with other vocabularies. The best known example is XLink.

Lastly, W3C XML Schema has an all too easy way (<schema attributeFormDefault="qualified">) of declaring your attributes as being in a namespace, forcing you to prefix them in your documents, even when you use a default namespace.

Leave a Comment