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:

  1. Output a number- or date future due to some culture

  2. 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:

  1. Output a number as a fraction. Here you use “ToFraction”

  2. 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:

For a barcode to be output the value may only contain digits. Depending on the kind of barcode, it must either contain 11 or 12 digits.

The table below shows the supported barcode-font expressions together with the requirement for the value to be output and the appropriate font to use:

Expression

Requirement

Font Names

Download Font

ToFontEAN13

Input must be 12 digits

EAN-13

FontPalace

ToFontEan13-FromUpca

Input must be 11 digits

As above

As above

ToFontUpca

Input must be 12 digits

As above

As above

ToFontUpca-FromEan13

Input must be 12 digits

As above

As above

 

 

 

 

ToFont2EAN13

Input must be 12 digits

Code EAN13

GrandZenu.net

ToFont2Ean13-FromUpca

Input must be 11 digits

As above

As above

ToFont2Upca

Input must be 12 digits

As above

As above

ToFont2Upca-FromEan13

Input must be 12 digits

As above

As above

EAN13 and UPCA are two very similar standards for outputting barcodes. More on EAN13- and UPCA codes can be found here.

Notice that there are two different fonts to choose from. These vary slightly in their design.              

Font

Output of value “123456789012” using font

EAN-13

Code EAN13

Replace

Replace allows you to replace (part of) some feature value with some other string. The syntax is:

  • <From> contains the value that you want replaced.

  • <To> is the value that replaces it.

Example:

Assume the MyString-feature holds the value “This is the original text” the above expression together with its feature value, will be output like this:

Notice how the word “original” is replaced with “updated”. The string matching is done case-insensitively.

CaseReplace

CaseReplace allows you to map a set of feature values to another set of values. The syntax is as follows:

  • <Fromx> denotes that if the feature has that value, then it should be replaced with its corresponding “To”-value, <Tox>.

  • <default> is the default value output if the feature value does not match any of the values <From1>, <From2>, … <Fromn>.

Example

Consider the following expression:

This simply means, that if “MyString” has the value “red” it is replaced with “Red”, has it the value “hi” it is replaced with “Hey” and all other values will cause “No match” to be output.

In case MyString holds the value “red” the following will be output, when both the feature value and the above expressions are output:

Notice that since MyString matches “red” the “To”-value “Red” will be output instead.

Notice further, that CaseReplace, as opposed to Replace, always matches feature values in their entirety. The feature value “The red fox” will hereby not have “red” replaced with “Red”. Instead “No match” will be output since “The red fox” does not match any of the From-values in the example.

As was the case for Replace, the string matching is done case-insensitively.