Namespaces

E4X and Namespaces

Oftentimes, XML documents will contain elements from a variety of namespaces. You can declare the default namespace for your E4X objects by placing the statement:

default xml namespace = "http://www.w3.org/1999/xhtml";

within the same scope as your E4X. You can also change namespaces at any time, by repeating the statement.

default xml namespace = "http://www.w3.org/1999/xhtml";
var a = <p>Some text</p>;
default xml namespace = "http://www.mozilla.org/keymaster/gat...re.is.only.xul";
var b = <label>More text</label>;
a.appendChild(b);

gives

<p xmlns="http://www.w3.org/1999/xhtml">
  Some text
  <label xmlns="http://www.mozilla.org/keymaster/gat...re.is.only.xul">More text</label>
</p>

name()

Using name() on an XML object returns a QName object (Qualified Name). QName is a global constructor available in JavaScript implementations supporting E4X. QName objects have two properties of particular relevance. The localName property returns the name of the element, without any namespace prefix. The uri property returns the uri of the namespace that the element is in.

var info = a.name();
info.localName; // returns 'p'.
info.uri; // returns "http://www.w3.org/1999/xhtml"

Elements in no namespace return the empty string as their uri.

The Namespace global constructor is very similar to QName. Namespace differs in its toString method, and in that it has a prefix property instead of a localName property.

For further information on namespaces in E4X, see Processing XML with E4X.

Next is the global XML object.