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

9
  • "Is a namespace definition required within an XML document" - Yes. Commented Feb 9, 2018 at 13:11
  • @Tomalak just to clarify sorry, are you saying regardless of using prefixes the namespace needs to be defined? Commented Feb 9, 2018 at 13:12
  • Also see this on the topic of the term "valid", because it does not mean what you think it does. Commented Feb 9, 2018 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. Commented Feb 9, 2018 at 13:16
  • 1
    @Tomalak: Actually, what Gareth has posted technically is XML -- it's just not namespace-well-formed XML. But you're right in that colons should only be used in XML names for namespace purposes. Commented Feb 9, 2018 at 15:15

1 Answer 1

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]

Sign up to request clarification or add additional context in comments.

1 Comment

Technically, XML names may use colons unrelated to namespace prefixes and still be well-formed. However, colons should be reserved for namespace prefixes, and in order to be namespace-well-formed, such prefixes must be declared, as you say.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.