leastfixedpoint

E4X: I want my S-expressions back

This page is a mirrored copy of an article originally posted on the (now sadly defunct) LShift blog; see the archive index here.

Sat, 24 June 2006

E4X is a new ECMA standard (ECMA-357) specifying an extension to ECMAScript for streamlining work with XML documents.

It adds objects representing XML to ECMAScript, and extends the syntax to allow literal XML fragments to appear in code. It also supports a very XPath-like notation for use in extracting data from XML objects. So far, so good - all these things are somewhat useful. However, there are serious problems with the design of the extension.

If E4X objects were real objects, if there were a means of splicing a sequence of child nodes into XML literal syntax, and if E4X supported XML namespace prefixes properly, most of my objections would be dealt with. As it stands, the overall verdict is “clunky at best”.

These are my main complaints:

In my opinion, E4X violates several programming language design principles: most importantly, those of regularity, simplicity and orthogonality, but also preservation of information, automation and structure. SXML, perhaps in combination with eager comprehensions, provides a far superior model for producing and consuming XML. Sadly, there’s no real alternative for ECMAScript yet - we’re limited either to library extensions, or to using the DOM without any syntactic or library support at all.

Comments

On 25 June, 2006 at 12:18 am, Chris Double wrote:

It’s also frustrating that you can’t take an E4X node and insert it directly into the browser DOM.

There is no ‘toHTML’ which prevents the use of E4X for dynamically generating valid HTML - you have to stick with XHTML. Unfortunately some browsers require things like ‘SCRIPT’ tags to have a closing tag rather than the shortcut method. ie. They need not . E4X is incapable of handling this and is non extensible for generating your own output methods.

As an s-expression style approach I tend to use code like this:

http://mg.to/2006/02/27/easy-dom-creation-for-jquery-and-prototype

Chris.

On 26 June, 2006 at 10:08 am, tonyg wrote:

Thanks for the link, Chris - something similar had occurred to me, too (both the SXML and Seaside HTML canvas approaches should translate to Javascript pretty well), but I haven’t had a chance to explore the design space properly yet.

As it happens, I got into experimenting with E4X through hacking a Seaside-a-like onto your server-side javascript implementation. I’ll post about that soon :-)

On 26 June, 2006 at 11:18 am, Chris Double wrote:

Nice! I look forward to seeing what you’ve done :)

On 7 March, 2008 at 11:21 am, tonyg wrote:

As it happens, I was wrong about the lack of computed tags:

js> var y = 'sometag';
js> (<{y}/>).toXMLString();
<sometag/>

On 7 March, 2008 at 11:42 am, tonyg wrote:

Second embarrassing update: If I’d used an XMLList instead of an array, my buildItems() would have worked just as I intended:

function buildItems() {
  return <><item>Hello</item>
           <item>World!</item></>;
}
var doc = <mydocument>{buildItems()}</mydocument>;

On 17 September, 2008 at 10:11 pm, Prestaul wrote:

I think that this article was a badly researched, knee-jerk reaction. This works:


function buildItems() {
return <>
Hello
World!
;
}
var doc = {buildItems()};

So does this:


function buildItems() {
var list = <>; // or: new XMLList()
list += Hello;
list += World!;
return list;
}
var doc = {buildItems()};