XRX/Detecting Duplicates

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

Motivation[edit | edit source]

You want to detect duplicate terms as the user types within a field of a form.

Method[edit | edit source]

We will create a form that sends a request to the server each time a letter is typed into a field. The form will call a ReST web service and pass the current term as a parameter.

Sample XQuery[edit | edit source]

The following query can be used on the server to detect duplicates.

xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare option exist:serialize "method=xml media-type=text/xml indent=yes";
let $term-name := request:get-parameter('term-name', '')
let $collection := '/db/terms'
if (not($term-name)) then
      <message>Error: term-name argument required</message>
  if (collection($collection)/Term[TermName/text()=$term-name or Abbreviation/text()=$term-name])
    then <true/>
    else <false/>

This query takes a single parameter and returns a true or false element.


If the term exists in the database it will return the following:


XForms Application[edit | edit source]

   xmlns:ev="http://www.w3.org/2001/xml-events" >
       <title>Duplicate Detection Example</title>
           <xf:instance xmlns="" id="term">
            <xf:submission id="check-for-duplicate" method="get" 
                    instance="term" replace="all"/>
          <h1>Check Term</h1>
          <xf:input ref="term-name">
               <xf:label>Term: </xf:label>
          <xf:submit submission="check-for-duplicate">
               <xf:label>Check for Dups</xf:label>

Now we have a simple XForms that calls a web service and returns a true/false record. Our next step is to make this test occur in the background as the user types and display a warning message as a duplicate has been detected.

Adding Character-by-Character Testing[edit | edit source]

Now we have two tasks. We need to fire off the event to the server as the user types and we need to bring the information back without interrupting the user.

First we want to add an event to the input field that sends a message each time a character is typed into the input field:

<xf:input ref="term-name" incremental="true">
   <xf:label >Term: </xf:label>
   <xf:action ev:event="xforms-value-changed">
      <xf:send submission="check-for-duplicate"/>

Back: FAQ Manager Next: Data Element Editor