LXP supports a simple search-and-replace mechanism for variable values with its
<varparser> tag. This tag takes two
attributes—find and replace. When you use the
<varparser> tag, a region is opened within which any variable value that is
substituted
will be filtered through the defined search-and-replace rule.
The <varparser> is primarily useful for stripping or escaping unwanted
characters. For example, in preparation to execute a SQL statement, single-quotes (') must
be prefixed by a backslash, as a single-quote delimits string constants to PostgreSQL. Example 13-12 demonstrates the escaping of single-quotes in a variable called
txt.
Example 13-12. Using <varparser> to prepare SQL
<lxp>
<varparser find="'" replace="\'">
<include sql="SELECT * FROM table WHERE txtfield = '$txt'">
<field /><br />
</include>
</varparser>
</lxp>
In Example 13-12, the <varparser find="'" replace="\'">
tag instructs LXP to replace any single-quote with a back-referenced \' sequence within any
substituted
variable value.
Note that this search-and-replace occurs
only
for
substituted variable values
.
As such, the literally typed apostrophes in the sql attribute of the
<include> tag are left unchanged; only the contents of variable values being
substituted
within that attribute (e.g., the txt variable's value, in
Example 13-12) are modified upon their substitution.
The closing </varparser> tag puts LXP back into normal variable substitution
mode.
Note: You can configure several simultaneous search-and-replace rules by nesting several
<varparser> tags within one another.