5

The specification of XML Namespaces explains that the interpretation of unprefixed attributes is determined by the element on which they appear. And that the namespace name for an unprefixed attribute name always has no value:

The namespace name for an unprefixed attribute name always has no value.

How does this rule apply to the namespace of the attribute jid in the following cases.

<query xmlns="jabber:iq:roster">
  <item jid="romeo@example.com"></item>
</query>

If the declaration of the namespace and the attribute in question both don't have an prefix, the attribute jid is in the namespace jabber:iq:roster.

<q:query xmlns:q="jabber:iq:roster">
  <q:item q:jid="romeo@example.com"></q:item>
</q:query>

If both, the declaration of the namespace and the attribute have the same prefix, the attribute jidalso has the namespace jabber:iq:roster:

<q:query xmlns:q="jabber:iq:roster">
  <q:item jid="romeo@example.com"></q:item>
</q:query>

But in which namespace is the attribute if the namespace is declared with an prefix but the attribute doesn't have a prefix? I would assume, that the attribute jid

  1. has the default namespace declared in a parent element,

    <parent xmlns="http://example.com">
      <q:query xmlns:q="jabber:iq:roster">
        <q:item jid="romeo@example.com"></q:item>
      </q:query>
    </parent>
    
  2. or no namespace, if there isn't such a declaration.

Do I get this right?

Flow
  • 23,572
  • 15
  • 99
  • 156
Tobias Kräntzer
  • 1,694
  • 1
  • 13
  • 23

2 Answers2

4

The normal interpretation used by XPath and other specs is that an unprefixed attribute is in no namespace.

There are language lawyers who will insist that the namespaces spec doesn't say that. It says that an unprefixed attribute is in whatever namespace the designer of the vocabulary says it is in. But this interpretation isn't very useful in practice; it's what the tools do that matters.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • 1
    But if the designer of a vocabulary defines what an unprefixed attribute is, than I have to know the namespace of that unprefixed attribute to know who the designer of the vocabulary is to get the meaning of the unprefixed attribute. – Tobias Kräntzer May 21 '12 at 08:46
  • 1
    @Tobias The NS of the element where the attribute lies. Not the NS of the attribute since it has no NS. – Ludovic Kuty Oct 30 '14 at 20:03
3

An attribute without prefix is always in the empty name space, i.e. it has no name space. It is doesn't matter whether the enclosing element has a name space or not. That's my reading of the second statement you refer to, and that is the interpretation of all the XML tools I've used.

forty-two
  • 12,204
  • 2
  • 26
  • 36
  • 1
    But if an unprefixed attribute has no namespace, how do I know, that the attribute `jid` form the first and third example is the attribute `jid` of the schema [`jabber:iq:roster`](http://xmpp.org/rfcs/rfc6121.html#schema)? BTW, I know that this is a theoretical discussion. ;-) – Tobias Kräntzer May 21 '12 at 08:43
  • 1
    You get the knowledge by studying the schema. There are two possibilities. 1) the schema defines it as an attribute local to the element or type declaration, or, 2) the schema allows any attribute at this point. – forty-two May 21 '12 at 09:07
  • 1
    Now I got it. The [explanation in the german Wikipedia](http://de.wikipedia.org/wiki/Namensraum_(XML)#Attribute) also helped me a bit. In all 3 examples above the attribute `jid` is the expected attribute of the roster item. Only if the namespace of the attribute is not null and differs from the namespace of the element it appears in, it is something different (e.g., ``). – Tobias Kräntzer May 21 '12 at 13:03
  • The empty NS does not exist. "It has no namespace" is correct but "is always in the empty namespace" is wrong. – Ludovic Kuty Oct 30 '14 at 20:01