Chapter 16. ECMAScript for XML

In 2002, a group of companies led by BEA Systems proposed an extension to ECMAScript to add native XML support to the language. In June 2004, ECMAScript for XML (E4X) was released as ECMA-357, which was revised in December 2005. E4X is not its own language; rather, it is an optional extension to the ECMAScript language. As such, E4X introduces new syntax for dealing with XML, as well as for XML-specific objects.

Though browser adoption has been slow, Firefox versions 1.5 and later support almost the entire E4X standard. This chapter focuses on the Firefox implementation.

E4X Types

As an extension to ECMAScript, E4X introduces the following new global types:

  • XML — Any single part of an XML structure

  • XMLList — A collection of XML objects

  • Namespace — Mapping between a namespace prefix and a namespace URI

  • QName — A qualified name made up of a local name and a namespace URI

Using these four types, E4X is capable of representing all parts of an XML document by mapping each type, specifically XML and XMLList, to multiple DOM types.

The XML Type

The XML type is the most important new type introduced in E4X, because it can represent any single part of an XML structure. An instance of XML can represent an element, an attribute, a comment, a processing instruction, or a text node. The XML type inherits from the Object type, so it inherits all of the default properties and methods of all objects. There are a few ways to create a new XML object, the first of which ...

Get Professional, JavaScript® for Web Developers, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.