Introduction to XML Events
By Micah Dubinko2005-04-22
From Events to XML Events
Up to this point, everything about events has been tied to procedural script. The XML Events specification builds upon the foundation of DOM Level 2 Events, adding a declarative way to hook up event observers. The W3C XForms specification (see Resources) is one of the first to define a library of XML Events-compatible elements that can accomplish common tasks declaratively. Listing 4 shows an example of this in XForms. (For the complete namespace declarations in use here, see the full example in Listing 5.)
Listing 4. Declarative event handlers
<xf:trigger>
<xf:label>Reset the form</xf:label>
<xf:reset ev:event="DOMActivate" model="mymodel" />
</xf:trigger>
XML Events is defined as a number of attributes. In Listing 4, the ev:event attribute specifies the specific event being listened for; the handler is the xf:reset element, and the observer defaults to the parent element, xf:trigger. Notice that no script is needed in this listing.
The XML Events specification consists mainly of the definitions of attributes that can be placed on existing elements, along with a listener element that can also host the attributes. Each attribute maps to a feature of DOM Level 2 events. Table 1 lists all the XML Events attributes.
Table 1. XML Events attributes
| Attribute | Function |
| event | As shown in Listing 4, this required attribute names the event that triggers the listener. |
| observer | This attribute points to the unique ID of an element that is the observer. |
| handler | This attribute points to the URI of an element, possibly in a different document, that performs some action or processing. |
| phase | Either capture or default , this attribute specifies the capture phase to be used. |
| propagate | Either stop or continue (the default), this attribute specifies whether event propagation continues. |
| defaultAction | Either cancel or perform (the default), this attribute specifies whether the default action fires after propagation. |
| target | This attribute causes the listener to respond only to events directed at the specific target, and you should only use it in special situations. |
| id | This attribute allows a document-unique identifier to be given to the listener element. |
Convenient defaults
Listing 4 shows a convenient way to use XML Events with the minimum possible number of attributes: XML Events attributes are attached directly to the handler element. Whenever the ev:handler attribute is omitted, the element bearing the XML Events attributes is considered the handler. The observer is either specified through the ev:observer attribute, or the parent element. In Listing 4, the observer is xf:trigger.
Another kind of defaulting allows the XML Events attributes to be attached to the observer element when the ev:observer attribute is absent but the ev:handler attribute is present.
Listing 5 recreates the behavior in Listing 1, showing both defaulting techniques and using both script and a declarative action.
Listing 5. Two ways to use XML Events
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"The script element here is a handler, attached to an observer on bod_id (the body element). The xf:message element by itself doesn't do anything, but the observer element (body again) points back to it, registering the event handler.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xf="http://www.w3.org/2002/xforms">
<head>
<title>XML Events</title>
<script type="text/javascript" ev:event="load" ev:observer="bod_id">
alert("hello");
</script>
<xf:message level="modal" id="hndl_id">goodbye</xf:message>
</head>
<body id="bod_id" ev:event="unload" ev:handler="#hndl_id">
</body>
</html>
Tutorial Pages:
» Dynamic Documents with Less Script
» Two Kinds of Events
» How Events Work
» From Events to XML Events
» Conclusion
» Resources
First published by IBM DeveloperWorks
