JavaScript Editor Free JavaScript Editor     JavaScript Debugger 

Main Page

Previous Page
Next Page

16.1. Sarissa

Although it's officially only an open source cross-browser JavaScript XML library, Sarissa is one of those libraries whose capabilities extend far beyond the basic XML support that I expected. This is a rare occurrence in today's world, where we can all remember being disappointed by movies, jobs, and most members of Congress. Sarissa wraps the browser's native XML application programming interfaces with common interfaces. This makes life much easier for the client-side developer than it would otherwise be.

Unlike my home-grown library, which supports only Microsoft Internet Explorer and Gecko-based browsers such as Firefox, Flock, Mozilla, and Netscape, Sarissa supports a wide range of browsers on multiple platforms. This serves as a really good example of what a number of dedicated developers can accomplish when they put their minds to it, as opposed to the lone mad scientist or even the bloated corporation. Sarissa supports, at least partially, the following web browsers:

  • Firefox

  • Konqueror (KDE 3.3)

  • Microsoft Internet Explorer (MSXML 3.0)

  • Mozilla

  • Opera

  • Safari

That's quite an impressive list of web browsers; I don't even have a machine capable of running Safari. I normally just press my nose to the window of the Apple Store and wish. Come to think of it, I usually do that with most stores that sell computers, including online ones. Well, at least now my wife knows how the monitor on her computer got the nose prints on it and who the nose prints belong to.

16.1.1. A Brief Overview of Sarissa

Table 16-1 briefly examines the goodies available in Sarissa, which read like a Who's Who of Ajax features.

Table 16-1. Sarissa Features



DOM Document Object (create)

Creates a new instance of an XML DOM document

DOM Document Object (load)

Loads an XML DOM document from either a remote source, such as the server, or a string either synchronously or asynchronously


Parses an XML DOM document for errors


Serializes an XML DOM document to a text string


Communicates with the web server via the XMLHttpRequest object


Provides the capability to apply an XPath statement with JavaScript

The overall syntax for Sarissa is both logical and consistent. By logical, I mean that if a particular parameter is necessary for a certain object, it is there. The consistency that I'm referring to is the capability to write a script once and be able to run it on any of the supported web browsers, without having to monkey around with the code too much. What a concept!

To see what I mean, let's take a look at how to create an XML DOM document using Sarissa:

var myXMLDocument = Sarissa.getDomDocument();

Relatively simple and painless, isn't it?

Loading the XML document from a remote source is only slightly more complex, unless you're indecisive, in which case you've got real problems in deciding between synchronous and asynchronous. Never mind, I'll go out on a limb and show how it is done synchronously in Listing 16-1 and asynchronously in Listing 16-2.

Listing 16-1. Loading Synchronously

var myXMLDocument = Sarissa.getDomDocument();

myXMLDocument.async = false;

Listing 16-2. Loading Asynchronously

var myXMLDocument = Sarissa.getDomDocument();

myXMLDocument.async = true;
myXMLDocument.onreadystatechange = readyStateHandler;

function readyStateHandler() {
  if(myXMLDocument.readyState == 4)

But what if the XML isn't remote? Say, for example, that it is already on the page in a JavaScript string. In that case, Listing 16-3 is the example for you.

Listing 16-3. Loading an XML Document Already on the Page

var myXMLDocument = Sarissa.getDomDocument();
var myDOMParser = new DOMParser();
var myXMLString = '<xyzzy>plugh</xyzzy>';

myXMLDocument = myDOMParser.parseFromString(myXMLString,'text/xml');

Alright, now through one means or another, we have an XML document loaded. This leaves only the question of what to do with it. That's a minor detail; it isn't like it's leftover Thanksgiving turkey or anything like that. We are not going to run out of ideas. Nobody has ever considered making XML enchiladas or XML stroganoff. XML gives us two possible options; we can either transform it or send it somewhere.

We start with the option to transform it because I consider myself something of an XSLT geek, especially when performing dangerous acts such as client-side transformations. I'm always up for playing with anything that could possibly make my job easier, and it doesn't get much easier than this. There are only a couple simple rules to remember when using XSLT with Sarissa: The XML is an XML document, and the XSL style sheet is also an XML document. That's all there is to it, and Listing 16-4 presents an example.

Listing 16-4. XSLT with Sarissa

var myXMLDocument = Sarissa.getDomDocument();
var myXSLDocument = Sarissa.getDomDocument();
var myXSLTProcessor = new XSLTProcessor();
var myXMLTransformed;

// Synchronous load of XML document

myXMLDocument.async = false;

// Synchronous load of XSL stylesheet
myXSLDocument.async = false;

// Import stylesheet

// Add a parameter 'take' value 'formula'
myXSLTProcessor.setParameter(null, 'take', 'formula');

// Transform, result in myXMLTransformed
myXMLTransformed = myXSLTProcessor.transformToDocument(myXMLDocument);

With XSLT out of the way, this leaves Sarissa's implementation of the XMLHttpRequest object as the last piece that I cover here. This implementation of the XMLHttpRequest object offers no surprises, unless you've jumped ahead to this chapter just to read about Sarissa. If this is the case, allow me to explain that XMLHttpRequest is available in two distinct flavors: synchronous and asynchronous. Synchronous is the one that waits quietly in line for its response, and asynchronous is the one that does other things and expects a callback with periodic updates. Listing 16-5 shows an example of a synchronous request, and Listing 16-6 shows an asynchronous request.

Listing 16-5. Synchronous Request

var myXMLHttpRequest = new XMLHttpRequest();'GET','manticore.xml',false);

Listing 16-6. Asynchronous Request

var myXMLHttpRequest = new XMLHttpRequest();'GET','ELP.xml',true);

myXMLHttpRequest.onreadystatechange = function() {
  if(myXMLHHttpRequest.readyState == 4)


If you're interested in using Sarissa for an Ajax application or any web application of your own, I heartily recommend it. The source code for Sarissa is available for download from, whose URL is, coincidentally, If you're unfamiliar with, I recommend that you put aside an afternoon, and about 30 blank CDs, and peruse their selections of open source goodies. In addition to a vast array of software, there is, amazingly enough, documentation to go along with the software. It, like Sarissa, is well worth the time.

Previous Page
Next Page

JavaScript Editor Free JavaScript Editor     JavaScript Debugger