mortimerfreeze
mortimerfreeze

Reputation: 95

WSO2 EI: Is it possible to trim spaces from a string stored as a property?

I have a json payload where I'm grabbing an addressLine field:

<property expression="json-eval($.addressLine)" name="addressLine" scope="default" type="STRING"/>

I then have a filter that checks to see if that field is there, and if it is missing, I have some additional logic that runs.

<filter regex="false" source="boolean(get-property('addressLine1'))">
   <then>
      ...
   </then>
</filter>

However, this logic doesn't work correctly when the addressLine value is an empty space (" "). I'm still fairly new to WSO2 EI and I've been looking around online, but haven't seen any sort of way to "trim" spaces from a property so I was curious if this is possible? And if not, what would be the best way to handle this issue? I know the ideal solution would be to not allow any empty space values beforehand, but since the json payload comes in externally from a different service, this was not an option.

Upvotes: 1

Views: 945

Answers (1)

tmoasz
tmoasz

Reputation: 1331

In wso2 for "trim" spaces you can use xpath functions (normalize-space, and translate). So for your addressLine property it should looks like:

    <property expression="translate(normalize-space($ctx:addressLine), ' ', '')"
              name="trimAddress" 
              scope="default" type="STRING"/>

The $ctx:addressLine works the same as get-property() but has better performance to get values from properties.

For filter i prefer also use xpath notation like below, but that is rather personal preferences.

<filter xpath="$ctx:trimAddress = ''">
   <then>
   ...
   </then>
   <else/>
</filter>

Upvotes: 1

Related Questions