28

Is "empty" a valid value for XML namespace? If yes what does it mean?

I have the following XML code but I'm not sure to which namespace Field1 and Field2 elements belong to.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header />
  <soap:Body>
    <Root xmlns="uri">
      <Field1 xmlns="">147079737</Field1>
      <Field2 xmlns="">POL</Field2>
    </Root>
  </soap:Body>
</soapenv:Envelope>
Gui Imamura
  • 556
  • 9
  • 26
Sergej Andrejev
  • 9,091
  • 11
  • 71
  • 108
  • 2
    An interesting question. If it's not legal, then how does one specify "no namespace" for an element whose parent *is* in a namespace? – skaffman Oct 19 '09 at 10:17
  • 10
    @skaffman: It is legal, and this is the way to bring an element into the global namespace. – Frerich Raabe Oct 19 '09 at 10:31
  • 4
    I concur with @Frerich Raabe: Without that, you would have no way of getting rid of an inherited namespace in a descendant node. – Tomalak Oct 19 '09 at 11:13
  • 1
    As I understood from XML spec this doesn't set namespace to global namespace but instead it to scope namespace. Am I wrong? – Sergej Andrejev Oct 19 '09 at 12:09

2 Answers2

29

Yes, it is valid. Section 6.2 in the Namespaces in XML 1.0 Recommendation specifically says:

The attribute value in a default namespace declaration MAY be empty. This has the same effect, within the scope of the declaration, of there being no default namespace.

Quoted from comment:

It is legal, and this is the way to bring an element into the global namespace.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • But is the above a *default* namespace declaration ? – Brian Agnew Oct 19 '09 at 10:21
  • @Brian: Yes; see Definition 3 in Section 3 in the 'Namespace in XML 1.0 Recommendation' I linked in my answer. It says '[Definition: If the attribute name matches DefaultAttName, then the namespace name in the attribute value is that of the default namespace in the scope of the element to which the declaration is attached.]'. DefaultAttName is defined to be 'xmlns'. – Frerich Raabe Oct 19 '09 at 10:26
  • In my case if it's the same as having no namespace then what namespace is it? Does XML have a predefined default namespace, the one that is used then there is no default namespace provided by user like in my case? – Sergej Andrejev Oct 19 '09 at 10:33
  • No. No NS means no NS. Please check [Namespace Myths Exploded](http://www.rpbourret.com/xml/NamespaceMyths.htm) by Ronald Bourret. – Ludovic Kuty Oct 30 '14 at 19:42
  • @Brian And it is not a default NS. It is a default NS un-declaration because as the rec writes it : "This has the same effect, within the scope of the declaration, of there being no default namespace." – Ludovic Kuty Oct 30 '14 at 19:44
5

It is not a valid XML NS. It is not a valid XML NS declaration. Please check section 2.2 of Namespaces in XML 1.0 :

The empty string, though it is a legal URI reference, cannot be used as a namespace name.

It is however the only way to undeclare a default NS declaration if there is one in effect, or it has no effect. See Namespaces in XML 1.0 and 1.1, section 6.2. The 1.1 NS rec added a way to also undeclare a NS declaration with a prefix.

Thus it is a valid value for the xmlns special attribute, but it is not "a valid XML namespace" like the OP wrote it.

Ludovic Kuty
  • 4,868
  • 3
  • 28
  • 42
  • 3
    It seems you are confusing namespace prefixes with the actual namespaces (namespace URIs). The empty string is not a valid namespace prefix, but a valid namespace URI. – vog Feb 21 '17 at 12:11
  • 2
    I don't think I am confusing anything. I know the difference. BTW I can't see where my answer might suggest that. – Ludovic Kuty Feb 21 '17 at 16:34
  • 5
    @lkuty I think you are answering the question whether the empty string is a valid namespace name. The OP however seems to ask whether the empty string is a valid value for the `xmlns` attribute. Maybe this disconnect is why there are two contradicting answers. – Frerich Raabe Feb 23 '17 at 07:38
  • OK, that explains things. In the original question, the OP is mixing and confusing declaration and NS name, and I focused on NS name instead of declaration. – Ludovic Kuty Feb 23 '17 at 11:00