Replaces all occurrences of a given text or pattern by a different text. You can give multiple replacement instructions in one go.
Returns the text after the replacements have been made. USoft Delivery Manager uses this mechanism to do machine-specific replacements when data are deployed on target machines.
Syntax
The required txt is a regular text string, for example, a narrative text, or an SQL statement. This is the input text.
The replacement instructions must be expressed either in a select attribute or in a varlist attribute. One of these is required, but you cannot have both. You can pass input parameters to the replacement routine by using variable references in the select attribute value.
Example
In this example, each occurrence in the TRAVEL.txt file of ITALIA is translated to ITALIA and each occurrence of GERMANY to DEUTSCHLAND, and the result is returned.
<example xmlns:pc="Processing.Command" xmlns:assign="Processing.Command.Assign" >
<pc:defs><Settings><Setting NAME="ITALY" VALUE="ITALIA"/><Setting NAME="GERMANY" VALUE="DEUTSCHLAND"/></Settings></pc:defs>
<pc:ReadFile filepath="TRAVEL.txt" assign:fileContent="."/>
<pc:assign-nodeset settings="{//pc:defs/*}"/>
<pc:ReplaceVariables txt="{$fileContent}" select="$settings"/>
</example>
|
If you are using select, the select attribute must be an XPath expression referencing an XML fragment that has a single top-level node with 1 or more child nodes. 0 child nodes is also legal but will have no effect. Each of the child nodes must have:
•an attribute or a child element named either NAME or PATTERN, and: •an attribute or a child element named VALUE, for example:
<r>
<abc NAME="lazy" VALUE="wild"/>
<abc NAME="dog" VALUE="lazy cat"/>
</r>
|
or:
<r>
<abc><NAME>lazy</NAME><VALUE>wild</VALUE></abc>
<def><NAME>dog</NAME><VALUE>lazy cat</VALUE></def>
</r>
|
or:
<r>
<abc><NAME>active</NAME><VALUE>lazy</VALUE></abc>
<def NAME="dog" VALUE="active cat"/>
<qwe><PATTERN>a(?=\s*active)</PATTERN><VALUE>an</VALUE></qwe>
</r>
|
The name of the top-level element (here, r) and the names of its child elements (here, abc, def, qwe) are irrelevant. In the replacement instructions, if you want to use different names than NAME, PATTERN, and VALUE, you can use the strings:ReplaceVariables() function.
|
<example xmlns:pc="Processing.Command">
<pc:defs>
<r><abc NAME="lazy" VALUE="wild"/><def NAME="dog" VALUE="lazy cat"/></r>
</pc:defs>
<pc:ReplaceVariables
txt="This is a story about a dog. The dog is named Max."
select="/example/pc:defs[1]/r" />
</example>
|
The result of this example is:
This is a story about a wild cat. The wild cat is named Max.
|
|
<example xmlns:pc="Processing.Command">
<pc:ReplaceVariables
txt="This is a story about a dog. The dog is named Max."
select=".">
<r>
<abc><NAME>active</NAME><VALUE>lazy</VALUE></abc>
<def NAME="dog" VALUE="active cat"/> >
<qwe><PATTERN>a(?=\s*active)</PATTERN><VALUE>an</VALUE></qwe> <!-- a before active is replaced by an -->
</r>
</pc:ReplaceVariables>
</example>
|
The result of this example is:
This is a story about an lazy cat. The lazy cat is named Max.
|
All the replacements are executed recursively, and all the matching replacements are ALWAYS executed, regardless of the order in which the instructions are stated (this is the difference with varlist in the next section). Following about, a is changed to an because, at one point in the replacement process, the text has "active cat", and an remains a part of the end result after the replacement of active by lazy. This unwanted effect may be countered by adding an extra instruction:
<example xmlns:pc="Processing.Command">
<pc:ReplaceVariables
txt="This is a story about a dog. The dog is named Max."
select=".">
<r>
<abc><NAME>active</NAME><VALUE>lazy</VALUE></abc>
<def NAME="dog" VALUE="active cat"/> >
<qwe><PATTERN>a(?=\s*active)</PATTERN><VALUE>an</VALUE></qwe> <!-- a before active is replaced by an -->
<qwe><PATTERN>an(?=\s*lazy)</PATTERN><VALUE>a</VALUE></qwe> --><!-- an before lazy is replaced by a -->
</r>
</pc:ReplaceVariables>
</example>
|
The result of this example is:
This is a story about a lazy cat. The lazy cat is named Max.
|
|
If you are using varlist, the varlist attribute must be an ordered list of one or more name-value pairs in the following syntax:
varlist ::= name=value[,name=value...]
|
|
<example xmlns:pc="Processing.Command">
<pc:ReplaceVariables
txt="This is a story about a dog. The dog is named Max."
varlist="dog=active cat;active=lazy" />
</example>
|
The result of this example is:
This is a story about a lazy cat. The lazy cat is named Max.
|
In the same way as with replacements expressed by select, each of the replacements expressed by varlist is applied recursively to the input text. But UNLIKE replacements with select, each replacement with varlist is executed only once in the stated sequence of replacements. Compare:
<example xmlns:pc="Processing.Command">
<pc:ReplaceVariables
txt="This is a story about a dog. The dog is named Max."
varlist="active=lazy;dog=active cat" />
</example>
|
The result of this example is:
This is a story about a active cat. The active cat is named Max.
|
|
See also
Passing input parameters to the replacement routine
Variables
strings:ReplaceVariables
|