The Perfion Query - Expressions
There are currently 5 expressions available:
FormatMultiValues
FormatValue
Convert
Replace
CaseReplace
Expressions are all used to output some feature in a different way than Perfion normally does. The syntax for writing an expression is this:
<Feature id='<FeatureName>'>
<Expression id='<Name>' expression='@<ExpressionCommand>(<expression parameters>)' />
</Feature>
<FeatureName> must contain the name of the feature which value you want to apply the expression to.
<Name> any name you want to give the expression.
<ExpressionCommand> is the name of the command you want to use, either FormatMultiValues, FormatValue, Convert, Replace or CaseReplace.
The expression parameters may contain some extra information needed to process the command.
The possible fixed command names are described for each of the 5 expressions in the following.
FormatMultiValues
FormatMultiValues is probably the most widely used expression. It is used to output a multi value as a single value, typically with a separator between each value.
The syntax is:
@FormatMultiValues(<BeforeValue>{Value}<AfterValue>|<BetweenValue>)
The expression template here contains the following elements:
{Value}: This is a placeholder indicating where each {Value} of the multi value feature should be output.
<BeforeValue>: The text written here (if any) will be output just before each value.
<AfterValue>: The text written here (if any) is output just after each value.
<BetweenValue>: The text written here (if any) will be output between two of the multiple values.
Let us look at an example of its use. Consider the feature “Multi Value Color” which indeed is a multi-value, selectable feature containing colors.
Normally you will select any feature like this:
<Select languages='EN'>
...
<Feature id='MultiColor' />
...
</Select>
And the result could, as an example, contain the following data.
Notice that in this and all following results in this chapter, the three dots denote attributes removed for brevity.
As previously described FormatMultiValues allows you to output them as a single value. Consider the following example:
Notice that both elements <BeforeValue> and <AfterValue> are left empty, while the <BetweenValue> is set to ; (a semi colon). This expression will produce the following output:
Indeed, the MultiColor feature has been output as if it was a single value with a semi-colon separating each value.
An example utilizing the Before- and After-values in the template are the following:
Which will provide the following output:
Unencoding the above value will provide a more recognizable version:
NOTE: it is allowed to use multiple expressions in your query even outputting the same feature. The id of the expression allows you to discern the results from each expression.
FormatValue
Not surprisingly FormatValue can be used to output a value in a different way than Perfion otherwise would have:
Output a number- or date future due to some culture
Output a value supplying a template allowing to output any of the tags instead or in addition to the value.
If an expression template is used, it may contain any or more of the following tags:
{Value} denotes that the value of the feature should be positioned here.
{Caption} denotes where the caption of the feature should be.
{CaptionAlternative} denotes where the alternative caption of the feature should be.
{Abbr} denotes where the abbreviated caption of the feature should be.
{Unit} denotes where the unit of the feature should be.
{Help} denotes where the help-text of the feature should be.
Example 1 - Outputting a number-feature formatted
In this example, RRP is a number-feature containing the retail price of some product. Without an expression, the Perfion API will per default output that formatted using the Invariant Culture meaning with a punctuation mark as decimal separator:
Say you instead want to output it according to Danish culture, getting a comma as decimal separator instead, you can use the expression:
Notice that you supply the wanted culture after a “|” (pipe). The output from the above gives the following result:
You can even pick a C# Numeric Format used to, for example, round the number. The following expressions rounds the price to a whole number:
The above example will output the retail price rounded to a whole number:
Valid culture codes can be found here. C#-style number formatting is described here.
Example 2 - Outputting a date-feature formatted
Date-features are handled much like number-features.
Having a feature named Deadline containing a Date will normally be output like this:
As with numbers you can add a culture:
To get a date formatted in Danish:
Notice that the date is output before the month. You can even specific a C#-style format for example like this:
This adds the name of the day output as well:
C#-style datetime formatting is described here.
Example 3 - Outputting a value supplying an expression template
Instead of using FormatValue on a number in a different culture, you can use an expression template to output more than just the feature value. Consider the following example:
This will not only output the value of the feature, but the value with its caption and unit as can be seen here:
Convert
The convert-expression has the following syntax:
Here name is again the name you want to give the expression while ConversionToPerform can be either “ToFraction” or “To<SomeFont>”
It currently has two capabilities:
Output a number as a fraction. Here you use “ToFraction”
Output a number as a barcode in the so-called EAN13-format. Here you use “To<SomeConversion>” where “<SomeConversion>” must be replaced with one of the available conversions Perfion supports.
Both are described in the following.
Example 1 – Outputting a number as a fraction
Instead of outputting a number with decimals as indeed that, you can choose to output it as a whole number together with a fraction.
Consider the Retail Price-feature mentioned earlier, where we had some product with a retail price of “199.75”. Converting that to a fraction can be done like this:
Resulting in the following being output for that expression.
Notice the “3/4”, which is output instead of the fractional part of “.75”. Perfion will never return an improper fraction and will always reduce the fraction as much as possible.
Example 2 – Outputting a number as a barcode
Perfion is capable of returning a number as a barcode in the so-called EAN13-format or rather as a string (= sequence of characters) that when output using a special font for it, it will indeed output that number as a barcode. Consider the following example:
Notice the Barcode-feature containing the value “123456789012”, i.e., a 12-digit number.
This number can be output as a barcode in the EAN13-format using the following expression:
This instructs Convert-expressions instructs Perfion to convert the value of barcode to a string, that if output using a font named EAN13 will produce a barcode representing the original value.
The result of the expression is shown here:
The result "b3D5FG|XYPQRx seems weird at first, but when output using the appropriate EAN13-font, it looks like this: