- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
If you were involved in browser scripting in the 1990's, you may remember a great deal of aggravation when it came to making things work across browsers and across versions. Part of the problem was in detemrining how to get the browser to react to an event such as a mouse click.
So it just makes sense that some people (in this case, the W3C's XML Working Group) would take it upon themselves to develop a standard way to trigger events. The result is the XML Events specification.
Reading the XML Events specification without having seen it in action can be daunting, with its talk of listeners and observers and bubbling and so on, but it all comes down to this:
To specify an event, you need to know three things:
- What the event is (mouseclick, document load, etc.)
- Where it's going (the element actually clicked, the document loaded, etc.)
- What element takes action (the page to reload, etc.)
- What should happpen when the event takes place (the script to run, page to load, etc.)
Let's look at it this way: consider a simple HTML link:
<a href="index.html" id="theLink">This is <b>definitely</b> a link.</a>
As you know, when you click the text in this link, say, the
word "definitely", the browser goes to the address specified by the href
attribute. We can also look at it this way:
We have an event, the mouse click. It happens to a target, theb
element, and when it's seen by the observer, thea
element, a handler, the instruction to go to the new address, gets executed.
In the case of HTML, certain events are a given: unless you do something to change it, when you click a link the browser goes to a new URL. The purpose of XML Events is to genericise these associations so any type of element can correspond to any kind of event.
To do that, we can create a listener which associates the event, observer, target, and handler.
For example, we can duplicate our HTML link:
<ev:listener ev:event="activate" ev:observer="theLink" ev:handler="#gotonewURL" />
In this case, #gotonewURL
represents a script that
extracts the proper URL and sends the browser to it. (XML Events
doesn't specify how handlers are ultimately specified or found.)
Notice that I didn't specify the target. If I'd specified theLink
as the target, the user would have to click on the a
element for
the listener to activate. If the user clicked on the word "definitely", the
target would be the b
element, and not the a
element,
so it wouldn't work.
On the other hand, if I don't specify a target here, that means that any
element within the a
element can be the target, and the observer
will still see it. How is that possible?
XML Events have two phases: capture, and bubbling. It works like this: When an event happens to an element, it doesn't actually happen to that element immediately. Instead, it starts at the top level -- the document itself -- and works its way down the tree to the target (see, it does matter!) in the capture phase. After it reaches the target, it works its way back up the tree in the bubbling phase. During this process, any observer registered to see the event can act on it.
I could go on for pages about phases and propogation and determining observers and so on, but the important thing here is that you understand that you have a great deal of control over how events are perceived and targeted, so I encourage you to check out the resources for more information.
Before I go, though, let's see some examples, as implemented in the X-Smiles browser as part of its XForms implementation. Consider this listener:
<?xml version="1.0" encoding="ISO-8859-1"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms"> <head> <xforms:model> <xforms:instance xmlns="" id="instance1"> <my/> </xforms:instance> <xforms:message id="theMessage" level="ephemeral">Hi there!</xforms:message> <ev:listener observer="theTrigger" event="click" handler="#theMessage" /> </xforms:model> </head> <body> <xforms:trigger id="theTrigger"> <xforms:label>Click Me!</xforms:label> </xforms:trigger> </body> </html>
Let's take this one step at a time. First, we have a listener that
defines the theTrigger
element as the observer, looking for
a click event, and when it finds it, it should execute the theMessage
handler, popping up a message. When the user clicks the button, it "sees" the
event and executes the handler, popping up the message.
Now, that's not to say this is the only way we can set up an event.
In fact, we don't actually need the listener
element at
all. You could instead just place the event on the handler:
<?xml version="1.0" encoding="ISO-8859-1"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms"> <head> <xforms:model> <xforms:instance xmlns="" id="instance1"> <my/> </xforms:instance> <xforms:message id="theMessage" level="ephemeral" ev:observer="theTrigger" ev:event="click">Hi there!</xforms:message> </xforms:model> </head> <body> <xforms:trigger id="theTrigger"> <xforms:label>Click Me!</xforms:label> </xforms:trigger> </body> </html>
Or you can eliminate the middleman entirely and place the event within the observer:
<?xml version="1.0" encoding="ISO-8859-1"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms"> <head> <xforms:model> <xforms:instance xmlns="" id="instance1"> <my/> </xforms:instance> </xforms:model> </head> <body> <xforms:trigger id="theTrigger"> <xforms:label>Click Me!</xforms:label> <xforms:message level="ephemeral" ev:event="click">Hi there!</xforms:message> </xforms:trigger> </body> </html>
In this case, the parent of the element on which the event is specified
becomes the observer. We've specified the event on the message
element, so the trigger
is the observer, just as before.
OK, so that's the basics. As I said, there's a good deal more, if you need to delve into it, but that should get you started.