XQuery/Incremental Search of the Chemical Elements

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Introduction[edit | edit source]

In this example of AJAX in its AHAH form, an incremental search of the chemical elements is implemented. This is also an example of the use of matching with a regular expression.

The raw data is taken from a file [1]provided by Elliotte Rusty Harold.

Execution[edit | edit source]

Search the Elements

The main page[edit | edit source]

The main page is a simple HTML file. The div element with id list is where the generated contents will be placed. The JavaScript function getList() is called when any of several interface events occur.

declare option exist:serialize "method=xhtml media-type=text/html
        doctype-public=-//W3C//DTD HTML 4.01 Transitional//EN

<html xmlns="http://www.w3.org/1999/xhtml" >
     <title>Chemical Elements</title>
      <script language="javascript" src="ajaxelement.js"/>
      <style type="text/css">
       td {{background-color: #efe; font-size:14px;}}
       th {{background-color: #ded; text-align: right; font-variant:small-caps;padding:3px; font-size:12px;}}
    <h1>Chemical Elements</h1>
    <table class="page">
    <td valign="top" width="30%"><form onSubmit="getList(); return false">
      <span><label for="name">Element Name </label> 
                     <input type="text" size="5" name="name" id="name" title="e.g. Silver"
                                   onkeyup="getList();" onfocus="getList();" />
    <td id="list"/>

The JavaScript[edit | edit source]

The JavaScript implements the simple functionality of calling the server-side script getElement.xq with the string entered in the search box, and in the callback, pasting the returned XHTML into the div. [ This JavaScript uses no supporting libraries although it would be simplified by using e.g. jQuery ]

function updateList() {
  if (http.readyState == 4) {
      var divlist = document.getElementById('list');
      divlist.innerHTML = http.responseText;
      isWorking = false;

function getList() {
  if (!isWorking && http) {
    var name = document.getElementById("name").value;
    http.open("GET", "getElement.xq?name=" + name);
    http.onreadystatechange = updateList;  
          // this sets the call-back function to be invoked when a response from the HTTP request is returned
    isWorking = true;

function getHTTPObject() {
  var xmlhttp;
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
  return xmlhttp;

var http = getHTTPObject(); //  create the HTTP Object
var isWorking = false;

The server-side search[edit | edit source]

This script is called by the getlist() function when a partial atom name has been entered. The string is converted to a simple regular expression and used in the eXist free text matching function to retrieve matching atoms. The response depends on the number of matches found:

  • if only one match, return a table of details
  • if more than one match, return with a list of matches
  • if none return "no matches"

A function turns the ATOM node into a table,mapping each child node to a row with the node name as the legend.

declare function local:atom-to-table($element ) {
   <table class="element">
      {for $node in $element/*
      let $label := replace(name($node),"_"," ") 
                   { $node/text()   }
let $name := request:get-parameter("name",())
  if ($name != "")
      let $search := concat('^',$name)  (: anchor the term to the start of the string :)
      let $elements := doc("periodicTable.xml")/PERIODIC_TABLE
      let $matches := $elements/ATOM[matches(NAME,$search,"i")]
         if (count($matches) = 0)
            <span>No matches</span>
         if (count($matches) =1)
         (: multiple matches :)
             <table class="list">
                <th>Atomic Weight</th>
               {for $match in $matches
                order by $match/NAME
                    <td>{string($match/ATOMIC_WEIGHT)} </td>
  else ()

To do[edit | edit source]

Some naming problems here - needs tidying. Units need to be included