Wednesday, April 19, 2006

XForms and easy XML programming

The latest Working Draft for XForms 1.1 includes some attributes for the conditional execution of action handlers. (They were originally proposed by David Landwehr.) These have been available in formsPlayer since version 1.4.3, and we've added a section to the skimstone site that explains how they are used.

I'll leave the detail--if you're interested--to the explanation on skimstone, but the reason I mention these features here is that with them XForms takes a very big step closer to the ideal XML manipulation language.

Managing XML

A lot of work is going on in programming languages at the moment to enhance them to make XML management easier--JavaScript is being extended with E4X, Groovy has its builders (copied in Ruby), C# and VB get XLinq, and so on. But often these techniques feel like they are working either against the host language, or against XML itself.

For example, although it's a great idea, E4X requires us to use script like this to access XML nodes:

var a = people.person.(name=="Joe").age;
As it happens I quite like the syntax, but it is going to be a pain for a lot of people who now have to master both XPath and this new selection approach. At least when you make the commitment to learning XPath you know that you can take that knowledge with you, from XSLT to XForms, through web services, and so on. But learning the syntax specific to a particular programming language just gives you that--a specific syntax for one language.

(By the way, if it's not obvious, the node selected with that E4X expression is the same as with the XPath expression 'person[name="Joe"]/age'.)

Using XML to manage XML

XForms is different in that it is already XML. Admittedly for some that's more than enough to rule it out as a candidate language, and I have to say that in the past the idea of having a programming language that used pointy brackets made my stomach churn. But I've increasingly come to the conclusion that although it wouldn't be my first choice, it's not as bad as it could be, and in fact there is a lot to be said for it (not least that it's...well...XML).

XML means tools

By using XML to define your applications you get benefits at both ends of the development process--you can automatically generate applications (and parts of them) using widely available tools, and you can validate your applications before you even execute them.

What's more--and this is very important--you can easily convert your application to some other language like JavaScript, Java or C#.

Readable

And the results are surprisingly readable.

To give an example, let's say we requested data from an RSS feed, and wanted to process the publication dates of each item in the feed, such that:
  • if there is no date we add one and set it to 'today';

  • if there is a date we convert it from the RFC 288 format used by RSS, to the ISO 8601 format as used by XML Schema and therefore XForms.

We can do this easily with the new XForms 1.1 attributes, simply by looping through all of the returned feed items and then either adding or converting the RSS element pubDate:

<xf:action ev:observer="sub-get-rss-feed" ev:event="xforms-submit-done">
<xf:action iterate="instance('inst-current-feed-rs')/channel/item">
<xf:action if="pubDate">
<xf:setvalue ref="pubDate"
value="inline:convrfc288datetoiso8601(.)" />
</xf:action>

<xf:action if="not(pubDate)">
<xf:duplicate origin="instance('inst-control')/templates/pubDate"
ref="." />
<xf:setvalue ref="pubDate" value="now()" />
</xf:action>
</xf:action>
</xf:action>
If you are familiar with writing 'programs' with XSLT or using XML to drive pipeline processing in systems like Cocoon, then you will know that you can end up with documents that seem quite verbose...until you look at the amount of functionality that you have packed into a handful of statements, and think of the equivalent in a more traditional language. XForms is much the same--an XML programming language that makes it very easy to manipulate XML.



Tags: | | | | | | | | | | |

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home