XML Namespace Declaration

A namespace declaration is an attribute with a fixed name xmlns, and which can occur multiple times in one start tag.

<tagName xmlns{:Prefix}=URI></tagName>

Description

If xmlns is followed by a colon and a name, that name is the so-called prefix or alias of the namespace. The namespace itself is a URI. When a subsequent element or attribute (or even the element in which the namespace is being declared) uses the same prefix, its URI is deemed to be the one with which it was declared.

Conversion to Struct

When converted without the /full switch, namespace declarations are ignored.

When converted with the /full switch, a namespace declaration converts to a Struct called by the prefix (if specified) or #default (if no prefix is declared), and the xmlClass attribute set to namespace-declaration.

Annotations

The Struct for the namespace gets only one annotation (xmlClass=namespace-declaration), but an element or attribute with a namespace prefix has the xmlNamespaceAlias = prefix and xmlNamespaceURI = uri tags set. This also applies to the element in which the namespace is declared.

XML to Struct Conversion: Namespaces

XML with namespace declaration and use of prefix:

<doc xmlns:ss="name://Space" xmlns='name://Default'>
<p class="note" ss:type="caution">Be careful. </p></doc>

String returned by $dbgString (after conversion with /full):

[]
  [doc]
    [$tags]
      [xmlClass] = element
      [xmlNamespaceURI] = name://Default    Callout 1
    [ss] = name://Space                     Callout 2
      [$tags]
        [xmlClass] = namespace-declaration   
    [#default] = name://Default             Callout 3
       [$tags]
        [xmlClass] = namespace-declaration
    [p]
      [$tags]
        [xmlClass] = element
        [xmlNamespaceURI] = name://Default  Callout 4
      [class] = note                          
        [$tags]
          [xmlClass] = attribute            Callout 5
      [type] = caution
        [$tags]
          [xmlClass] = attribute
          [xmlNamespaceURI] = name://Space  Callout 6
          [xmlNamespaceAlias] = ss
      [] = Be careful.
  1.  Namespace is declared in doc element, so it gets xmlNamespaceURI annotation.
  2.  Namespace declaration with prefix (alias)
  3.  Default namespace declaration (without prefix)
  4.  The p element does not specify a prefix, so it gets the default namespace.
  5.  Default namespace declarations do not apply directly to attribute names.
  6.  The type attribute is preceded by the prefix, so it gets the namespace declared for ss. Both the xmlNamespaceURI and xmlNamespaceAlias attributes are set.