XML literals in JavaScript?

By | January 12, 2018
Questions:

E4X (Ecma-357) is an extension to ECMAScript that adds XML literals as first-class primitives. That’s awesome, but with only Mozilla and Adobe support (without V8 and IE support too), E4X is virtually dead from a web developer’s perspective that has to support users with any modern browser.

What other work is being done around implementing XML literals in JavaScript? Is there a way to get something similar to XML literals or E4X in JavaScript that anyone is working on? Maybe some plugins for frameworks?

I ran across LunaScript the other day (asana.com/Luna), which has implemented XML literals in their JavaScript-like language. That’s great, but I’ll probably never work at Asana and therefore never write LunaScript.

Answers:

Personally, I don’t think there is any advantage at all to XML literals.

var el= <foo>bar<baz/></foo>;

is not really noticeably better than:

var el= new XML('<foo>bar<baz/></foo>');

There was a reasonable rationale for adding regex literals to JavaScript: that otherwise the backslashes are a super-pain. (Though personally I would have preferred raw strings.) Since backslashes are not common in XML, I don’t think there is any such justification there.

Adding the full (complex!) syntax of XML as basic language syntax is not a win. In fact it has caused grievous security problems in practice.

I want E4X gone, not more of the same.

Questions:
Answers:

There is an open source project for XML in JavaScript:

http://xmljs.sourceforge.net/

XML for <SCRIPT> is a powerful, standards-compliant JavaScript XML parser that is designed to help web application designers implement cross platform applications that take advantage of client-side manipulation of XML data. XML for <SCRIPT> provides a full suite of tools, including:

* A standards-compliant W3C DOM Level 2 processor
* An XPath processor
* A standards-compliant SAX processor
* A simple (classic) DOM processor
* Proxies for XML retrieval from any domain
* Utilities for XML and application development

XML for <SCRIPT> is Free software and is distrubuted under the terms of the GNU Lesser General Public Licence (LGPL) , an open source license.

Questions:
Answers:

js-xml-literal adds support for XML literals in any Javascript engine by means of code rewriting.

https://github.com/laverdet/js-xml-literal

The syntax is a subset of E4X, however the API mimics the DOM more closely. On the client side, you can interact with the browser’s DOM directly using XML literals; and on the server side (NodeJS) you can operate on a virtual DOM before flushing to a string.

Leave a Reply

Your email address will not be published. Required fields are marked *