XQuery/Incremental Search of the Chemical Elements

Introduction
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 [/ajax/periodicTable.xml ]provided by Elliotte Rusty Harold.

Execution
[/ajax/periodicTable.xq Search the Elements]

The main page
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.

The JavaScript
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 ]

The server-side search
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:

A function turns the ATOM node into a table,mapping each child node to a row with the node name as the legend.
 * 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"

}; let $name := request:get-parameter("name",) return if ($name != "") then 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")] return if (count($matches) = 0) then No matches else if (count($matches) =1) then local:atom-to-table($matches) else (: multiple matches :) else

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