E4X! Where have you been all my life?

How in the heck did I miss this? After seeing a couple of blog posts here on weblogs.asp.net about E4X I went out and did a bit of googling. In case you missed the boat like I did here's the scoop: E4X is a set of extensions to java script that was standardized back in June of 2004. It basically makes XML a first class citizen of the language (hmmm wonder where some of the ideas for XLINQ came from?). There are a couple of implementations of note that I have found. Namely firefox 1.5rc1 and macromedia action script.

Now for the bad news. From w3schools.com : "So far there is no indication of E4X support in Internet Explorer 7". Sorry IE, all the cool stuff in web browser land is hapening outside of Microsoft.

In the mean time go get Firefox 1.5 and try this out:

<html>

<head>

<script>

function createHeaderRow()

{

      return <tr><th>First Name</th><th>Last Name</th></tr>;

}

 

function onLoad()

{

      var people = new XML(document.getElementById("people").innerHTML);

      var table = <table border="1" />;

      table.tr += createHeaderRow();

      for each (var person in people.person.(@state=='active'))

      {

            table.tr += <tr><td>{person.firstname.text()}</td><td>{person.lastname.text()}</td></tr>;

      }

 

      var source = document.getElementById("source");

      source.value = table;

 

      var output = document.getElementById("output");

      output.innerHTML = table;

}

</script>

</head>

<body onload="onLoad()">

 

<div id="output"></div>

 

<hr>

 

<textarea id="source" cols="80" rows="20"></textarea>

 

<script id="people" type="text/xml">

<people>

      <person state="active">

            <firstname>David</firstname>

            <lastname>Findley</lastname>

      </person>

      <person state="inactive">

            <firstname>Joe</firstname>

            <lastname>Schmoe</lastname>

      </person>

      <person state="active">

            <firstname>Lance</firstname>

            <lastname>Hunt</lastname>

      </person>

</people>

</script>

 

</body>

</html>

 

some additional resources:

 

http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-357.pdf

http://developer.mozilla.org/en/docs/E4X

http://www.w3schools.com/e4x/default.asp

http://www.linkwerk.com/pub/javascript/e4x/e4x-tester/

 

 

 

 

2 Comments

Comments have been disabled for this content.