XRX/Detecting Duplicates

From Wikibooks, open books for an open world
< XRX
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'
return
if (not($term-name)) then
   <error>
      <message>Error: term-name argument required</message>
   </error>
else
<result>{
  if (collection($collection)/Term[TermName/text()=$term-name or Abbreviation/text()=$term-name])
    then <true/>
    else <false/>
}</result>

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

$BASENAME/xqueries/term-exists.xq?term-name=Product

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

<result>
   <true/>
</result>

XForms Application[edit | edit source]

<html
   xmlns="http://www.w3.org/1999/xhtml"
   xmlns:xf="http://www.w3.org/2002/xforms"
   xmlns:ev="http://www.w3.org/2001/xml-events" >
   <head>
       <title>Duplicate Detection Example</title>
       <xf:model>
           <xf:instance xmlns="" id="term">
               <data>
                   <term-name>Product</term-name>
                </data>
            </xf:instance>
            <xf:submission id="check-for-duplicate" method="get" 
                    action="term-exists.xq" 
                    instance="term" replace="all"/>
        </xf:model>
     </head>
     <body>
          <h1>Check Term</h1>
          <xf:input ref="term-name">
               <xf:label>Term: </xf:label>
          </xf:input>
          <xf:submit submission="check-for-duplicate">
               <xf:label>Check for Dups</xf:label>
          </xf:submit>
     </body>
</html>

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"/>
   </xf:action>
</xf:input>

Back: FAQ Manager Next: Data Element Editor