Applying string transformation rules

String transformation rules are used to transform an input character string to a user-friendly output character string. In NXT, transformations are used for the following:

There are two types of transformations that you can create:

Find-replace transformations

Find-replace transformations use find and replace attributes that are stated in the XIL file. A Find-replace transformation replaces all entries corresponding to the find attribute with entries of the replace attribute. You can compose transformation rules using the syntax of a Find-replace transformation. Consider the following <rule> node:


<rule find="_" replace=" " />
		

The transformation in the previous example finds all underscores in the source string and replaces them with spaces. When this <rule> node is added to the XIL file, the output for the string day_of_week is transformed to day of week.

Use the regular expressions that follow ECMAScript syntax for the find attribute and the substitution syntax for the replace attribute. For example, to convert the string the number is 1234 or 5678 to the string the number is code(1234) or code(5678), use the following <rule> node:


<rule find="(\d{4})" replace="code(%1%)"/>
		

A digit between percent signs ( % ) is the match number in the regular expression.

For case shift, use following converters between percent signs ( % ):

The following example converts the string this is the test to the string this is the TEST:


<rule find="(test)" replace="%1u%"/>
		

The following example converts the first letter of each word to uppercase:


<rule find="\b(\w)" replace="%1u%"/>
		

Match-replace transformations

If the match and replace attributes are specified in the <rule> node, and the find attribute is not specified, the match-replace transformation is performed. If the source string completely matches the category that is specified in the match attribute, this string is replaced by the category of the replace attribute. Use regular expressions that follow ECMAScript syntax for the match attribute and the substitution syntax for the replace attribute. The following example converts the string 4 to the string Less than 5 (4):


<rule match="([0-4])" replace="Less than 5 (%1%)"/>
	

Note: Find-replace transformations replace all occurrences of a specified attribute in facets and facet categories. For example, the text facet changes to test, if you apply the following rule to this facet: <rule find="x" replace="s"/>. But with the match-replace transformation the text facet remains unchanged, because you search for a facet or a facet category that fully matches the specified attribute.


String transformation algorithm

The string transformation engine recursively passes through all of the <rule> nodes. The source string is passed only to the first <rule> node. The other <rule> nodes receive the results from the previous node. If a node contains child nodes, the transformation of the parent node is performed fist, and then the results are passed to the child nodes. The following rules are used in the string transformation algorithm:

Facet rule tester

You can use facet rule tester to validate transformation rules for strings in the XIL file. Since the 4.8 version of NXT, the facet rule tester is included in the NXT Builder and is available in c:\Program Files\Rocket\NXT 4\Builder\bin\ directory for 32-bit Windows version, and in c:\Program Files (x86)\Rocket\NXT 4\Builder\bin\ directory for 64-bit Windows version.

To validate the transformation rules with the facet rule tester, complete the following steps:

  1. Open the facet rule tester.
  2. In the File field, type the path to the XIL file.
  3. In the Source column, enter any number of strings for validation.
  4. The transformation results are displayed in the Result column.

Note: Before validation, check that the XIL file has the <facet-name-rules> section.

The facet rule tester tracks changes in the XIL file. You can save changes in the XIL file and immediately see the results of transformation in the Result column. For additional convenience, you can select Always on top and Opacity settings from View menu.

You can also test the rules in Content Network Manager in Edit title rules dialog.