Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Default namespaces for unqualified attribute names?

I'm trying to understand the correct interpretation of the "Namespaces in XML 1.0 (Third Edition)" definition for unqualified attribute namespaces.

"The namespace name for an unprefixed attribute name always has no value."

And later in the same section:

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

So if I want to declare a default namespace for an element (and its children), do I also have to declare a prefix-namespace mapping for any attributes which reside within that namespace?

For example, in this example

<parent xmlns="http://example.com/foo">     <child attrib="value">text</child> <parent> 

I would interpret the above definition to say that the namespace of attrib is empty.

So if I needed attrib to have the same namespace as parent, then I would be forced to do this?

<foo:parent xmlns:foo="http://example.com/foo">     <foo:child foo:attrib="value">text</foo:child> <foo:parent> 

or this?

<parent xmlns="http://example.com/foo" xmlns:foo="http://example.com/foo">     <child foo:attrib="value">text</child> <parent> 

This seems silly to me as it appears to defeat the purpose of default namespaces. I'm hoping that I'm just misunderstanding the spec.

like image 569
mckamey Avatar asked Jul 22 '10 19:07

mckamey


People also ask

What is the default namespace in XML?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

What is the default namespace The namespace used by default?

The default namespace is the one for all tags without qualified names. Namespace prefixes (and the default namespace) are assigned to URLs using a reserved XML attribute (xmlns for the default namespace, xmlns: name for any namespace prefix).

Can XML attributes have namespaces?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

What is the default namespace in XML Mcq?

The default namespace is used in the XML document to save you from using prefixes in all the child elements. The only difference between default namespace and a simple namespace is that: There is no need to use a prefix in default namespace.


2 Answers

You're correct. The idea behind attributes not being part of the default namespace is that they are considered to exist in an "element namespace" — so in this case, <foo:child/> is considered to be the 'namespace' for @attrib. Note that this is just conceptual; there's no API or anything that refers to attribute namespaces this way.

This was chosen because multiple elements may have attributes with the same names, but different meanings — unlike a traditional namespace, which is a set of names (so no duplicates). In a way, it gives more structure to the namespace, instead of having a flat set.

You can read about this in a very old version of the Namespaces recommendation.

This convention means that whenever you see a prefixed attribute, it represents some 'additional' information which isn't related to the main schema in the document.

like image 193
porges Avatar answered Oct 22 '22 07:10

porges


Per the spec, you are correct to consider the namespace of the attrib in the first example to be empty. However, there is a subtlety here that may not be readily obvious.

Consider this example further down in the spec of an element with two attributes with the same name (one prefixed and another unprefixed).

<!-- This is OK, even though an element cannot have two attributes       with the same name --> <x xmlns:n1="http://www.w3.org"     xmlns="http://www.w3.org" >   <good a="1"     n1:a="2" /> </x> 

This is conformant because the two attributes are indeed in two different namespaces:

  • n1:a belongs to http://www.w3.org namespace (which is the namespace of good as well)
  • a is treated to belong to an inaccessible namespace http://wwww.w3.org > good (and different from the namespace of good).

Note that http://wwww.w3.org > good namespace does not exist; for example, you cannot query for attributes in this namespace with XPath. If you ask for namespace-uri(\\good\a), it will be empty. To make the idea of a separate element namespace concrete, I made up a namespace that has both the element namespace and name together with a separator (> is not allowed unescaped in attribute values anyways).

Now, instead of saying that the two attributes are in two different namespaces, it is more correct to say that they belong to two different namespace partitions:

  • n1:a attribute belongs to the Global Attribute Partition (http://www.w3.org)
  • good element belongs to All Element Types Partition (also http://www.w3.org)
  • a belongs to the Per Element Type Partition Of good (i.e., http://wwww.w3.org > good).

Here's the relevant part of the spec Porges linked to:

A.2 XML Namespace Partitions

In order to support the goal of making both qualified and unqualified names useful in meeting their intended purpose, we identify the names appearing in an XML namespace as belonging to one of several disjoint traditional (i.e. set-structured) namespaces, called namespace partitions. The partitions are:

The All Element Types Partition All element types in an XML namespace appear in this partition. Each has a unique local part; the combination of the namespace name and the local part uniquely identifies the element type.

The Global Attribute Partition This partition contains the names of all attributes which are defined, in this namespace, to be global. The only required characteristic of a global attribute is that its name be unique in the global attribute partition. This specification makes no assertions as to the proper usage of such attributes. The combination of the namespace name and the attribute name uniquely identifies the global attribute.

The Per-Element-Type Partitions Each type in the All Element Types Partition has an associated namespace in which appear the names of the unqualified attributes that are provided for that element. This is a traditional namespace because the appearance of duplicate attribute names on an element is forbidden by XML 1.0. The combination of the attribute name with the element's type and namespace name uniquely identifies each unqualified attribute.

In XML documents conforming to this specification, the names of all qualified (prefixed) attributes are assigned to the global attribute partition, and the names of all unqualified attributes are assigned to the appropriate per-element-type partition.

like image 20
4 revs Avatar answered Oct 22 '22 08:10

4 revs