Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 2 min read

Support

Motivation

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

Screen Image

Replace Test Tool Before Submit (Image: The original uploader was Dmccreary at English Wikibooks ., CC BY-SA 3.0)

Replace Test Tool After Submit (Image: The original uploader was Dmccreary at English Wikibooks ., CC BY-SA 3.0)

Replace Test

Sample Program

<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ev="http://www.w3.org/2001/xml-events" >
<head>
<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}

</style>
<xf:model>
<xf:instance id="request" xmlns="">
<data>
<input>abcdefghi</input>
<pattern>def</pattern>
<replacement>123</replacement>
</data>
</xf:instance>
<xf:instance id="response" xmlns="">
<data/>
</xf:instance>
<xf:submission id="submit" method="get" action="http://localhost:8080/exist/rest/db/test/replace/replace.xq" replace="instance" instance="response"
separator="&">
<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" />
</xf:submission>
</xf:model>
</head>
<body>
<h1>XForms Replace Tester</h1>
<xf:input ref="input">
<xf:label>Input:</xf:label>
</xf:input>
<xf:input ref="pattern">
<xf:label>Pattern:</xf:label>
</xf:input>
<xf:input ref="replacement">
<xf:label>Replacement:</xf:label>
</xf:input>
<xf:switch>
<xf:case id="ready">
<xf:submit submission="submit">
<xf:label>Submit</xf:label>
</xf:submit>
<xf:submit submission="echo-test">
<xf:label>Echo Test</xf:label>
</xf:submit>
</xf:case>
<xf:case id="case-busy">
<p>Waiting for response...</p>
</xf:case>
<xf:case id="case-submit-error">
<p>The server has returned a submit error event.</p>
</xf:case>
<xf:case id="case-done">
<xf:output ref="instance('response')/replace-result/text()">
<xf:label>Result:</xf:label>
</xf:output>
</xf:case>
</xf:switch>
</body>
</html>

XQuery Replace Tester

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:

localhost/exist/rest/db/test/replace/replace.xq

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', '')

return

{$input }
{$pattern}
{$replacement}
{replace($input , $pattern, $replacement)}

Discussion

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.

References

Examples of replacement functions can be found here:

w3c replace examples

Where this page came from

This page was imported from Wikibooks. From “XForms” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

Get each new page of

XForms

Following means one email when a page is added, nothing when a page changes.

Following means one email when a page is added, nothing when a page changes.

Comments

Spinner Logo

Next in XForms

CC BY-SA 4.0
*/ .inspector xf|input > xf|label, .inspector xf|select > xf|label, .inspector xf|select1 > xf|label, .inspector xf|textarea > xf|label {text-align:right; padding-right:10px; width:200px; float:left; text-align:right;} /* This line ensures all the separate label controls in the inspector appear on t
CC BY-SA 4.0
This example assumes that the user wants to do these operations on a simple form and guides the user through a series of questions. A series of defaults is assumed but the user is allowed to override these defaults when the requirements differ from the standard. The phrase "convention over configura
CC BY-SA 4.0
In our example a dashboard may have up to 36 portlets, but best practices usually contain 7 portlets plus or minus one or two. ## Application Design Our dashboard layout will be one of three types: - row oriented where all portlets in a row have the same height - column oriented where all portlets i
CC BY-SA 4.0
XForms, Web Service Tester
CC BY-SA 4.0
These tools are frequently used during the process of creating an enterprise-wide controlled vocabulary. These vocabularies are used for identifying data elements or expressing business rules in a consistent way across multiple organizations. ## Method In this program we will have once instance that
CC BY-SA 4.0
At the end the line items are totaled and an indication made to see if the invoice was paid or not. 123 2008-10-10 Acme Services 123 Main St. Anytown, MN 12345 Acme Consumer 123 Main St. Anytown, MN 12345 Widget 123 100.00 Widget 456 200.00 Widget 789 300.00 net-30 false ## XForms User Interface The