Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Introduction to XML Events

By Micah Dubinko
2005-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
AttributeFunction
eventAs shown in Listing 4, this required attribute names the event that triggers the listener.
observerThis attribute points to the unique ID of an element that is the observer.
handlerThis attribute points to the URI of an element, possibly in a different document, that performs some action or processing.
phaseEither capture or default , this attribute specifies the capture phase to be used.
propagateEither stop or continue (the default), this attribute specifies whether event propagation continues.
defaultActionEither cancel or perform (the default), this attribute specifies whether the default action fires after propagation.
targetThis attribute causes the listener to respond only to events directed at the specific target, and you should only use it in special situations.
idThis 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"

"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>
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.

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


 | Bookmark
Related Tutorials:
» Starting with XML
» Performing Client-Side XSL Transformations
» Create a Google Sitemap for your Web Site
» XML and Scripting Languages
» Parsing Comma-Separated Values
» XML Security Suite: Increasing the Security of E-Business

Ask A Question
characters left.