Wednesday, January 16, 2008

XML is DEAD!!! Welcome Glimmerized XML!

Yes!!! You heard it! XML is finally DEAD to all XML haters... why? Because, XML has been officially Glimmerized!

Ok, this originally happened with the Builder framework, but it was very easy to implement something similar with Glimmer's flexible engine.

Take this XHTML document for example:

<html>
  <body>
    <form id="contact" class="information">
      <input id="name" name="contact.name" />
    </form>
  </body>
</html>

Here is the same document Glimmerized:

html {
  body {
    form(:id => "contact", :class => "information") {
      input(:id => "name", :name => "contact.name")
    }
  }
}

Isn't it much cleaner? Isn't it much much easier to read? It's about time we got something like that, thanks to the power of Ruby and the flexibility of the Glimmer engine.

Since XHTML is a special form of XML, Glimmerized syntax goes further to give special treatment to ids and classes due to their popular use:

html {
  body {
    form_contact_information {
      input_name(:name => "contact.name")
    }
  }
}

The word following the first underscore after an element name represents the ID. The word following the second underscore after an element name represents the class. If there is no ID, then simply place two underscores before the class name (e.g. form__information)

How do you print the resulting XML? Simply assign the html element to a variable and call to_xml on it:

document = html { }
document.to_xml #prints <html></html>

What else? If most of the XHTML on your project is maintained by programmers, they'd definitely appreciate the Glimmerized syntax, wouldn't they? If so, you can easily run a Ruby script that Glimmerizes all your XHTML documents so that you can delete them and rely on the pretty Ruby syntax instead! What happens if you later change your mind however, and push the task of maintaining the XHTMLs to web designers? Simple! Run another Ruby script that converts Glimmerized XHTML documents back into the basic XML syntax!

Now that's a funeral that does not fill my heart with any regret!!! Rust in pieces XML!

8 comments:

Antoine said...

Andy, doesn't this look just like the Builder API ?

How do you handle namespaces ?

AhtiK said...

Nice idea! But let's face it - replacing closing tag with } makes it harder to read.

Java has the same problem - hard to see which } is closing what :)

{} is much faster to write yet harder to read IMHO.

Frederick Polgardy said...

You've kind of reinvented rxml. But isn't it nice to discover something totally independently? :)

Annas "Andy" Maleh said...

Ideas are in the air Fred! ;)

Ahtik, I agree, it's a matter of personal preference. I personally find it easier to read and write Builder-style syntax provided it has correct indentation.

Antoine, you're right. I updated the post to indicate that. Glimmerized XML support was written in a couple of hours for fun, so namespaces are not being handled yet. I was just toying with how easy it is to leverage the Glimmer engine after it was refactored to follow the Chain of Responsibility Design Pattern.

Ed Merks said...

I was looking at JSON a while back which has similar ideas. I tried to find out if there was a standard way to represent xsi:type and never did turn up an answer.

I have the same question here. EMF could easily produce any nice uniform syntax but when parsing something, we really need to know exactly what type of object to create. General data structures also involve references other than containment (which is denoted by nesting) so there needs to be a standard way for that as well. And the world doesn't consist of isolated stand alone documents, so support for references that span document is important too.

In my opinion, XML is marginally human readable and it's real purpose is to be machine readable, not the be-all-and-end-all in syntax. Imagine expressing Java with XML syntax. It almost makes you sick to think about it.

I certainly don't agree that having a closing brace that consists of a long string of letters improves readability. Only indentation makes either form readable...

Dave Hoover said...

Andy, it's exciting to see that the Glimmer engine is so easily reused. That's a great sign.

Annas "Andy" Maleh said...

Antoine, namespaces are now supported. Please check my blog post about it:
http://andymaleh.blogspot.com/2008/01/glimmerized-xml-namespaces.html

Annas "Andy" Maleh said...

EMF could easily produce any nice uniform syntax but when parsing something, we really need to know exactly what type of object to create. General data structures also involve references other than containment (which is denoted by nesting) so there needs to be a standard way for that as well. And the world doesn't consist of isolated stand alone documents, so support for references that span document is important too.

The goal of the Glimmerized DSL is to simplify the authoring of XML documents. So, if XML supports your needs Ed, so will Glimmerized XML when it is done.