XML Events is a W3C specification for handling events occurring in an XML document. These events are typically caused by a user interacting with the document as a web page using a device such as a web browser on a PC, mobile phone or telephone.
Formal Definition Edit
Technically an XML event is the representation of some asynchronous occurrence (such as a mouse click on a button) that gets associated with any data element in an XML document. XML Events provides a static, syntactic binding to the DOM Events interface, allowing the event to be handled.
The XML Events standard is defined to provide XML-based languages with the ability to uniformly integrate event listeners and associated event handlers with Document Object Model (DOM) Level 2 event interfaces. The result is to provide a declarative, interoperable way of associating behaviors with XML-based documents such as XHTML.
Advantages of XML Events Edit
XML Events uses a separation of concerns design pattern, and is technology neutral with regards to handlers. It gives authors freedom in organising their code, and allows separation of document content from scripting.
Both legacy HTML and early SVG versions bind events to a presentation element by encoding the event name in an attribute name, such that the value of the attribute is the action for that event at that element. For example (with the onclick attribute):
<p>Stay <a href="http://www.example.com" onclick="window.alert('Hello!'); return false;">here</a>!</p>
This design has three drawbacks:
- it hard-wires the events into the language, so that to add a new event type, you have to make a change to the language
- it forces software developers to mix the content of the document with the specifications of the scripting and event handling, rather than allowing you to separate them out
- it restricts you to a single scripting language per document.
Relationship to Other StandardsEdit
Unlike DOM Events which are usually associated with HTML documents, XML events are designed to be independent of devices. XML Events are also used extensively in XForms, and in version 1.2 of the SVG specification, as of July 2006 still a working draft.
Example of XML Events using Listener in XFormsEdit
The following is an example of how XML events are used in the XForms specification:
<html xmlns="http://www.w3.org/2002/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xf="http://www.w3.org/2002/xforms"> <head> ... <ev:listener event="DOMActivate" observer="myButton" handler="#doit"/> </head>
- W3C XML Events Specification was a W3C Recommendation on 14 October 2003
- W3C XML Events for HTML Authors tutorial