Markup Thoughts: Groovy, XQuery and XML in PHP 5

I'm a Java head, but I'm really starting to warm up to PHP, especially the next version PHP 5. I just saw this neat article about the new XML support in PHP and it's pretty great. I really like the simplicity of dealing with XML and XPath. (All bow down to XPath). Check it out:

$sxe = simplexml_load_file("articles.xml");
foreach($sxe->item as $item) {
print $item->title ."n";
foreach($sxe->xpath('/articles/item/title') as $item) {
print $item . "n";

I personally think that's very cool. It's up there with JSTL x: tags in terms of ease of use, but PHP is so much more deployed that I can see this being used quite a bit as new apps are developed.

Now since I'm looking with a critical eye at page markup (again) I have to say that Groovy looks like it could be a real winner in this space. Like JSP it compiles down to Java bytecode, so it's not interpreted like Velocity or even the Expression Language syntax in JSP. But unlike Java's bonehead handling of NullPointerExceptions and things like that, Groovy would be a real scripting language and really applicable to creating the Web/WAP view layer.

I'm not interested in artificially limiting the power of the script on the pages. There's always tricky logic to be had on the view side of things. Always. No matter what you do, no matter what project I've worked on, there's always a requirement in the view layer that ends up needing a lot of logic. It's "view logic" but it's logic nonetheless. This can end up being some call out to another template page like in Velocity or insane use of JSTL logic or a custom tag or (*gasp*) an embedded scriptlet, but no matter what it always ends up on the page due to resource or time constraints. It's just life.

So looking at it like that, a full-featured scripting language for page creation would be great. I looked at Groovy's Groovlets, and I looked at Cedric's Canvas (a Groovy based Velocity clone) and I looked at "GSP" and they all seem to be lacking in one way or another. What I'd like to see is a PHP-like solution, but with Groovy as the language being used not Java. And I want to put the controller logic in Groovy as well - since, hey, it ends up as bytecode, why not? Rapid App Development plus MVC goodness. I'm down with that... (yesssss, Anthony, I know you use Jython in that layer and it works great. But Groovy would be more Java-like and maybe it'll be a JSR as well...).

This brings me to XQuery. If we're only talking about markup + logic in the view, then this is really the answer since it is being created to do exactly that. Tags are native datatypes and XPath is integrated into the language. It seems that this actually could be a perfect solution. Have you seen XQuery? It's Yet Another Procedural Language akin to JavaScript and Java, but it's got some very cool tricks to it:

  FOR $u IN document("users.xml")
    EVERY $item IN document("items.xml")
      SOME $b IN document("bids.xml")
        ($item/itemno = $b/itemno AND $u/userid = $b/userid)

This is pretty powerful stuff. I'm not sure how much momentum XQuery will ultimatly get in the programming community, but IBM seems to be doing a lot of work in that area and it seems to really match the problem of manipulating user-facing markup for web and handheld based devices pretty well.

It would definitely be cool if Groovy did a lot of that though, no?


< Previous         Next >