Allemis
Allemis

Reputation: 57

XML: namespace with an empty URI

Why the XML validator does not allow the use of an empty namespace URI? Is it the validator (http://www.xmlvalidation.com) feature or the feature of XML in general?

<?xml version="1.0"?>
<root xmlns:em="">
<em:elem/>  
</root>

Is it valid to use any string in namespace URI? (e.g. xmlns="abcd")

Upvotes: 0

Views: 2470

Answers (1)

Michael Kay
Michael Kay

Reputation: 163272

Use of an empty string in a namespace declaration has a specific meaning in XML Namespaces 1.1: it turns it into an "undeclaration", so within its scope, the prefix "em" is not associated with any URI. But XML Namespaces 1.1 has never been widely adopted, either by users or by XML parser writers.

XML Namespaces 1.0 explicitly says (§2.2): The empty string, though it is a legal URI reference, cannot be used as a namespace name.

The answer to the question "Is it valid to use any string in namespace URI?" is a complex one. The XML namespaces spec says on the one hand:

 An XML namespace is identified by a URI reference

and

    The attribute's normalized value MUST be either a URI reference — 
the namespace name identifying the namespace — or an empty string.

It also says:

    The use of relative URI references, including 
same-document references, in namespace declarations 
is deprecated.

(Your example xmlns:p="abcd" uses a relative URI reference.)

But when it comes to defining conformance rules (§7) it does NOT say that a conformant document must use valid URIs in its namespace declarations, and I know this was a deliberate decision (because I challenged it at the time). In practice the vast majority of XML parsers and applications allow any string as a namespace name, but there are a few exceptions, such as the XOM library, and I would advise anyone to choose absolute URIs as good practice.

Upvotes: 5

Related Questions