Views
Xml events
From PicoForms
Contents |
[edit] XML Events
XML Events is a W3C specification for handling events that occur in an XML document. It is a method of incorporating events in markup languages that have several advantages over the HTML onclick style of event handling.
XML Events are caused by a user interacting with XML document as a web page in a web browser on a PC, PDA or mobile.
Events offer life to XML content in a document making it capable for attaching more functionality to the host XML browser.
In technical terms, an XML event represents some asynchronous actions, such as a mouse click on a button, which associate themselves with any data element in an XML document.
XML Events are designed in such a way that they are not dependent on devices; this makes XML Events different from DOM Events usually associated with HTML documents.
XML Events are also extensively used in XForms.
[edit] Event
An event is an action which is initiated either by a user (such as clicking a mouse button or pressing a key on the key-board), a device such as the MIDI keyboard or the computer itself (such as running out of memory), and requires a pre-programmed response to generate the reaction.
Stated simply, when an event occurs the web page reacts.
[edit] Event Object
The event object provides information about an even that has occurred.
For example, on occurrence of a mouseover event, the event object provides information about the mouse button that has been clicked, the cursor coordinates, whether any modifier key like the "shift" key has been depressed, the element that was clicked, and so on.
[edit] Target
Every event has a target, the item that actually has been clicked on. It is the XML or HTML element which is most closely associated with the event.
The target is usually not the thing we are particularly interested in.
[edit] Event Handlers
An event handler is a piece of executable code or markup responding to a particular event.
It is a method or function which contains program statements executed in response to an event. An event handler processes the actions that occur, such as key depressions and mouse clicks.
Event handlers make the web content dynamic. For example JavaScript is a common method used for scripting event handlers for web content.
[edit] Default Action
Some events, however, are distinct from any handler and cause actions known as default action. For example, clicking a hyperlink causes a default action.
[edit] Writing XML Events
XML Events uses exactly the same event mechanisms that are used in HTML, however, they are written differently.
Consider the example:
<input type="update" onclick="validate(); return true;">
This says that if the “click” event occurs to the <input> element, then the code in the “onclick” attribute is performed.
The XML equivalent of the above example is:
<input type="submit">
<script ev:event="DOMActivate" type="text/javascript">
validate();
</script>
</input>
[edit] XML Events with DOM 2
A major traditional technique for processing XML files is using using a programming language and the DOM Application Programming Interface. With XML Events the XML language is capable of uniformly integrating event listeners and associated event handlers with Document Object Model (DOM) Level 2 event interfaces.
[edit] DOM
Dom (Document Object Model) is a platform and an Application Programming Interface (API) that provides programs and scripts the ability to dynamically access and update the content, structure, and style of a document.
DOM is suitable for applications where a file is accessed repeatedly or out of sequence order. This is because of the reason that navigation in any direction (e.g., parent and previous sibling) and arbitrary modifications are supported by DOM, making it necessary to have an implementation that at least buffer the document read so far.
In DOM implementation the document must completely be loaded into memory and constructed as a tree of objects before giving access to it; therefore lot of memory space is required for DOM implementation.
[edit] DOM Levels
The W3C has divided Dom into various levels:
DOM 1
DOM 1 or the DOM Level 1 deals with HTML and XML document models. DOM 1 contains the functionality for document navigation and manipulation.
DOM 2
DOM 2 or the DOM Level 2 defines the functionality that manipulates the style information attached to a document. With DOM 2 a style sheet object model is attached to DOM 1.
An event model is also defined by DOM 2 and support for XML namespaces are also provided by it.
DOM 2 Events
With DOM 2 Events an Application Programming Interface (API) is specified for accessing document events.
[edit] XML DOM
As the term suggests, XML DOM is the Document Object Model for XML.
XML DOM defines a standard set of objects for XML documents, that is, a standard way for accessing and manipulating XML documents is defined by XML DOM.
It is independent of any platform or language.
An XML document is presented by the DOM as a tree structure, known as a node tree. All the elements, and the text and attributes contained in the elements are defined as nodes and are accessed through the DOM tree or the node tree. The contents of the existing elements can be modified or deleted and new elements can also be created.
[edit] Event Flow
Event flow is the process of commencement of an event from the DOM implementation and its passage into the DOM. The event can be handled in several ways with the methods of event capture and event bubbling and with different techniques of event listener registration.
The DOM presents the XML document as a tree structure.
An event occurs at a given point – the target node – of the node tree. Once an event has occurred, it is dispatched by passing it down the document tree or the node tree. The dispatch of the event starts at the root and it is passed to the element which is the target node of that event. The passage of the event from the root to the target node is called the capture phase.
After the event reaches the target, it may again be passed up the node tree in the phase called bubbling until it reaches back to the root.
An event can be responded to at any observer – the nodes or elements that come either in the capture phase or the bubbling phase of the event flow. Any node in the passage of the event, including the root node and the target, can be an observer.
An action is said to have been performed if an event is responded to.
Any specification for an action is called a handler. A handler may be attached to an observer in either of the two phases. A listner is a binding for an event handler that is intended to target some element in the document. A handler can listen only for one phase; and to listen for both the phases two handlers must be attached.
After the desired event is received, observers provide event handlers to be trigerred, which in turn, respond to the event by performing the behavior specified to them. Event handlers can also influence the event in its further processing, such as, making the event propagation to stop.
[edit] Event Listener
[edit] EventListner Interface
The EventListner interface is the primary method that handles events. The EventListener interface is implemented and listener is registered on an EventTarget with the help of the using the AddEventListener method. Once the use of the listener is over the EventListener must be removed from the EventTarget.
[edit] Listener Element
The listener element supports a subset of the EventListener interface. The event listeners are declared with the listener element and also registered with in the DOM with specific nodes. The listener element has some attributes:
event
The event attribute is used for specifying the event type that has caused the listener to register. This attribute has an XML Name as its value.
observer
The observer attribute is used for specifying the the id of the element the event listener will be registered with. This attribute is optional and in its absence the element that has the event attribute over itself or its parent acts as the observer.
target
The target attribute is used for specifying the target element id of the event. When the target attribute is present, the event handler processes only the events matching both the event and target attributes. Following the propagation of events, a desendent node of the observer element or the observer element itself must be the target element.
The target attribute must be used with great care.
Suppose we want to specify:
<listener event="click" observer="title"
target="link1" handler="#clicker"/>
where we have the ‘title’ as some ancestor of the node:
<a id="link1" href="doc.html">The <em>first</em> text</a>
If the user requires to click on the word “first”, then the <em> element will be the target and not the <a> element, therefore the handler will not be activated.
The correct way of catching all the mouse clicks on the <a> element and its children must be:
<listener event="click" observer="link1" handler="#clicker"/>
Here we see that there is no target attribute in the code.
handler
The handler attribute is used to specify the Uniform Resource Identifier (URI) reference of a resource defining the action to be performed when event comes to the observer. This attribute is optional and in the absence of this attribute the element with the event attribute over it acts as the handler.
phase
The phase attribute is used for specifying the phase or the time during the event propagation when the listener will be activated by the desired event.
For example:
phase=”capture”
The listener is activated during the capturing phase of the event
phase=”default”
This is the behavious where the listner is acvated during the bubbling or the target phase.
Since all events do not have the bubbling phase, those without this phase can only be handled with phase=”default” if the target element of the event is made the observer.
propagate
The propagate attribute is used for specifying the continuation or termination of the event propagation. It is an optional attribute that specifies whether an event, after all its listeners have been processed at the current node, should be allowed to continue on its path or not, the path being either the capture or the bubbling phase.
For example:
propagate=”stop”
With this attribute the event propagation is stopped
propagate=”continue”
With this attribute the propagation of the event continues if such propagation is not stopped by some other means, such as using another listener or a scripting.
The default behaviour of the propagate attribute is propagate="continue".
defaultAction
The defaultAction attribute is used for specifying the perform tast of the default action. It is also an optional attribute that specifies whether the default action for an event is to be performed after all its listeners have been processed.
For example:
defaultAction=”cancel”
With this attribute the default action is cancelled if the event type is cancelable
defaultAction=”perform”
With this attribute the default action of the event is performed if the default action is not stopped by some other means, such as using another listener or a scripting.
The default behaviour of the defaultAction attribute is defaultAction="perform".
All enents are not cancelable, and for those which are not will have their defaultAction attribute ignored.
id
The id attribute is the unique identifier to elements in XML documents. The values of this optional attribute is used for manipulating element through a DOM interface.
The attributes are arranged in the following table:
| Attribute | Function | |
|---|---|---|
| event | It specifies the event type that triggers the listener. | |
| observer | It specifies the id of element the event listener will be registered with. | |
| target | It specifies the target element id; for this attribute the listener responds only to events directed at the specific target. | |
| handler | It specifies the URI of an element that defines the action to be performed. | |
| phase | It specifies the phase (capture or default) when the listener will be activated | |
| propagate | It specifies whether an event should be allowed to continue on its path (capture or bubbling phase). | |
| defaultAction | It specifies whether the default action is trigerred after all its listeners have been processed. | |
| id | It is a document-unique identifier given to the listener element whose value can manipulate the element through a DOM interface. |
Table 1: XML Events attributes and their respective functions
