admin管理员组

文章数量:1289496

Is there any way to get E4X(ECMAScript) to work with NodeJS?

It would really help to output slick html/xml without hassle/noise.

It works fine using SpiderMonkey since it is natively implemented, but it doesn't seem to work with NodeJS.

using node

$node
> var name = "World";
> var p = <p>Hello {name}</p>;
...

using spidermonkey

$js
js> var name = "World";
js> var p = <p>Hello {name}</p>;
Hello World
js>

thanks in advance

Is there any way to get E4X(ECMAScript) to work with NodeJS?

It would really help to output slick html/xml without hassle/noise.

It works fine using SpiderMonkey since it is natively implemented, but it doesn't seem to work with NodeJS.

using node

$node
> var name = "World";
> var p = <p>Hello {name}</p>;
...

using spidermonkey

$js
js> var name = "World";
js> var p = <p>Hello {name}</p>;
Hello World
js>

thanks in advance

Share Improve this question edited Feb 20, 2011 at 13:44 zanona asked Feb 20, 2011 at 12:40 zanonazanona 12.7k25 gold badges90 silver badges146 bronze badges 3
  • 1 Not really an answer to your question, but I like to write NodeJS scripts in CoffeeScript, because you can use heredocs, or something like p = """Hello #{name}""". 3 quotation marks also allows you to have newlines inside strings. – Thai Commented Feb 20, 2011 at 14:11
  • @Thai thanks for this it was good to know that there's something helpul like CoffeScript and actually is very clever, I like it a lot. – zanona Commented Feb 20, 2011 at 17:57
  • You can use React & JSX in NodeJS. But it's not exactly what you want. – user1742529 Commented Dec 8, 2017 at 9:25
Add a ment  | 

2 Answers 2

Reset to default 10

Node uses V8, which does not implement E4X as of now.

There's a 2 year old issue, but still active issue on the topic. But it has no real "status" nor was it assigned to anyone.

So in short: The answer is no.

I have developed a babel plugin that adds E4X basic support to NodeJS.

https://www.npmjs./package/babel-plugin-transform-simple-e4x

You can also use the npm simple4x library to parse xml strings to a XML like object.

https://www.npmjs./package/simple4x

The plugin transpiles the following E4X:

var fooId = 'foo-id';
var barText = 'bar text';
var xml =
    <xml>
        <foo id={fooId}>
          {barText}
        </foo>
    </xml>;

To the following JavaScript:

var XML = new require("simple4x");

var fooId = 'foo-id';
var barText = 'bar text';
var xml = new XML("<xml><foo id=\"" + fooId + "\">" + barText + "</foo></xml>");

本文标签: javascriptE4X with NodeJSStack Overflow