XML Default namespaces for unqualified attribute names?

后端 未结 4 684
清酒与你
清酒与你 2020-12-05 05:01

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

相关标签:
4条回答
  • 2020-12-05 05:15

    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.

    0 讨论(0)
  • 2020-12-05 05:19

    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.

    0 讨论(0)
  • 2020-12-05 05:25

    I've found the explicit explanation in "XML in a Nutshell" by Elliotte Rusty Harold:

    The attributes are a different story. Default namespaces only apply to elements, not to attributes.

    0 讨论(0)
  • 2020-12-05 05:27

    Your interpretation of the spec is correct. Some kind of rationale is also given in the second paragraph of section 6.2 in the namespaces spec you referenced:

    the interpretation of unprefixed attributes is determined by the element on which they appear.

    But I would also be interested in some more details on why this specific behavior was chosen.

    0 讨论(0)
提交回复
热议问题