1

Is a namespace definition required within an XML document where prefixes are used for the document to be considered valid and compliant?

Take the following code for example, is this valid and compliant?

<?xml version="1.0"?>
<ns:root_element>
  <ns:some_element>Some data</ns:some_element>
  <ns:another_element>Some more data</ns:another_element>
</ns:root_element>

(I am aware that the correct way to define the namespace is using the xmlns:ns="" attribute).

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Gareth
  • 5,140
  • 5
  • 42
  • 73
  • *"Is a namespace definition required within an XML document"* - Yes. – Tomalak Feb 09 '18 at 13:11
  • @Tomalak just to clarify sorry, are you saying regardless of using prefixes the namespace needs to be defined? – Gareth Feb 09 '18 at 13:12
  • Also see [this](https://stackoverflow.com/a/4679212/18771) on the topic of the term "valid", because it does not mean what you think it does. – Tomalak Feb 09 '18 at 13:12
  • 1
    I am saying that what you have here is not even XML, let alone "valid" - an XML parser will reject it outright. A namespace first and foremost is a *URI*. Prefixes are basically convenience handles. The URI is the relevant part here, you can't not have a URI. – Tomalak Feb 09 '18 at 13:16
  • @Tomalak thanks, makes sense. – Gareth Feb 09 '18 at 13:18
  • 1
    @Tomalak: Actually, what Gareth has posted technically *is* XML -- it's just not [***namespace-well-formed***](https://stackoverflow.com/a/25830482/290085) XML. But you're right in that colons *should* only be used in XML names for namespace purposes. – kjhughes Feb 09 '18 at 15:15
  • @kjhughes Which parser would accept that file? The ones I tried certainly did not... – Tomalak Feb 09 '18 at 15:40
  • @Tomalak: None that I know of offhand; the major parsers strictly embrace namespace-well-formedness and require that colons should only be used for namespace prefixes. Still, from a specification standpoint, we can't technically say that OP's data is not XML. – kjhughes Feb 09 '18 at 16:10
  • Well, good that that's settled. :) I haven't been aware of the concept of "namespace-well-formedness" until today, so I guess I have learned something, too. – Tomalak Feb 09 '18 at 16:19

1 Answers1

2

Yes, namespace prefix declaration is required because the spec says so. Prefixed element name defined in the XML namespaces specification as follows (notice that the spec even emphasize this requirement):

PrefixedName ::= Prefix ':' LocalPart

The Prefix provides the namespace prefix part of the qualified name, and MUST be associated with a namespace URI reference in a namespace declaration. [source]

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • 1
    Technically, [**XML names *may* use colons unrelated to namespace prefixes and still be well-formed**](https://stackoverflow.com/a/25830482/290085). However, colons *should* be reserved for namespace prefixes, and in order to be ***namespace-well-formed***, such prefixes *must* be declared, as you say. – kjhughes Feb 09 '18 at 15:11