XForms/Replace Tester


You want to build a form that will allow you quickly test replacement regular expressions using XQuery's replace function.

Screen ImageEdit

Replace Test Tool Before Submit
Replace Test Tool After Submit

Link to Working ApplicationEdit

Replace Test

Sample ProgramEdit

  xmlns:ev="http://www.w3.org/2001/xml-events" >
        <style type="text/css">
        @namespace xf url("http://www.w3.org/2002/xforms");
        body {font-family: Ariel, Helvetica, san-serif}

/* Input controls appear on their own lines. */
xf|input, xf|select, xf|select1, xf|textarea 
{display:block; margin:5px 0;}

/* Makes the labels right aligned in a 150px wide column that floats to the left of the input controls. */
xf|input > xf|label, xf|select > xf|label, xf|select1 > xf|label, xf|textarea > xf|label, xf|output > xf|label 
{font-weight: bold;text-align:right; padding-right:10px; width:150px; float:left; text-align:right;}

/* make the input boxes a little wider */
.xf-value {width: 200px}

            <xf:instance id="request" xmlns="">
             <xf:instance id="response" xmlns="">
            <xf:submission id="submit" method="get" action="http://localhost:8080/exist/rest/db/test/replace/replace.xq" replace="instance" instance="response"
               <xf:toggle case="case-busy" ev:event="xforms-submit" />
               <xf:toggle case="case-submit-error" ev:event="xforms-submit-error" />
               <xf:toggle case="case-done" ev:event="xforms-submit-done" />
    <h1>XForms Replace Tester</h1>
        <xf:input ref="input">
        <xf:input ref="pattern">
        <xf:input ref="replacement">
           <xf:case id="ready">
           <xf:submit submission="submit">
            <xf:submit submission="echo-test">
              <xf:label>Echo Test</xf:label>
           <xf:case id="case-busy">
              <p>Waiting for response...</p>
           <xf:case id="case-submit-error">
              <p>The server has returned a submit error event.</p>
            <xf:case id="case-done">
              <xf:output ref="instance('response')/replace-result/text()">

XQuery Replace TesterEdit

If you are using eXist, just place this file on the server in the same folder as your XForms test driver.

In the example above I used a test folder on the localhost:


xquery version "1.0";
declare namespace exist = "http://exist.sourceforge.net/NS/exist"; 
declare namespace system="http://exist-db.org/xquery/system";
declare namespace request="http://exist-db.org/xquery/request";
declare option exist:serialize "method=xml media-type=text/xml indent=yes";
(: replace demo :)
let $input := request:get-parameter('input', '')
let $pattern  := request:get-parameter('pattern', '')
let $replacement := request:get-parameter('replacement', '')

   <input>{$input }</input>
   <replace-result>{replace($input , $pattern, $replacement)}</replace-result>


This shows that you can quickly built tools to teach yourself complex functions like regular expression handling. You can also use the XQuery match function with returns a true/false if the regular expression matches an input string.

There are two variations of this example that are interesting. The first is where you replace the input form with a large text area for doing global replacements of large blocks of text. The second is where you replace the patten input box with a selection list with common replacement patterns.


Examples of replacement functions can be found here:

w3c replace examples

Next Page: Process Step Editor | Previous Page: Event Logger

Home: XForms