Example: SpEL Expression Usage
Using SpEL, you can easily manipulate your data. Here you are provided with some concrete examples of SpEL usage in Data Hub transformation expressions.
SpEL expressions allow you to define sophisticated data transformations that go beyond simple one-to-one mappings of attributes. However, SpEL has its limitations. If you must perform complex data processing, then consider writing a handler instead. Don't try to do everything with SpEL alone.
All expressions within the target XML <transformationExpression> element are parsed by the SpEL expression parser. You can also employ SpEL during composition by adding spel="true" to the <expression> tag of your canonical XML.
Literal Expressions
You can use literal expressions to replace an attribute with a fixed value of any type, such as a String, Integer, Double, or Boolean. You can also assign the value null. In the following example we replace the catalog version with an empty string, for all values of catalogVersion.
<attribute> <name>catalogVersion</name> <localizable>false</localizable> <collection>false</collection> <transformationExpression>""</transformationExpression> <exportCode>$catalogVersion</exportCode> <mandatoryInHeader>true</mandatoryInHeader> </attribute>
The literal expression type should match the type expected by the target system for this attribute.
Methods
All standard Java methods that are available in SpEL can be used in your transformation expressions. Here is an example using the substring() method that is provided in the java.lang.String class.
<attribute> <name>name</name> <localizable>false</localizable> <collection>false</collection> <exportCode>name</exportCode> <transformationExpression>name.substring(2)</transformationExpression> </attribute>
The example removes the first two characters from all values of the attribute name. The attribute must be of type String to use this method..
Ternary Operators
Using the ternary operator to apply conditional logic, you can assign a value to an attribute when it matches some other value. In the following example we identify any description attributes that evaluate to an empty string.
<attribute> <name>description</name> <localizable>true</localizable> <collection>false</collection> <transformationExpression>styleDescription != "" ? styleDescription : "No description available"</transformationExpression> <exportCode>description</exportCode> <mandatoryInHeader>false</mandatoryInHeader> </attribute>
If the value is not empty, we pass it on unaltered. However, if the description is an empty string, we insert the text "No description available". Note that we have also used the logical operator NOT (!) here.
Regular Expressions
Regular expressions, and the matches operator, can be used to return a boolean true or false value. Here we test the validity of an email address supplied in the canonical item email attribute. We then populate a new target attribute called validEmail with the boolean result.
<attribute>
<name>validEmail</name>
<localizable>false</localizable>
<collection>false</collection>
<exportCode>validEmail</exportCode>
<transformationExpression>email matches '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$'</transformationExpression>
</attribute>
In this hypothetical scenario, the attribute validEmail is not defined in the canonical item XML, but is required by the target system. Since the required true or false value can easily be determined from the canonical attribute email, we can use a SpEL expression in the target XML. There is no need to define a canonical item attribute called validEmail.
Combining SpEL Functions
SpEL expressions can be combined for more sophisticated transformations. Here, we first check whether the first four characters of the code attribute string are three uppercase alpha characters followed by a hyphen. If they are, we pass the code unaltered to the target attribute prodCode. If they are not, then we replace anything preceding the hyphen in the code value with UCP. The example combines the substring method, regular expressions, and concatenation, along with the ternary operator.
<attribute>
<name>code</name>
<localizable>true</localizable>
<collection>false</collection>
<exportCode>prodCode</exportCode>
<transformationExpression>code.substring(0,4) matches '^[A-Z]{3}-' ? code : 'UCP' + code.substring(code.IndexOf('-'))</transformationExpression>
</attribute>
The extent to which you should combine SpEL functions is limited. For complex transformations of canonical or target item attribute values, consider using a handler instead.