Why is the "xmlns" attribute NOT in the "xmlns" namespace?

I) Reading "xmlns"

In the XLINQ implementation, the "xmlns:" prefix is associated with thehttp://www.w3.org/2000/xmlns/ namespace, which is correct according to the w3c specs.

However, the "xmlns" attribute, which is used for declaring the default namespace, is associated with the empty namespace. This is incorrect according to the w3c specs, which state that also the bare "xmlns" attribute should be associated with thehttp://www.w3.org/2000/xmlns/ namespace.

I would expect the following test to work both for prefixed and unprefixed namespace delcarations (but it doesn't work):

attribute.Namespace == XNamespace.Xmlns;

II) Writing "xmlns"

In order to write a prefixed namespace declaration, one has to use:

element.SetAttributeValue(XNamespace.Xmlns+prefix,urn);

However, in order to write the default namespace declaration, one has to use:

element.SetAttributeValue("xmlns",urn);

I would expect the following (but it doesn't work):

element.SetAttributeValue(XNamespace.Xmlns,urn);

Why the special behavior of the unprefixed "xmlns" attribute?

III) Testing for the empty namespace

Another related question, is there a better method for testing for the empty namespace other than:

String.IsNullOrEmpty(namespace.NamespaceName);

[3442 byte] By [YuvalGilboa] at [2008-1-6]
# 1

Answers in the same order as your questions. A bit long.

1. Reading "xmlns".

If you want to determine if an attribute is really a namespace declaration you should use the property XAttribute.IsNamespaceDeclaration. It works both for prefixed namespace declarations and the default namespace declaration.

Regarding your other point, in the first CTP XLinq did use XNamespace.Xmlns + "xmlns" for the XName that represents the default namespace declaration. That choice aligns better with the rest of System.Xml. However, in our usability studies, both novice and advanced users had a difficult time constructing and/or querying for the default namespace declaration. The most intuitive choice was new XAttribute("xmlns"), the same way it is expressed in the XML format. This is mostly why the XName for the default namespace declaration is now XNamespace.None + "xmlns" or simply "xmlns". Regarding correctness, the XML Namespace 1.1 does mandate the namespace for the prefixed namespace declarations but there is no mention of the namespace for the default namespace declaration - this is not the first whole in w3c recommendations.

2. Writing "xmlns"

XNamespace.Xmlns is an XNamespace not a XName as required by the first argument of XElement.SetAttributeValue(). Probably what you want is XElement.SetAttributeValue(XNamespace.None + "xmlns", ...). Also, there is no special behaviour around "xmlns" - that's exactly what we tried to avoid. All methods that require a name take XName. An XName is typically constructed from an XNamespace + "localName" where local name is a string. The same info can be provided as a string of the format "{namespaceName}localName" aka an expanded name. If the namespace name is the empty string one can simply write "localName". That's why for unqualified attributes (eg. "xmlns") one can get by without specifying an XNamespace a la XElement.SetAttributeValue("xmlns", ...).

3. Testing for the empty namespace. The XNamespace.NamespaceName is never null. The XNamespace objects are atomized based on namespace name and consequently there is only one object which has XNamespace.NamespaceName equal to the empty string, namely XNamespace.None. One can just test for object identity a la XNamespace.None == element.Namespace

Ion

IonVasilian at 2007-9-28 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 2
Thanks for all your answers. Things are much clearer now.
YuvalGilboa at 2007-9-28 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...

Visual Studio Orcas

Site Classified