XML Events (original) (raw)
An Events Syntax for XML
W3C Recommendation 14 October 2003
This version:
http://www.w3.org/TR/2003/REC-xml-events-20031014
Latest version:
http://www.w3.org/TR/xml-events
Previous version:
http://www.w3.org/TR/2003/PR-xml-events-20030804
Diff-marked version:
<xml-events-diff.html>
Editors:
Shane McCarron, Applied Testing and Technology, Inc.
Steven Pemberton, CWI/W3C®
T. V. Raman, IBM Corporation
Please refer to the errata for this document, which may include some normative corrections.
This document is also available in these non-normative formats: PostScript version, PDF version, ZIP archive, and Gzip'd TAR archive.
The English version of this specification is the only normative version. Non-normative translations may also be available.
Copyright © 2003 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark, document use and software licensing rules apply.
Abstract
The XML Events module defined in this specification provides XML languages with the ability to uniformly integrate event listeners and associated event handlers with Document Object Model (DOM) Level 2 event interfaces [DOM2EVENTS]. The result is to provide an interoperable way of associating behaviors with document-level markup.
Status of This Document
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document is a Recommendation of the W3C. It has been reviewed by W3C Members and other interested parties, and has been endorsed by the Director as a W3C Recommendation. It is a stable document and may be used as reference material or cited as a normative reference from another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web. A test suite for XML Events has been developed as part of a public XForms 1.0 Test Suite, along with an implementation report.
This document has been produced by the W3C HTML Working Group (Members only) as part of the HTML Activity. The goals of the HTML Working Group are discussed in the HTML Working Group charter. Patent disclosures relevant to this specification can be found on the Working Group's patent disclosure page.
Please report errors in this specification to www-html-editor@w3.org (archive). It is inappropriate to send discussion email to this address. Public discussion may take place on www-html@w3.org (archive).
Contents
- 1. Introduction
- 2. Conformance Requirements
- 2.1. Document Conformance
- 2.2. Host Language Conformance
- 2.3. User Agent Conformance
- 3. The XML Events Module
- 3.1. The listener Element
* 3.1.1. Examples of listener Usage - 3.2. Attaching Attributes Directly to the Observer Element
* 3.2.1. Examples of Using Attributes Attached to an Observer Element - 3.3. Attaching Attributes Directly to the Handler Element
* 3.3.1. Examples of Using Attributes Attached to a Handler Element - 3.4. Summary of Observer and Handler Attribute Defaulting
- 3.5. Event Handlers
- 3.6. The Basic XML Events Profile
- 3.1. The listener Element
- 4. Naming Event Types
- A. DTD Implementation
- A.1. Qualified Names Module
- A.2. XML Events Module
- B. Schema Implementation
- B.1. Attributes Module
- B.2. XML Events Module
- C. References
- C.1. Normative References
- C.2. Other References
- D. Acknowledgments
1.Introduction
This section is informative.
An event is the representation of some asynchronous occurrence (such as a mouse click on the presentation of the element, or an arithmetical error in the value of an attribute of the element, or any of unthinkably many other possibilities) that gets associated with an element (targeted at it) in an XML document.
In the DOM model of events [DOM2EVENTS], the general behavior is that when an event occurs it is dispatched by passing it down the document tree in a phase called capture to the element where the event occurred (called its target), where it then may be passed back up the tree again in the phase called bubbling. In general an event can be responded to at any element in the path (an observer) in either phase by causing an action, and/or by stopping the event, and/or by cancelling the default action for the event. The following diagram illustrates this:
Event flow in DOM2: an event targeted at an element (marked 'target') in the tree passes down the tree from the root to the target in the phase called 'capture'. If the event type allows it, the event then travels back up the tree by the same route in a phase called 'bubbling'. Any node in the route, including the root node and the target, may be an 'observer': that is to say, a handler may be attached to it that is activated when the event passes through in either phase. A handler can only listen for one phase. To listen for both you have to attach two handlers.
An action is some way of responding to an event; a handler is some specification for such an action, for instance using scripting or some other method. A listener is a binding of such a handler to an event targeting some element in a document.
HTML [HTML4] binds events to an 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. This method has two main disadvantages: firstly it hardwires the events into the language, so that to add a new event, you have to make a change to the language, and secondly it forces you to mix the content of the document with the specifications of the scripting and event handling, rather than allowing you to separate them out. SVG [SVG] uses a similar method.
The process of defining a new version of HTML identified the need for an extensible event specification method. The design requirements were the following:
- Syntactically expose the DOM event model to an XML document [XML].
- Provide for new event types without requiring modification to the DOM or the DTD.
- Allow for integration with other XML languages.
The DOM specifies an event model that provides the following features:
- A generic event system,
- Means for registering event listeners and handlers,
- Means for routing events through a tree structure,
- Access to context information for each event, and
- A definition of event flow, as sketched above.
Element listener
and its attributes defined in this specification are the method of binding a DOM level 2 event at an element to an event handler. They encapsulate various aspects of the DOM level 2 event interface, thereby providing markup-level specification of the actions to be taken during the various phases of event propagation.
This document neither specifies particular events, nor mandates any particular methods of specifying actions. These definitions are left to any markup language using the facilities described here.
2.Conformance Requirements
This section is normative.
The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119].
2.1.Document Conformance
XML Events is not a stand-alone document type. It is intended to be integrated into other host languages such as XHTML. A conforming XML Events document is a document that requires only the facilities described as mandatory in this specification and the facilities described as mandatory in its host language. Such a document must meet all the following criteria:
- The document must conform to the constraints expressed in Appendix B - Schema Implementation or Appendix A - DTD Implementation, combined with the constraints expressed in its host language implementation.
- The document must contain an
xmlns
declaration for the XML Events namespace [XMLNAMES]. The namespace for XML Events is defined to behttp://www.w3.org/2001/xml-events
. An example start tag of a root element might look like:
2.2.Host Language Conformance
When XML Events are included in a host language, all of the facilities required in this specification must be included in the host language. In addition, the elements and attributes defined in this specification must be included in the content model of the host language.
2.3.User Agent Conformance
A conforming user agent must support all of the features required in this specification.
3.The XML Events Module
This section is normative.
This specification defines a module called XML Events. The XML Events module uses the XML namespace [XMLNAMES] identifier http://www.w3.org/2001/xml-events
.
Examples in this document that use the namespace prefix "ev
" all assume an xmlns
declaration xmlns:ev="http://www.w3.org/2001/xml-events"
somewhere suitable in the document involved. All examples are informative.
The remainder of this section describes the elements and attributes in this module, the semantics, and provides an abstract module definition as required in [XHTMLMOD].
The XML Events Module supports the following element and attributes:
Element | Attributes | Minimal Content Model | ||
---|---|---|---|---|
listener | event (NMTOKEN), observer (IDREF), target (IDREF), handler (URI), phase ("capture" | "default"*), propagate ("stop" | "continue"*), defaultAction ("cancel" | "perform"*), id (ID) | EMPTY |
Implementations: DTD, XML Schema
3.1.The listener Element
Element listener
supports a subset of the DOM's EventListener
interface. It is used to declare event listeners and register them with specific nodes in the DOM, and has the following attributes:
event
The required event
attribute specifies the event type for which the listener is being registered. As specified by [DOM2EVENTS], the value of the attribute should be an XML Name [XML].
observer
The optional observer
attribute specifies the id
of the element with which the event listener is to be registered. If this attribute is not present, the observer is the element that the event
attribute is on (see later under "Attaching Attributes Directly to the Observer Element"), or the parent of that element (see later under "Attaching Attributes Directly to the Handler Element").
target
The optional target
attribute specifies the id
of the target element of the event (i.e., the node that caused the event). If this attribute is present, only events that match both the event
and target
attributes will be processed by the associated event handler. Clearly because of the way events propagate, the target element should be a descendent node of the observer element, or the observer element itself.
Use of this attribute requires care; for instance, if you specify
where 'para1' is some ancestor of the following node
and the user happens to click on the word "draft", the <em>
element, and not the <a>
, will be the target, and so the handler will not be activated; to catch all mouse clicks on the <a>
element and its children, use observer="link1"
, and no target
attribute.
handler
The optional handler
attribute specifies the URI reference of a resource that defines the action that should be performed if the event reaches the observer. (This specification does not mandate what form that element should take: see further the section "Event Handlers"). If this attribute is not present, the handler is the element that theevent
attribute is on (see later under "Attaching Attributes Directly to the Handler Element").
phase
The optional phase
attribute specifies when (during which DOM 2 event propagation phase) the listener will be activated by the desired event.
capture
Listener is activated during capturing phase.
default
Listener is activated during bubbling or target phase.
The default behavior is phase="default"
.
Note that not all events bubble, in which case with phase="default" you can only handle the event by making the event's target the observer.
propagate
The optional propagate
attribute specifies whether after processing all listeners at the current node, the event is allowed to continue on its path (either in the capture or the bubble phase).
stop
event propagation stops
continue
event propagation continues (unless stopped by other means, such as scripting, or by another listener).
The default behavior is propagate="continue"
.
defaultAction
The optional defaultAction
attribute specifies whether after processing of all listeners for the event, the default action for the event (if any) should be performed or not. For instance, in XHTML the default action for a mouse click on an <a>
element or one of its descendents is to traverse the link.
cancel
if the event type is cancelable, the default action is cancelled
perform
the default action is performed (unless cancelled by other means, such as scripting, or by another listener).
The default value is defaultAction="perform"
.
Note that not all events are cancelable, in which case this attribute is ignored.
id
The optional id
attribute is a document-unique identifier. The value of this identifier is often used to manipulate the element through a DOM interface.
Note that observer = "<element-id>"
and event = "<event-type>"
are similar to the begin = "<element-id>.<event-type>"
attribute in SMIL EventTiming [SMIL20].
3.1.1.Examples of listener Usage
- This example attaches the handler in the element at
"#doit"
that will get activated when the event calledactivate
occurs on the element withid="button1"
, or any of its children. The activation will occur during bubbling, or if the event happened on the observer element itself, when the event reaches the element (phase target). - This attaches the handler at
#overflow-handler
that will get activated when the eventoverflow
occurs on the element withid="expr1"
and bubbles up to the element withid="prog1"
. - This attaches the handler at
#popup
that will get activated whenever anactivate
event occurs at the element withid="embargo"
or any of its children. Since it will be activated during the capture phase, and propagation is stopped, this will have the effect (regardless of what the handler does) of preventing any child elements of theembargo
element seeing anyactivate
events. - This attaches a handler from another document.
3.2.Attaching Attributes Directly to the Observer Element
All the attributes from the listener
element with the exception of id
may be used as global attributes, as defined in Namespaces in XML [XMLNAMES], to attach the attributes to other elements.
Note that this means that the <listener>
element is strictly speaking redundant, since the following
would have the same effect as
<ev:listener event="click" observer="button1" handler="#clicker"/>
Nonetheless, for utility the <listener>
element has been retained.
If the observer
attribute is omitted (but not the handler
attribute), then the element that the other attributes are attached to is the observer element.
3.2.1. Examples of Using Attributes Attached to an Observer Element
- This first example will attach the handler identified by
"#popper"
to the<a>
element, and cancel the default action for the event.
The document - This will attach the handler at
#handle-overflow
for the eventoverflow
to the current element....
3.3.Attaching Attributes Directly to the Handler Element
If, when attaching the global attributes to an element, the handler
attribute is omitted then the element that the other attributes are attached to is the handler element.
Note that, since the observer
and target
attributes are IDREFs, in this case the handler and observer/target elements must be in the same document (while in other cases, since the handler
attribute is a URI, the handler element may be in another document).
If the observer
attribute is also omitted, then the parent of the handler element is the observer element.
3.3.1. Examples of Using Attributes Attached to a Handler Element
- In this case the element is the handler for the
submit
event on the element withid="form1"
. - In this case the
<action>
element is the handler for eventq-submit
, and the observer is thequestionnaire
element. ... ... - The
<script>
element is the handler for eventclick
; the<img>
element is the observer. - The
<onevent>
element is the handler for evententerforward
. The<card>
element is the observer.Hello!
- The
<catch>
element is the handler for thenomatch
event. The observer is the<field>
element. What is the code word? rutabaga It is the name of an obscure vegetable. Security violation! - This example shows three handlers for different events. The observer for all three is the
<secret>
element.Please enter your password Mail help@example.com in case of problems A pet's name This field is required
3.4.Summary of Observer and Handler Attribute Defaulting
The following table summarizes which elements play the role of observer or handler if the relevant attribute is omitted.
The effect of omitted observer and handler attributes
Handler present | Handler omitted | |
---|---|---|
Observer present | (As declared) | Element is handler |
Observer omitted | Element is observer | Element is handler Parent is observer |
3.5.Event Handlers
This specification does not require an XML application that uses XML Events to use any particular method for specifying handlers. However, the examples, particularly those in the section on attaching the attributes directly to the handler, are intended to give examples of how they could be specified.
It is however recognized that two methods are likely to occur often: scripting (such as XHTML's