Friday, March 02, 2007

XML Events 2.0

The latest draft of XML Events 2.0 contains a new, simple, and very powerful idea - allowing XPath to handle events and even animate elements.

But there are a few ways it could be extended even further.

XPath observers

Firstly, the draft spec hints that the "observer" attribute could in future be used to give any URI, not just from the current document. But it would be far better to allow an XPath statement, rather than a URI. Then you could specify a whole set of nodes. For example, consider:

<.script event="mouseclick" observer="//img">.
  document.getElementById("paragraph2").style.fontSize = "5";
which uses the XML Events namespace "ev" to alter the paragraph font when any image is clicked.

XPath event() function

Secondly, there's a new XPath event() function, which coupled with the new "while" attribute allows for animation. Consider this implementation of drag and drop:

<.script observer="//img" while="event(button) =1" />.

Not bad eh, especially compared to mountains of javascript. The new XPath event() function allows you to figure out mouse position and even the time when the event occured - which is very useful when it comes to animation and controlling timing.

<.style observer="//img" while="true()">.
./@css:left = //div1/@css:left + 10;

Note the "style" element - this is just a CSS stylesheet using XPath. It positions each image 10 pixels to the right of the div1 element. If the div1 element is moved, then the images will move to maintain their relative position.

There are two benefits to this approach. Firstly, rather than using CSS selectors, you can use the full power of XPath to indicate the elements being styled. And secondly, you can use all sorts of XPath functions to assign values - like avg() or sum() or even sin().

XPath + XML Events

So I would urge the XML Events authors to extend the use of XPath in their spec. Firstly, XPath should be used in the observer attribute, to allow any collection of elements to be handled. Secondly, please keep the new event() funtion - it's great! And thirdly, XPath should be used to allow the animation of XML values.

No comments: